Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 3 | #include <http_request.h> |
| 4 | #include <http_response.h> |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 5 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 6 | namespace crow |
| 7 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 8 | struct SecurityHeadersMiddleware |
| 9 | { |
| 10 | struct Context |
| 11 | { |
| 12 | }; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 13 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 14 | void beforeHandle(crow::Request& req, Response& res, Context& ctx) |
| 15 | { |
Ed Tanous | fd828ba | 2018-08-09 10:58:08 -0700 | [diff] [blame] | 16 | #ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 17 | if ("OPTIONS"_method == req.method()) |
| 18 | { |
| 19 | res.end(); |
| 20 | } |
| 21 | #endif |
Ed Tanous | fd828ba | 2018-08-09 10:58:08 -0700 | [diff] [blame] | 22 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 23 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | void afterHandle(Request& req, Response& res, Context& ctx) |
| 25 | { |
| 26 | /* |
| 27 | TODO(ed) these should really check content types. for example, |
| 28 | X-UA-Compatible header doesn't make sense when retrieving a JSON or |
| 29 | javascript file. It doesn't hurt anything, it's just ugly. |
| 30 | */ |
Ed Tanous | 750ceae | 2018-11-30 15:02:22 -0800 | [diff] [blame] | 31 | using bf = boost::beast::http::field; |
| 32 | res.addHeader(bf::strict_transport_security, "max-age=31536000; " |
| 33 | "includeSubdomains; " |
| 34 | "preload"); |
| 35 | res.addHeader(bf::x_frame_options, "DENY"); |
| 36 | |
| 37 | res.addHeader(bf::pragma, "no-cache"); |
| 38 | res.addHeader(bf::cache_control, "no-Store,no-Cache"); |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 39 | |
Ed Tanous | a3268f9 | 2019-09-23 14:06:03 -0700 | [diff] [blame] | 40 | res.addHeader("X-XSS-Protection", "1; " |
| 41 | "mode=block"); |
| 42 | res.addHeader("X-Content-Type-Options", "nosniff"); |
| 43 | |
| 44 | #ifndef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION |
Ed Tanous | e6de21a | 2019-08-21 12:50:42 -0700 | [diff] [blame] | 45 | res.addHeader("Content-Security-Policy", "default-src 'none'; " |
| 46 | "img-src 'self' data:; " |
| 47 | "font-src 'self'; " |
| 48 | "style-src 'self'; " |
| 49 | "script-src 'self'; " |
| 50 | "connect-src 'self' wss:"); |
| 51 | // The KVM currently needs to load images from base64 encoded |
| 52 | // strings. img-src 'self' data: is used to allow that. |
Ed Tanous | 3eb2f35 | 2018-12-20 12:30:45 -0800 | [diff] [blame] | 53 | // https://stackoverflow.com/questions/18447970/content-security-policy-data-not-working-for-base64-images-in-chrome-28 |
Ed Tanous | e6de21a | 2019-08-21 12:50:42 -0700 | [diff] [blame] | 54 | |
Ed Tanous | a3268f9 | 2019-09-23 14:06:03 -0700 | [diff] [blame] | 55 | #else |
| 56 | // If XSS is disabled, we need to allow loading from addresses other |
| 57 | // than self, as the BMC will be hosted elsewhere. |
| 58 | res.addHeader("Content-Security-Policy", "default-src 'none'; " |
| 59 | "img-src *; " |
| 60 | "font-src *; " |
| 61 | "style-src *; " |
| 62 | "script-src *; " |
| 63 | "connect-src *"); |
Ed Tanous | fd828ba | 2018-08-09 10:58:08 -0700 | [diff] [blame] | 64 | |
Ed Tanous | a3268f9 | 2019-09-23 14:06:03 -0700 | [diff] [blame] | 65 | const std::string_view origin = req.getHeaderValue("Origin"); |
| 66 | res.addHeader(bf::access_control_allow_origin, origin); |
Ed Tanous | 750ceae | 2018-11-30 15:02:22 -0800 | [diff] [blame] | 67 | res.addHeader(bf::access_control_allow_methods, "GET, " |
| 68 | "POST, " |
| 69 | "PUT, " |
Ed Tanous | da7f41e | 2018-12-10 13:36:40 -0800 | [diff] [blame] | 70 | "PATCH, " |
| 71 | "DELETE"); |
Ed Tanous | 750ceae | 2018-11-30 15:02:22 -0800 | [diff] [blame] | 72 | res.addHeader(bf::access_control_allow_credentials, "true"); |
| 73 | res.addHeader(bf::access_control_allow_headers, "Origin, " |
| 74 | "Content-Type, " |
| 75 | "Accept, " |
| 76 | "Cookie, " |
| 77 | "X-XSRF-TOKEN"); |
Ed Tanous | fd828ba | 2018-08-09 10:58:08 -0700 | [diff] [blame] | 78 | |
| 79 | #endif |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 81 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | } // namespace crow |