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 | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 7 | static const char* strict_transport_security_key = "Strict-Transport-Security"; |
| 8 | static const char* strict_transport_security_value = |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 9 | "max-age=31536000; includeSubdomains; preload"; |
| 10 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 11 | static const char* ua_compatability_key = "X-UA-Compatible"; |
| 12 | static const char* ua_compatability_value = "IE=11"; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 13 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 14 | static const char* xframe_key = "X-Frame-Options"; |
| 15 | static const char* xframe_value = "DENY"; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 16 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 17 | static const char* xss_key = "X-XSS-Protection"; |
| 18 | static const char* xss_value = "1; mode=block"; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 19 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 20 | static const char* content_security_key = "X-Content-Security-Policy"; |
| 21 | static const char* content_security_value = "default-src 'self'"; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 22 | |
Ed Tanous | 746b22a | 2017-11-07 15:32:12 -0800 | [diff] [blame] | 23 | static const char* pragma_key = "Pragma"; |
| 24 | static const char* pragma_value = "no-cache"; |
| 25 | |
| 26 | static const char* cache_control_key = "Cache-Control"; |
| 27 | static const char* cache_control_value = "no-Store,no-Cache"; |
| 28 | |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 29 | struct SecurityHeadersMiddleware { |
| 30 | struct context {}; |
| 31 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 32 | void before_handle(crow::request& req, response& res, context& ctx) {} |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 33 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 34 | void after_handle(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 | */ |
| 40 | res.add_header(strict_transport_security_key, |
| 41 | strict_transport_security_value); |
| 42 | res.add_header(ua_compatability_key, ua_compatability_value); |
| 43 | res.add_header(xframe_key, xframe_value); |
| 44 | res.add_header(xss_key, xss_value); |
| 45 | res.add_header(content_security_key, content_security_value); |
Ed Tanous | 746b22a | 2017-11-07 15:32:12 -0800 | [diff] [blame] | 46 | res.add_header(pragma_key, pragma_value); |
| 47 | res.add_header(cache_control_key, cache_control_value); |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 48 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 49 | }; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 50 | } // namespace crow |