Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <crow/http_request.h> |
| 4 | #include <crow/http_response.h> |
| 5 | |
| 6 | namespace crow { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 7 | static const char* strictTransportSecurityKey = "Strict-Transport-Security"; |
| 8 | static const char* strictTransportSecurityValue = |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 9 | "max-age=31536000; includeSubdomains; preload"; |
| 10 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 11 | static const char* uaCompatabilityKey = "X-UA-Compatible"; |
| 12 | static const char* uaCompatabilityValue = "IE=11"; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 13 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 14 | static const char* xframeKey = "X-Frame-Options"; |
| 15 | static const char* xframeValue = "DENY"; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 16 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 17 | static const char* xssKey = "X-XSS-Protection"; |
| 18 | static const char* xssValue = "1; mode=block"; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 19 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 20 | static const char* contentSecurityKey = "X-Content-Security-Policy"; |
| 21 | static const char* contentSecurityValue = "default-src 'self'"; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 22 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 23 | static const char* pragmaKey = "Pragma"; |
| 24 | static const char* pragmaValue = "no-cache"; |
Ed Tanous | 746b22a | 2017-11-07 15:32:12 -0800 | [diff] [blame] | 25 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 26 | static const char* cacheControlKey = "Cache-Control"; |
| 27 | static const char* cacheControlValue = "no-Store,no-Cache"; |
Ed Tanous | 746b22a | 2017-11-07 15:32:12 -0800 | [diff] [blame] | 28 | |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 29 | struct SecurityHeadersMiddleware { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 30 | struct Context {}; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 31 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 32 | void beforeHandle(crow::Request& req, Response& res, Context& ctx) {} |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 33 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 34 | void afterHandle(Request& req, Response& res, Context& ctx) { |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 35 | /* |
| 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 Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 40 | 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 Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 47 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 48 | }; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 49 | } // namespace crow |