blob: 872f4aa65382976de592730b29118d7f675e0837 [file] [log] [blame]
Ed Tanous8041f312017-04-03 09:47:01 -07001#pragma once
2
3#include <crow/http_request.h>
4#include <crow/http_response.h>
5
Ed Tanous1abe55e2018-09-05 08:30:59 -07006namespace crow
7{
Ed Tanous1abe55e2018-09-05 08:30:59 -07008struct SecurityHeadersMiddleware
9{
10 struct Context
11 {
12 };
Ed Tanous8041f312017-04-03 09:47:01 -070013
Ed Tanous1abe55e2018-09-05 08:30:59 -070014 void beforeHandle(crow::Request& req, Response& res, Context& ctx)
15 {
Ed Tanousfd828ba2018-08-09 10:58:08 -070016#ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
Ed Tanous1abe55e2018-09-05 08:30:59 -070017 if ("OPTIONS"_method == req.method())
18 {
19 res.end();
20 }
21#endif
Ed Tanousfd828ba2018-08-09 10:58:08 -070022 }
Ed Tanous8041f312017-04-03 09:47:01 -070023
Ed Tanous1abe55e2018-09-05 08:30:59 -070024 void afterHandle(Request& req, Response& res, Context& ctx)
25 {
26 /*
27 TODO(ed) these should really check content types. for example,
28 X-UA-Compatible header doesn't make sense when retrieving a JSON or
29 javascript file. It doesn't hurt anything, it's just ugly.
30 */
Ed Tanous750ceae2018-11-30 15:02:22 -080031 using bf = boost::beast::http::field;
32 res.addHeader(bf::strict_transport_security, "max-age=31536000; "
33 "includeSubdomains; "
34 "preload");
35 res.addHeader(bf::x_frame_options, "DENY");
36
37 res.addHeader(bf::pragma, "no-cache");
38 res.addHeader(bf::cache_control, "no-Store,no-Cache");
39 res.addHeader("X-Content-Security-Policy", "default-src 'self'");
40 res.addHeader("X-XSS-Protection", "1; "
41 "mode=block");
42 res.addHeader("X-UA-Compatible", "IE=11");
Ed Tanousfd828ba2018-08-09 10:58:08 -070043
44#ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
45
Ed Tanous750ceae2018-11-30 15:02:22 -080046 res.addHeader(bf::access_control_allow_origin, "http://localhost:8080");
47 res.addHeader(bf::access_control_allow_methods, "GET, "
48 "POST, "
49 "PUT, "
Ed Tanousda7f41e2018-12-10 13:36:40 -080050 "PATCH, "
51 "DELETE");
Ed Tanous750ceae2018-11-30 15:02:22 -080052 res.addHeader(bf::access_control_allow_credentials, "true");
53 res.addHeader(bf::access_control_allow_headers, "Origin, "
54 "Content-Type, "
55 "Accept, "
56 "Cookie, "
57 "X-XSRF-TOKEN");
Ed Tanousfd828ba2018-08-09 10:58:08 -070058
59#endif
Ed Tanous1abe55e2018-09-05 08:30:59 -070060 }
Ed Tanous8041f312017-04-03 09:47:01 -070061};
Ed Tanous1abe55e2018-09-05 08:30:59 -070062} // namespace crow