blob: f7bc478d97a7de913499cccd5645a33c506a8d84 [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 Tanous55c7b7a2018-05-22 15:27:24 -070032 void beforeHandle(crow::Request& req, Response& res, Context& ctx) {}
Ed Tanous8041f312017-04-03 09:47:01 -070033
Ed Tanous55c7b7a2018-05-22 15:27:24 -070034 void afterHandle(Request& req, Response& res, Context& ctx) {
Ed Tanousf3d847c2017-06-12 16:01:42 -070035 /*
36 TODO(ed) these should really check content types. for example,
37 X-UA-Compatible header doesn't make sense when retrieving a JSON or
38 javascript file. It doesn't hurt anything, it's just ugly.
39 */
Ed Tanous55c7b7a2018-05-22 15:27:24 -070040 res.addHeader(strictTransportSecurityKey, strictTransportSecurityValue);
41 res.addHeader(uaCompatabilityKey, uaCompatabilityValue);
42 res.addHeader(xframeKey, xframeValue);
43 res.addHeader(xssKey, xssValue);
44 res.addHeader(contentSecurityKey, contentSecurityValue);
45 res.addHeader(pragmaKey, pragmaValue);
46 res.addHeader(cacheControlKey, cacheControlValue);
Ed Tanousf3d847c2017-06-12 16:01:42 -070047 }
Ed Tanous8041f312017-04-03 09:47:01 -070048};
Ed Tanous911ac312017-08-15 09:37:42 -070049} // namespace crow