Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 1 | #include <security_headers_middleware.hpp> |
| 2 | |
| 3 | namespace crow { |
| 4 | |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 5 | static const std::string strict_transport_security_key = |
| 6 | "Strict-Transport-Security"; |
| 7 | static const std::string strict_transport_security_value = |
| 8 | "max-age=31536000; includeSubdomains; preload"; |
| 9 | |
| 10 | static const std::string ua_compatability_key = "X-UA-Compatible"; |
| 11 | static const std::string ua_compatability_value = "IE=11"; |
| 12 | |
| 13 | static const std::string xframe_key = "X-Frame-Options"; |
| 14 | static const std::string xframe_value = "DENY"; |
| 15 | |
| 16 | static const std::string xss_key = "X-XSS-Protection"; |
| 17 | static const std::string xss_value = "1; mode=block"; |
| 18 | |
| 19 | static const std::string content_security_key = "X-Content-Security-Policy"; |
| 20 | static const std::string content_security_value = "default-src 'self'"; |
| 21 | |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 22 | void SecurityHeadersMiddleware::before_handle(crow::request& req, response& res, |
| 23 | context& ctx) {} |
| 24 | |
| 25 | void SecurityHeadersMiddleware::after_handle(request& /*req*/, response& res, |
| 26 | context& ctx) { |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 27 | /* |
| 28 | TODO(ed) these should really check content types. for example, |
| 29 | X-UA-Compatible header doesn't make sense when retrieving a JSON or |
| 30 | javascript file. It doesn't hurt anything, it's just ugly. |
| 31 | */ |
| 32 | res.add_header(strict_transport_security_key, |
| 33 | strict_transport_security_value); |
| 34 | res.add_header(ua_compatability_key, ua_compatability_value); |
| 35 | res.add_header(xframe_key, xframe_value); |
| 36 | res.add_header(xss_key, xss_value); |
| 37 | res.add_header(content_security_key, content_security_value); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 38 | } |
| 39 | } |