blob: d99729f420c18941052b574108c4bf4ac02a48d8 [file] [log] [blame]
Ed Tanous52cc1122020-07-18 13:51:21 -07001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "bmcweb_config.h"
Ed Tanous2205bbf2021-06-17 13:33:47 -07004
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "http_request.hpp"
6#include "http_response.hpp"
Ed Tanous52cc1122020-07-18 13:51:21 -07007
Ed Tanous0260d9d2021-02-07 19:31:07 +00008inline void addSecurityHeaders(const crow::Request& req [[maybe_unused]],
9 crow::Response& res)
Ed Tanous52cc1122020-07-18 13:51:21 -070010{
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 Reynolds1d9502b2023-05-12 10:47:30 -050029 // 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 Tanouse662eae2022-01-25 10:39:19 -080073 if (bmcwebInsecureDisableXssPrevention == 0)
Ed Tanous0260d9d2021-02-07 19:31:07 +000074 {
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 Muddebihal09e7afd2021-03-17 00:55:57 -070080 "connect-src 'self' wss:; "
81 "form-action 'none'; "
82 "frame-ancestors 'none'; "
Jiaqing Zhao91ac2e52022-03-17 00:18:58 +080083 "object-src 'none'; "
Basheer Ahmed Muddebihal09e7afd2021-03-17 00:55:57 -070084 "base-uri 'none' ");
Ed Tanous0260d9d2021-02-07 19:31:07 +000085 // The KVM currently needs to load images from base64 encoded
86 // strings. img-src 'self' data: is used to allow that.
Basheer Ahmed Muddebihal09e7afd2021-03-17 00:55:57 -070087 // https://stackoverflow.com/questions/18447970/content-security-polic
88 // y-data-not-working-for-base64-images-in-chrome-28
Ed Tanous0260d9d2021-02-07 19:31:07 +000089 }
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 Muddebihal09e7afd2021-03-17 00:55:57 -070099 "connect-src *; "
100 "form-action *; "
101 "frame-ancestors *; "
Jiaqing Zhao91ac2e52022-03-17 00:18:58 +0800102 "object-src *; "
Basheer Ahmed Muddebihal09e7afd2021-03-17 00:55:57 -0700103 "base-uri *");
Ed Tanous52cc1122020-07-18 13:51:21 -0700104
Ed Tanous26ccae32023-02-16 10:28:44 -0800105 std::string_view origin = req.getHeaderValue("Origin");
Ed Tanous0260d9d2021-02-07 19:31:07 +0000106 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 Tanous52cc1122020-07-18 13:51:21 -0700119}