1 #pragma once
2 
3 #include "bmcweb_config.h"
4 
5 #include "http_request.hpp"
6 #include "http_response.hpp"
7 
8 inline void addSecurityHeaders(crow::Response& res)
9 {
10     using bf = boost::beast::http::field;
11 
12     // Recommendations from https://owasp.org/www-project-secure-headers/
13     // https://owasp.org/www-project-secure-headers/ci/headers_add.json
14     res.addHeader(bf::strict_transport_security, "max-age=31536000; "
15                                                  "includeSubdomains");
16 
17     res.addHeader(bf::pragma, "no-cache");
18 
19     if (res.getHeaderValue(bf::cache_control).empty())
20     {
21         res.addHeader(bf::cache_control, "no-store, max-age=0");
22     }
23     res.addHeader("X-Content-Type-Options", "nosniff");
24 
25     std::string_view contentType = res.getHeaderValue("Content-Type");
26     if (contentType.starts_with("text/html"))
27     {
28         res.addHeader(bf::x_frame_options, "DENY");
29         res.addHeader("Referrer-Policy", "no-referrer");
30         res.addHeader(
31             "Permissions-Policy",
32             "accelerometer=(),"
33             "ambient-light-sensor=(),"
34             "autoplay=(),"
35             "battery=(),"
36             "camera=(),"
37             "display-capture=(),"
38             "document-domain=(),"
39             "encrypted-media=(),"
40             "fullscreen=(),"
41             "gamepad=(),"
42             "geolocation=(),"
43             "gyroscope=(),"
44             "layout-animations=(self),"
45             "legacy-image-formats=(self),"
46             "magnetometer=(),"
47             "microphone=(),"
48             "midi=(),"
49             "oversized-images=(self),"
50             "payment=(),"
51             "picture-in-picture=(),"
52             "publickey-credentials-get=(),"
53             "speaker-selection=(),"
54             "sync-xhr=(self),"
55             "unoptimized-images=(self),"
56             "unsized-media=(self),"
57             "usb=(),"
58             "screen-wak-lock=(),"
59             "web-share=(),"
60             "xr-spatial-tracking=()");
61         res.addHeader("X-Permitted-Cross-Domain-Policies", "none");
62         res.addHeader("Cross-Origin-Embedder-Policy", "require-corp");
63         res.addHeader("Cross-Origin-Opener-Policy", "same-origin");
64         res.addHeader("Cross-Origin-Resource-Policy", "same-origin");
65         res.addHeader("Content-Security-Policy",
66                       "default-src 'none'; "
67                       "img-src 'self' data:; "
68                       "font-src 'self'; "
69                       "style-src 'self'; "
70                       "script-src 'self'; "
71                       "connect-src 'self' wss:; "
72                       "form-action 'none'; "
73                       "frame-ancestors 'none'; "
74                       "object-src 'none'; "
75                       "base-uri 'none' ");
76         // The KVM currently needs to load images from base64 encoded
77         // strings. img-src 'self' data: is used to allow that.
78         // https://stackoverflow.com/questions/18447970/content-security-polic
79         // y-data-not-working-for-base64-images-in-chrome-28
80     }
81 }
82