blob: 7639637b0079383cbdfcc7ddfc188d2b7dab5250 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous52cc1122020-07-18 13:51:21 -07003#pragma once
4
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "http_response.hpp"
Ed Tanous52cc1122020-07-18 13:51:21 -07006
Ed Tanousd7857202025-01-28 15:32:26 -08007#include <boost/beast/http/field.hpp>
8
Ed Tanous89cda632024-04-16 08:45:54 -07009inline void addSecurityHeaders(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");
Patrick Williamsbd79bce2024-08-16 15:22:20 -040031 res.addHeader(
32 "Permissions-Policy",
33 "accelerometer=(),"
34 "ambient-light-sensor=(),"
35 "autoplay=(),"
36 "battery=(),"
37 "camera=(),"
38 "display-capture=(),"
39 "document-domain=(),"
40 "encrypted-media=(),"
41 "fullscreen=(),"
42 "gamepad=(),"
43 "geolocation=(),"
44 "gyroscope=(),"
45 "layout-animations=(self),"
46 "legacy-image-formats=(self),"
47 "magnetometer=(),"
48 "microphone=(),"
49 "midi=(),"
50 "oversized-images=(self),"
51 "payment=(),"
52 "picture-in-picture=(),"
53 "publickey-credentials-get=(),"
54 "speaker-selection=(),"
55 "sync-xhr=(self),"
56 "unoptimized-images=(self),"
57 "unsized-media=(self),"
58 "usb=(),"
59 "screen-wak-lock=(),"
60 "web-share=(),"
61 "xr-spatial-tracking=()");
Ed Tanousc056aa72024-04-14 09:57:09 -070062 res.addHeader("X-Permitted-Cross-Domain-Policies", "none");
63 res.addHeader("Cross-Origin-Embedder-Policy", "require-corp");
64 res.addHeader("Cross-Origin-Opener-Policy", "same-origin");
65 res.addHeader("Cross-Origin-Resource-Policy", "same-origin");
Patrick Williamsbd79bce2024-08-16 15:22:20 -040066 res.addHeader("Content-Security-Policy",
67 "default-src 'none'; "
68 "img-src 'self' data:; "
69 "font-src 'self'; "
70 "style-src 'self'; "
71 "script-src 'self'; "
72 "connect-src 'self' wss:; "
73 "form-action 'none'; "
74 "frame-ancestors 'none'; "
75 "object-src 'none'; "
76 "base-uri 'none' ");
Ed Tanous788fe742024-04-22 12:41:06 -070077 // The KVM currently needs to load images from base64 encoded
78 // strings. img-src 'self' data: is used to allow that.
79 // https://stackoverflow.com/questions/18447970/content-security-polic
80 // y-data-not-working-for-base64-images-in-chrome-28
Ed Tanous0260d9d2021-02-07 19:31:07 +000081 }
Ed Tanous52cc1122020-07-18 13:51:21 -070082}