Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 2205bbf | 2021-06-17 13:33:47 -0700 | [diff] [blame] | 3 | #include <bmcweb_config.h> |
| 4 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 5 | #include <http_response.hpp> |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 6 | |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 7 | inline void addSecurityHeaders(const crow::Request& req [[maybe_unused]], |
| 8 | crow::Response& res) |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 9 | { |
| 10 | /* |
| 11 | TODO(ed) these should really check content types. for example, |
| 12 | X-UA-Compatible header doesn't make sense when retrieving a JSON or |
| 13 | javascript file. It doesn't hurt anything, it's just ugly. |
| 14 | */ |
| 15 | using bf = boost::beast::http::field; |
| 16 | res.addHeader(bf::strict_transport_security, "max-age=31536000; " |
| 17 | "includeSubdomains; " |
| 18 | "preload"); |
| 19 | res.addHeader(bf::x_frame_options, "DENY"); |
| 20 | |
| 21 | res.addHeader(bf::pragma, "no-cache"); |
| 22 | res.addHeader(bf::cache_control, "no-Store,no-Cache"); |
| 23 | |
| 24 | res.addHeader("X-XSS-Protection", "1; " |
| 25 | "mode=block"); |
| 26 | res.addHeader("X-Content-Type-Options", "nosniff"); |
| 27 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 28 | if (bmcwebInsecureDisableXssPrevention == 0) |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 29 | { |
| 30 | res.addHeader("Content-Security-Policy", "default-src 'none'; " |
| 31 | "img-src 'self' data:; " |
| 32 | "font-src 'self'; " |
| 33 | "style-src 'self'; " |
| 34 | "script-src 'self'; " |
Basheer Ahmed Muddebihal | 09e7afd | 2021-03-17 00:55:57 -0700 | [diff] [blame] | 35 | "connect-src 'self' wss:; " |
| 36 | "form-action 'none'; " |
| 37 | "frame-ancestors 'none'; " |
Jiaqing Zhao | 91ac2e5 | 2022-03-17 00:18:58 +0800 | [diff] [blame] | 38 | "object-src 'none'; " |
Basheer Ahmed Muddebihal | 09e7afd | 2021-03-17 00:55:57 -0700 | [diff] [blame] | 39 | "base-uri 'none' "); |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 40 | // The KVM currently needs to load images from base64 encoded |
| 41 | // strings. img-src 'self' data: is used to allow that. |
Basheer Ahmed Muddebihal | 09e7afd | 2021-03-17 00:55:57 -0700 | [diff] [blame] | 42 | // https://stackoverflow.com/questions/18447970/content-security-polic |
| 43 | // y-data-not-working-for-base64-images-in-chrome-28 |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 44 | } |
| 45 | else |
| 46 | { |
| 47 | // If XSS is disabled, we need to allow loading from addresses other |
| 48 | // than self, as the BMC will be hosted elsewhere. |
| 49 | res.addHeader("Content-Security-Policy", "default-src 'none'; " |
| 50 | "img-src *; " |
| 51 | "font-src *; " |
| 52 | "style-src *; " |
| 53 | "script-src *; " |
Basheer Ahmed Muddebihal | 09e7afd | 2021-03-17 00:55:57 -0700 | [diff] [blame] | 54 | "connect-src *; " |
| 55 | "form-action *; " |
| 56 | "frame-ancestors *; " |
Jiaqing Zhao | 91ac2e5 | 2022-03-17 00:18:58 +0800 | [diff] [blame] | 57 | "object-src *; " |
Basheer Ahmed Muddebihal | 09e7afd | 2021-03-17 00:55:57 -0700 | [diff] [blame] | 58 | "base-uri *"); |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 59 | |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 60 | const std::string_view origin = req.getHeaderValue("Origin"); |
| 61 | res.addHeader(bf::access_control_allow_origin, origin); |
| 62 | res.addHeader(bf::access_control_allow_methods, "GET, " |
| 63 | "POST, " |
| 64 | "PUT, " |
| 65 | "PATCH, " |
| 66 | "DELETE"); |
| 67 | res.addHeader(bf::access_control_allow_credentials, "true"); |
| 68 | res.addHeader(bf::access_control_allow_headers, "Origin, " |
| 69 | "Content-Type, " |
| 70 | "Accept, " |
| 71 | "Cookie, " |
| 72 | "X-XSRF-TOKEN"); |
| 73 | } |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 74 | } |