blob: 561fd816353ace7a5952d7f6e20f9d364c0df184 [file] [log] [blame]
Ed Tanous8041f312017-04-03 09:47:01 -07001#pragma once
2
3#include <crow/http_request.h>
4#include <crow/http_response.h>
5
Ed Tanous1abe55e2018-09-05 08:30:59 -07006namespace crow
7{
Ed Tanous55c7b7a2018-05-22 15:27:24 -07008static const char* strictTransportSecurityKey = "Strict-Transport-Security";
9static const char* strictTransportSecurityValue =
Ed Tanousf3d847c2017-06-12 16:01:42 -070010 "max-age=31536000; includeSubdomains; preload";
11
Ed Tanous55c7b7a2018-05-22 15:27:24 -070012static const char* uaCompatabilityKey = "X-UA-Compatible";
13static const char* uaCompatabilityValue = "IE=11";
Ed Tanousf3d847c2017-06-12 16:01:42 -070014
Ed Tanous55c7b7a2018-05-22 15:27:24 -070015static const char* xframeKey = "X-Frame-Options";
16static const char* xframeValue = "DENY";
Ed Tanousf3d847c2017-06-12 16:01:42 -070017
Ed Tanous55c7b7a2018-05-22 15:27:24 -070018static const char* xssKey = "X-XSS-Protection";
19static const char* xssValue = "1; mode=block";
Ed Tanousf3d847c2017-06-12 16:01:42 -070020
Ed Tanous55c7b7a2018-05-22 15:27:24 -070021static const char* contentSecurityKey = "X-Content-Security-Policy";
22static const char* contentSecurityValue = "default-src 'self'";
Ed Tanous8041f312017-04-03 09:47:01 -070023
Ed Tanous55c7b7a2018-05-22 15:27:24 -070024static const char* pragmaKey = "Pragma";
25static const char* pragmaValue = "no-cache";
Ed Tanous746b22a2017-11-07 15:32:12 -080026
Ed Tanous55c7b7a2018-05-22 15:27:24 -070027static const char* cacheControlKey = "Cache-Control";
28static const char* cacheControlValue = "no-Store,no-Cache";
Ed Tanous746b22a2017-11-07 15:32:12 -080029
Ed Tanous1abe55e2018-09-05 08:30:59 -070030struct SecurityHeadersMiddleware
31{
32 struct Context
33 {
34 };
Ed Tanous8041f312017-04-03 09:47:01 -070035
Ed Tanous1abe55e2018-09-05 08:30:59 -070036 void beforeHandle(crow::Request& req, Response& res, Context& ctx)
37 {
Ed Tanousfd828ba2018-08-09 10:58:08 -070038#ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
Ed Tanous1abe55e2018-09-05 08:30:59 -070039 if ("OPTIONS"_method == req.method())
40 {
41 res.end();
42 }
43#endif
Ed Tanousfd828ba2018-08-09 10:58:08 -070044 }
Ed Tanous8041f312017-04-03 09:47:01 -070045
Ed Tanous1abe55e2018-09-05 08:30:59 -070046 void afterHandle(Request& req, Response& res, Context& ctx)
47 {
48 /*
49 TODO(ed) these should really check content types. for example,
50 X-UA-Compatible header doesn't make sense when retrieving a JSON or
51 javascript file. It doesn't hurt anything, it's just ugly.
52 */
53 res.addHeader(strictTransportSecurityKey, strictTransportSecurityValue);
54 res.addHeader(uaCompatabilityKey, uaCompatabilityValue);
55 res.addHeader(xframeKey, xframeValue);
56 res.addHeader(xssKey, xssValue);
57 res.addHeader(contentSecurityKey, contentSecurityValue);
58 res.addHeader(pragmaKey, pragmaValue);
59 res.addHeader(cacheControlKey, cacheControlValue);
Ed Tanousfd828ba2018-08-09 10:58:08 -070060
61#ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
62
Ed Tanous1abe55e2018-09-05 08:30:59 -070063 res.addHeader("Access-Control-Allow-Origin", "http://localhost:8080");
64 res.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH");
65 res.addHeader("Access-Control-Allow-Credentials", "true");
66 res.addHeader("Access-Control-Allow-Headers",
67 "Origin, Content-Type, Accept, Cookie, X-XSRF-TOKEN");
Ed Tanousfd828ba2018-08-09 10:58:08 -070068
69#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 }
Ed Tanous8041f312017-04-03 09:47:01 -070071};
Ed Tanous1abe55e2018-09-05 08:30:59 -070072} // namespace crow