summaryrefslogtreecommitdiff
path: root/include/security_headers_middleware.hpp
blob: 561fd816353ace7a5952d7f6e20f9d364c0df184 (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
#pragma once

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

namespace crow
{
static const char* strictTransportSecurityKey = "Strict-Transport-Security";
static const char* strictTransportSecurityValue =
    "max-age=31536000; includeSubdomains; preload";

static const char* uaCompatabilityKey = "X-UA-Compatible";
static const char* uaCompatabilityValue = "IE=11";

static const char* xframeKey = "X-Frame-Options";
static const char* xframeValue = "DENY";

static const char* xssKey = "X-XSS-Protection";
static const char* xssValue = "1; mode=block";

static const char* contentSecurityKey = "X-Content-Security-Policy";
static const char* contentSecurityValue = "default-src 'self'";

static const char* pragmaKey = "Pragma";
static const char* pragmaValue = "no-cache";

static const char* cacheControlKey = "Cache-Control";
static const char* cacheControlValue = "no-Store,no-Cache";

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.
         */
        res.addHeader(strictTransportSecurityKey, strictTransportSecurityValue);
        res.addHeader(uaCompatabilityKey, uaCompatabilityValue);
        res.addHeader(xframeKey, xframeValue);
        res.addHeader(xssKey, xssValue);
        res.addHeader(contentSecurityKey, contentSecurityValue);
        res.addHeader(pragmaKey, pragmaValue);
        res.addHeader(cacheControlKey, cacheControlValue);

#ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION

        res.addHeader("Access-Control-Allow-Origin", "http://localhost:8080");
        res.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH");
        res.addHeader("Access-Control-Allow-Credentials", "true");
        res.addHeader("Access-Control-Allow-Headers",
                      "Origin, Content-Type, Accept, Cookie, X-XSRF-TOKEN");

#endif
    }
};
} // namespace crow