summaryrefslogtreecommitdiff
path: root/include/security_headers_middleware.hpp
blob: f60ce766b2bc03dce3c8b5d576fbe53b12ccc075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once

#include <http_request.h>
#include <http_response.h>

namespace crow
{
struct SecurityHeadersMiddleware
{
    struct Context
    {
    };

    void beforeHandle(crow::Request& req, Response& res, Context& ctx)
    {
#ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
        if ("OPTIONS"_method == req.method())
        {
            res.end();
        }
#endif
    }

    void afterHandle(Request& req, Response& res, Context& ctx)
    {
        /*
         TODO(ed) these should really check content types.  for example,
         X-UA-Compatible header doesn't make sense when retrieving a JSON or
         javascript file.  It doesn't hurt anything, it's just ugly.
         */
        using bf = boost::beast::http::field;
        res.addHeader(bf::strict_transport_security, "max-age=31536000; "
                                                     "includeSubdomains; "
                                                     "preload");
        res.addHeader(bf::x_frame_options, "DENY");

        res.addHeader(bf::pragma, "no-cache");
        res.addHeader(bf::cache_control, "no-Store,no-Cache");

        res.addHeader("X-XSS-Protection", "1; "
                                          "mode=block");
        res.addHeader("X-Content-Type-Options", "nosniff");

#ifndef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
        res.addHeader("Content-Security-Policy", "default-src 'none'; "
                                                 "img-src 'self' data:; "
                                                 "font-src 'self'; "
                                                 "style-src 'self'; "
                                                 "script-src 'self'; "
                                                 "connect-src 'self' wss:");
        // The KVM currently needs to load images from base64 encoded
        // strings. img-src 'self' data: is used to allow that.
        // https://stackoverflow.com/questions/18447970/content-security-policy-data-not-working-for-base64-images-in-chrome-28

#else
        // If XSS is disabled, we need to allow loading from addresses other
        // than self, as the BMC will be hosted elsewhere.
        res.addHeader("Content-Security-Policy", "default-src 'none'; "
                                                 "img-src *; "
                                                 "font-src *; "
                                                 "style-src *; "
                                                 "script-src *; "
                                                 "connect-src *");

        const std::string_view origin = req.getHeaderValue("Origin");
        res.addHeader(bf::access_control_allow_origin, origin);
        res.addHeader(bf::access_control_allow_methods, "GET, "
                                                        "POST, "
                                                        "PUT, "
                                                        "PATCH, "
                                                        "DELETE");
        res.addHeader(bf::access_control_allow_credentials, "true");
        res.addHeader(bf::access_control_allow_headers, "Origin, "
                                                        "Content-Type, "
                                                        "Accept, "
                                                        "Cookie, "
                                                        "X-XSRF-TOKEN");

#endif
    }
};
} // namespace crow