blob: 750f87b7192c7b39a2514136160b65fd93776508 [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
6namespace crow {
Ed Tanous55c7b7a2018-05-22 15:27:24 -07007static const char* strictTransportSecurityKey = "Strict-Transport-Security";
8static const char* strictTransportSecurityValue =
Ed Tanousf3d847c2017-06-12 16:01:42 -07009 "max-age=31536000; includeSubdomains; preload";
10
Ed Tanous55c7b7a2018-05-22 15:27:24 -070011static const char* uaCompatabilityKey = "X-UA-Compatible";
12static const char* uaCompatabilityValue = "IE=11";
Ed Tanousf3d847c2017-06-12 16:01:42 -070013
Ed Tanous55c7b7a2018-05-22 15:27:24 -070014static const char* xframeKey = "X-Frame-Options";
15static const char* xframeValue = "DENY";
Ed Tanousf3d847c2017-06-12 16:01:42 -070016
Ed Tanous55c7b7a2018-05-22 15:27:24 -070017static const char* xssKey = "X-XSS-Protection";
18static const char* xssValue = "1; mode=block";
Ed Tanousf3d847c2017-06-12 16:01:42 -070019
Ed Tanous55c7b7a2018-05-22 15:27:24 -070020static const char* contentSecurityKey = "X-Content-Security-Policy";
21static const char* contentSecurityValue = "default-src 'self'";
Ed Tanous8041f312017-04-03 09:47:01 -070022
Ed Tanous55c7b7a2018-05-22 15:27:24 -070023static const char* pragmaKey = "Pragma";
24static const char* pragmaValue = "no-cache";
Ed Tanous746b22a2017-11-07 15:32:12 -080025
Ed Tanous55c7b7a2018-05-22 15:27:24 -070026static const char* cacheControlKey = "Cache-Control";
27static const char* cacheControlValue = "no-Store,no-Cache";
Ed Tanous746b22a2017-11-07 15:32:12 -080028
Ed Tanous8041f312017-04-03 09:47:01 -070029struct SecurityHeadersMiddleware {
Ed Tanous55c7b7a2018-05-22 15:27:24 -070030 struct Context {};
Ed Tanous8041f312017-04-03 09:47:01 -070031
Ed Tanousfd828ba2018-08-09 10:58:08 -070032 void beforeHandle(crow::Request& req, Response& res, Context& ctx) {
33#ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
34 if ("OPTIONS"_method == req.method()) {
35 res.end();
36 }
37#endif
38 }
Ed Tanous8041f312017-04-03 09:47:01 -070039
Ed Tanous55c7b7a2018-05-22 15:27:24 -070040 void afterHandle(Request& req, Response& res, Context& ctx) {
Ed Tanousf3d847c2017-06-12 16:01:42 -070041 /*
42 TODO(ed) these should really check content types. for example,
43 X-UA-Compatible header doesn't make sense when retrieving a JSON or
44 javascript file. It doesn't hurt anything, it's just ugly.
45 */
Ed Tanous55c7b7a2018-05-22 15:27:24 -070046 res.addHeader(strictTransportSecurityKey, strictTransportSecurityValue);
47 res.addHeader(uaCompatabilityKey, uaCompatabilityValue);
48 res.addHeader(xframeKey, xframeValue);
49 res.addHeader(xssKey, xssValue);
50 res.addHeader(contentSecurityKey, contentSecurityValue);
51 res.addHeader(pragmaKey, pragmaValue);
52 res.addHeader(cacheControlKey, cacheControlValue);
Ed Tanousfd828ba2018-08-09 10:58:08 -070053
54#ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
55
56 res.addHeader("Access-Control-Allow-Origin", "http://localhost:8080");
57 res.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH");
58 res.addHeader("Access-Control-Allow-Credentials", "true");
59 res.addHeader("Access-Control-Allow-Headers",
60 "Origin, Content-Type, Accept, Cookie, X-XSRF-TOKEN");
61
62#endif
Ed Tanousf3d847c2017-06-12 16:01:42 -070063 }
Ed Tanous8041f312017-04-03 09:47:01 -070064};
Ed Tanous911ac312017-08-15 09:37:42 -070065} // namespace crow