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 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 6 | namespace crow |
| 7 | { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 8 | static const char* strictTransportSecurityKey = "Strict-Transport-Security"; |
| 9 | static const char* strictTransportSecurityValue = |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 10 | "max-age=31536000; includeSubdomains; preload"; |
| 11 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 12 | static const char* uaCompatabilityKey = "X-UA-Compatible"; |
| 13 | static const char* uaCompatabilityValue = "IE=11"; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 14 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 15 | static const char* xframeKey = "X-Frame-Options"; |
| 16 | static const char* xframeValue = "DENY"; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 17 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 18 | static const char* xssKey = "X-XSS-Protection"; |
| 19 | static const char* xssValue = "1; mode=block"; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 20 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 21 | static const char* contentSecurityKey = "X-Content-Security-Policy"; |
| 22 | static const char* contentSecurityValue = "default-src 'self'"; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 23 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 24 | static const char* pragmaKey = "Pragma"; |
| 25 | static const char* pragmaValue = "no-cache"; |
Ed Tanous | 746b22a | 2017-11-07 15:32:12 -0800 | [diff] [blame] | 26 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 27 | static const char* cacheControlKey = "Cache-Control"; |
| 28 | static const char* cacheControlValue = "no-Store,no-Cache"; |
Ed Tanous | 746b22a | 2017-11-07 15:32:12 -0800 | [diff] [blame] | 29 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | struct SecurityHeadersMiddleware |
| 31 | { |
| 32 | struct Context |
| 33 | { |
| 34 | }; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 35 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 36 | void beforeHandle(crow::Request& req, Response& res, Context& ctx) |
| 37 | { |
Ed Tanous | fd828ba | 2018-08-09 10:58:08 -0700 | [diff] [blame] | 38 | #ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 39 | if ("OPTIONS"_method == req.method()) |
| 40 | { |
| 41 | res.end(); |
| 42 | } |
| 43 | #endif |
Ed Tanous | fd828ba | 2018-08-09 10:58:08 -0700 | [diff] [blame] | 44 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 45 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 46 | void afterHandle(Request& req, Response& res, Context& ctx) |
| 47 | { |
| 48 | /* |
| 49 | TODO(ed) these should really check content types. for example, |
| 50 | X-UA-Compatible header doesn't make sense when retrieving a JSON or |
| 51 | javascript file. It doesn't hurt anything, it's just ugly. |
| 52 | */ |
| 53 | res.addHeader(strictTransportSecurityKey, strictTransportSecurityValue); |
| 54 | res.addHeader(uaCompatabilityKey, uaCompatabilityValue); |
| 55 | res.addHeader(xframeKey, xframeValue); |
| 56 | res.addHeader(xssKey, xssValue); |
| 57 | res.addHeader(contentSecurityKey, contentSecurityValue); |
| 58 | res.addHeader(pragmaKey, pragmaValue); |
| 59 | res.addHeader(cacheControlKey, cacheControlValue); |
Ed Tanous | fd828ba | 2018-08-09 10:58:08 -0700 | [diff] [blame] | 60 | |
| 61 | #ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION |
| 62 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 63 | res.addHeader("Access-Control-Allow-Origin", "http://localhost:8080"); |
| 64 | res.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH"); |
| 65 | res.addHeader("Access-Control-Allow-Credentials", "true"); |
| 66 | res.addHeader("Access-Control-Allow-Headers", |
| 67 | "Origin, Content-Type, Accept, Cookie, X-XSRF-TOKEN"); |
Ed Tanous | fd828ba | 2018-08-09 10:58:08 -0700 | [diff] [blame] | 68 | |
| 69 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 71 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | } // namespace crow |