blob: ec72cbb9eb4b689a29acc2c64814ef0840d7bcaf [file] [log] [blame]
Ed Tanous8041f312017-04-03 09:47:01 -07001#pragma once
2
Ed Tanousc94ad492019-10-10 15:39:33 -07003#include <http_request.h>
4#include <http_response.h>
Ed Tanous8041f312017-04-03 09:47:01 -07005
Ed Tanous1abe55e2018-09-05 08:30:59 -07006namespace crow
7{
Ed Tanous1abe55e2018-09-05 08:30:59 -07008struct SecurityHeadersMiddleware
9{
10 struct Context
Gunnar Mills1214b7e2020-06-04 10:11:30 -050011 {};
Ed Tanous8041f312017-04-03 09:47:01 -070012
Ed Tanous1abe55e2018-09-05 08:30:59 -070013 void beforeHandle(crow::Request& req, Response& res, Context& ctx)
14 {
Ed Tanousfd828ba2018-08-09 10:58:08 -070015#ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
Ed Tanous1abe55e2018-09-05 08:30:59 -070016 if ("OPTIONS"_method == req.method())
17 {
18 res.end();
19 }
20#endif
Ed Tanousfd828ba2018-08-09 10:58:08 -070021 }
Ed Tanous8041f312017-04-03 09:47:01 -070022
Ed Tanous1abe55e2018-09-05 08:30:59 -070023 void afterHandle(Request& req, Response& res, Context& ctx)
24 {
25 /*
26 TODO(ed) these should really check content types. for example,
27 X-UA-Compatible header doesn't make sense when retrieving a JSON or
28 javascript file. It doesn't hurt anything, it's just ugly.
29 */
Ed Tanous750ceae2018-11-30 15:02:22 -080030 using bf = boost::beast::http::field;
31 res.addHeader(bf::strict_transport_security, "max-age=31536000; "
32 "includeSubdomains; "
33 "preload");
34 res.addHeader(bf::x_frame_options, "DENY");
35
36 res.addHeader(bf::pragma, "no-cache");
37 res.addHeader(bf::cache_control, "no-Store,no-Cache");
Ed Tanous3eb2f352018-12-20 12:30:45 -080038
Ed Tanousa3268f92019-09-23 14:06:03 -070039 res.addHeader("X-XSS-Protection", "1; "
40 "mode=block");
41 res.addHeader("X-Content-Type-Options", "nosniff");
42
43#ifndef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
Ed Tanouse6de21a2019-08-21 12:50:42 -070044 res.addHeader("Content-Security-Policy", "default-src 'none'; "
45 "img-src 'self' data:; "
46 "font-src 'self'; "
47 "style-src 'self'; "
48 "script-src 'self'; "
49 "connect-src 'self' wss:");
50 // The KVM currently needs to load images from base64 encoded
51 // strings. img-src 'self' data: is used to allow that.
Ed Tanous3eb2f352018-12-20 12:30:45 -080052 // https://stackoverflow.com/questions/18447970/content-security-policy-data-not-working-for-base64-images-in-chrome-28
Ed Tanouse6de21a2019-08-21 12:50:42 -070053
Ed Tanousa3268f92019-09-23 14:06:03 -070054#else
55 // If XSS is disabled, we need to allow loading from addresses other
56 // than self, as the BMC will be hosted elsewhere.
57 res.addHeader("Content-Security-Policy", "default-src 'none'; "
58 "img-src *; "
59 "font-src *; "
60 "style-src *; "
61 "script-src *; "
62 "connect-src *");
Ed Tanousfd828ba2018-08-09 10:58:08 -070063
Ed Tanousa3268f92019-09-23 14:06:03 -070064 const std::string_view origin = req.getHeaderValue("Origin");
65 res.addHeader(bf::access_control_allow_origin, origin);
Ed Tanous750ceae2018-11-30 15:02:22 -080066 res.addHeader(bf::access_control_allow_methods, "GET, "
67 "POST, "
68 "PUT, "
Ed Tanousda7f41e2018-12-10 13:36:40 -080069 "PATCH, "
70 "DELETE");
Ed Tanous750ceae2018-11-30 15:02:22 -080071 res.addHeader(bf::access_control_allow_credentials, "true");
72 res.addHeader(bf::access_control_allow_headers, "Origin, "
73 "Content-Type, "
74 "Accept, "
75 "Cookie, "
76 "X-XSRF-TOKEN");
Ed Tanousfd828ba2018-08-09 10:58:08 -070077
78#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 }
Ed Tanous8041f312017-04-03 09:47:01 -070080};
Ed Tanous1abe55e2018-09-05 08:30:59 -070081} // namespace crow