blob: 2a2eb40d7df355da76a7382bb6d714e47070dcff [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{
Ed Tanous52cc1122020-07-18 13:51:21 -070011 using bf = boost::beast::http::field;
Ed Tanous52cc1122020-07-18 13:51:21 -070012
Joseph Reynolds1d9502b2023-05-12 10:47:30 -050013 // Recommendations from https://owasp.org/www-project-secure-headers/
14 // https://owasp.org/www-project-secure-headers/ci/headers_add.json
Ed Tanousd8139c62023-06-14 17:11:47 -070015 res.addHeader(bf::strict_transport_security, "max-age=31536000; "
16 "includeSubdomains");
Ed Tanousd8139c62023-06-14 17:11:47 -070017
18 res.addHeader(bf::pragma, "no-cache");
Ed Tanousd8139c62023-06-14 17:11:47 -070019
Ed Tanous499b5b42024-04-06 08:39:18 -070020 if (res.getHeaderValue(bf::cache_control).empty())
21 {
22 res.addHeader(bf::cache_control, "no-store, max-age=0");
23 }
Ed Tanousd8139c62023-06-14 17:11:47 -070024 res.addHeader("X-Content-Type-Options", "nosniff");
25
Ed Tanousc056aa72024-04-14 09:57:09 -070026 std::string_view contentType = res.getHeaderValue("Content-Type");
27 if (contentType.starts_with("text/html"))
Ed Tanous0260d9d2021-02-07 19:31:07 +000028 {
Ed Tanousc056aa72024-04-14 09:57:09 -070029 res.addHeader(bf::x_frame_options, "DENY");
30 res.addHeader("Referrer-Policy", "no-referrer");
31 res.addHeader("Permissions-Policy", "accelerometer=(),"
32 "ambient-light-sensor=(),"
33 "autoplay=(),"
34 "battery=(),"
35 "camera=(),"
36 "display-capture=(),"
37 "document-domain=(),"
38 "encrypted-media=(),"
39 "fullscreen=(),"
40 "gamepad=(),"
41 "geolocation=(),"
42 "gyroscope=(),"
43 "layout-animations=(self),"
44 "legacy-image-formats=(self),"
45 "magnetometer=(),"
46 "microphone=(),"
47 "midi=(),"
48 "oversized-images=(self),"
49 "payment=(),"
50 "picture-in-picture=(),"
51 "publickey-credentials-get=(),"
52 "speaker-selection=(),"
53 "sync-xhr=(self),"
54 "unoptimized-images=(self),"
55 "unsized-media=(self),"
56 "usb=(),"
57 "screen-wak-lock=(),"
58 "web-share=(),"
59 "xr-spatial-tracking=()");
60 res.addHeader("X-Permitted-Cross-Domain-Policies", "none");
61 res.addHeader("Cross-Origin-Embedder-Policy", "require-corp");
62 res.addHeader("Cross-Origin-Opener-Policy", "same-origin");
63 res.addHeader("Cross-Origin-Resource-Policy", "same-origin");
Ed Tanous788fe742024-04-22 12:41:06 -070064 res.addHeader("Content-Security-Policy", "default-src 'none'; "
65 "img-src 'self' data:; "
66 "font-src 'self'; "
67 "style-src 'self'; "
68 "script-src 'self'; "
69 "connect-src 'self' wss:; "
70 "form-action 'none'; "
71 "frame-ancestors 'none'; "
72 "object-src 'none'; "
73 "base-uri 'none' ");
74 // The KVM currently needs to load images from base64 encoded
75 // strings. img-src 'self' data: is used to allow that.
76 // https://stackoverflow.com/questions/18447970/content-security-polic
77 // y-data-not-working-for-base64-images-in-chrome-28
Ed Tanous0260d9d2021-02-07 19:31:07 +000078 }
Ed Tanous52cc1122020-07-18 13:51:21 -070079}