Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 3 | #include "bmcweb_config.h" |
Ed Tanous | 2205bbf | 2021-06-17 13:33:47 -0700 | [diff] [blame] | 4 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "http_request.hpp" |
| 6 | #include "http_response.hpp" |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 7 | |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 8 | inline void addSecurityHeaders(const crow::Request& req [[maybe_unused]], |
| 9 | crow::Response& res) |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 10 | { |
| 11 | /* |
| 12 | TODO(ed) these should really check content types. for example, |
| 13 | X-UA-Compatible header doesn't make sense when retrieving a JSON or |
| 14 | javascript file. It doesn't hurt anything, it's just ugly. |
| 15 | */ |
| 16 | using bf = boost::beast::http::field; |
| 17 | res.addHeader(bf::strict_transport_security, "max-age=31536000; " |
| 18 | "includeSubdomains; " |
| 19 | "preload"); |
| 20 | res.addHeader(bf::x_frame_options, "DENY"); |
| 21 | |
| 22 | res.addHeader(bf::pragma, "no-cache"); |
| 23 | res.addHeader(bf::cache_control, "no-Store,no-Cache"); |
| 24 | |
| 25 | res.addHeader("X-XSS-Protection", "1; " |
| 26 | "mode=block"); |
| 27 | res.addHeader("X-Content-Type-Options", "nosniff"); |
| 28 | |
Joseph Reynolds | 1d9502b | 2023-05-12 10:47:30 -0500 | [diff] [blame^] | 29 | // Recommendations from https://owasp.org/www-project-secure-headers/ |
| 30 | // https://owasp.org/www-project-secure-headers/ci/headers_add.json |
| 31 | res.addHeader("Referrer-Policy", "no-referrer"); |
| 32 | res.addHeader("Permissions-Policy", "accelerometer=(), " |
| 33 | "ambient-light-sensor=(), " |
| 34 | "autoplay=(), " |
| 35 | "battery=(), " |
| 36 | "bluetooth=(), " |
| 37 | "camera=(), " |
| 38 | "ch-ua=(), " |
| 39 | "ch-ua-arch=(), " |
| 40 | "ch-ua-bitness=(), " |
| 41 | "ch-ua-full-version=(), " |
| 42 | "ch-ua-full-version-list=(), " |
| 43 | "ch-ua-mobile=(), " |
| 44 | "ch-ua-model=(), " |
| 45 | "ch-ua-platform=(), " |
| 46 | "ch-ua-platform-version=(), " |
| 47 | "ch-ua-wow64=(), " |
| 48 | "cross-origin-isolated=(), " |
| 49 | "display-capture=(), " |
| 50 | "encrypted-media=(), " |
| 51 | "execution-while-not-rendered=(), " |
| 52 | "execution-while-out-of-viewport=(), " |
| 53 | "fullscreen=(), " |
| 54 | "geolocation=(), " |
| 55 | "gyroscope=(), " |
| 56 | "hid=(), " |
| 57 | "idle-detection=(), " |
| 58 | "keyboard-map=(), " |
| 59 | "magnetometer=(), " |
| 60 | "microphone=(), " |
| 61 | "midi=(), " |
| 62 | "navigation-override=(), " |
| 63 | "payment=(), " |
| 64 | "picture-in-picture=(), " |
| 65 | "publickey-credentials-get=(), " |
| 66 | "screen-wake-lock=(), " |
| 67 | "serial=(), " |
| 68 | "sync-xhr=(), " |
| 69 | "usb=(self), " |
| 70 | "web-share=(), " |
| 71 | "xr-spatial-tracking2=()"); |
| 72 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 73 | if (bmcwebInsecureDisableXssPrevention == 0) |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 74 | { |
| 75 | res.addHeader("Content-Security-Policy", "default-src 'none'; " |
| 76 | "img-src 'self' data:; " |
| 77 | "font-src 'self'; " |
| 78 | "style-src 'self'; " |
| 79 | "script-src 'self'; " |
Basheer Ahmed Muddebihal | 09e7afd | 2021-03-17 00:55:57 -0700 | [diff] [blame] | 80 | "connect-src 'self' wss:; " |
| 81 | "form-action 'none'; " |
| 82 | "frame-ancestors 'none'; " |
Jiaqing Zhao | 91ac2e5 | 2022-03-17 00:18:58 +0800 | [diff] [blame] | 83 | "object-src 'none'; " |
Basheer Ahmed Muddebihal | 09e7afd | 2021-03-17 00:55:57 -0700 | [diff] [blame] | 84 | "base-uri 'none' "); |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 85 | // The KVM currently needs to load images from base64 encoded |
| 86 | // strings. img-src 'self' data: is used to allow that. |
Basheer Ahmed Muddebihal | 09e7afd | 2021-03-17 00:55:57 -0700 | [diff] [blame] | 87 | // https://stackoverflow.com/questions/18447970/content-security-polic |
| 88 | // y-data-not-working-for-base64-images-in-chrome-28 |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 89 | } |
| 90 | else |
| 91 | { |
| 92 | // If XSS is disabled, we need to allow loading from addresses other |
| 93 | // than self, as the BMC will be hosted elsewhere. |
| 94 | res.addHeader("Content-Security-Policy", "default-src 'none'; " |
| 95 | "img-src *; " |
| 96 | "font-src *; " |
| 97 | "style-src *; " |
| 98 | "script-src *; " |
Basheer Ahmed Muddebihal | 09e7afd | 2021-03-17 00:55:57 -0700 | [diff] [blame] | 99 | "connect-src *; " |
| 100 | "form-action *; " |
| 101 | "frame-ancestors *; " |
Jiaqing Zhao | 91ac2e5 | 2022-03-17 00:18:58 +0800 | [diff] [blame] | 102 | "object-src *; " |
Basheer Ahmed Muddebihal | 09e7afd | 2021-03-17 00:55:57 -0700 | [diff] [blame] | 103 | "base-uri *"); |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 104 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 105 | std::string_view origin = req.getHeaderValue("Origin"); |
Ed Tanous | 0260d9d | 2021-02-07 19:31:07 +0000 | [diff] [blame] | 106 | res.addHeader(bf::access_control_allow_origin, origin); |
| 107 | res.addHeader(bf::access_control_allow_methods, "GET, " |
| 108 | "POST, " |
| 109 | "PUT, " |
| 110 | "PATCH, " |
| 111 | "DELETE"); |
| 112 | res.addHeader(bf::access_control_allow_credentials, "true"); |
| 113 | res.addHeader(bf::access_control_allow_headers, "Origin, " |
| 114 | "Content-Type, " |
| 115 | "Accept, " |
| 116 | "Cookie, " |
| 117 | "X-XSRF-TOKEN"); |
| 118 | } |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 119 | } |