1+ //
2+ // Copyright (c) 2025 Amlal El Mahrouss (amlal at nekernel dot org)
3+ //
4+ // Distributed under the Boost Software License, Version 1.0. (See accompanying
5+ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+ //
7+ // Official repository: https://github.com/cppalliance/http_proto
8+ //
9+
10+ #ifndef BOOST_HTTP_PROTO_SERVER_HELMET_HPP
11+ #define BOOST_HTTP_PROTO_SERVER_HELMET_HPP
12+
13+ #include < boost/http_proto/detail/config.hpp>
14+ #include < boost/http_proto/server/route_handler.hpp>
15+
16+ namespace boost {
17+ namespace http_proto {
18+
19+ // / \brief Helmet middleware options.
20+ struct helmet_options
21+ {
22+ using helmet_pair = std::pair<std::string, std::vector<std::string>>;
23+ using helmet_map = std::vector<helmet_pair>;
24+
25+ // / \brief {key, enabled}
26+ // / \note i.e {bad-header, ""} <-- disabled
27+ struct helmet_headers_option {
28+ helmet_map headers = {
29+ {" Content-Security-Policy" , {" default-src 'self'" , " base-uri 'self'" , " font-src 'self' https: data:" , " form-action 'self'" , " frame-ancestors 'self'" ,
30+ " img-src 'self' data:" , " object-src 'none'" , " script-src 'self'" , " script-src-attr 'none'" , " style-src 'self' https: 'unsafe-inline'" , " upgrade-insecure-requests" }},
31+ {" Cross-Origin-Embedder-Policy" , {" require-corp" }},
32+ {" Cross-Origin-Opener-Policy" , {" same-origin" }},
33+ {" Cross-Origin-Resource-Policy" , {" same-origin" }},
34+ {" X-DNS-Prefetch-Control" , {" off" }},
35+ {" Expect-CT" , {" max-age=86400, enforce" }},
36+ {" X-Frame-Options" , {" SAMEORIGIN" }},
37+ {" X-Powered-By" , {" " }}, // Remove this header
38+ {" Strict-Transport-Security" , {" max-age=15552000" , " includeSubDomains" }},
39+ {" X-Download-Options" , {" noopen" }},
40+ {" X-Content-Type-Options" , {" nosniff" }},
41+ {" Origin-Agent-Cluster" , {" ?1" }},
42+ {" X-Permitted-Cross-Domain-Policies" , {" none" }},
43+ {" Referrer-Policy" , {" no-referrer" }},
44+ {" X-XSS-Protection" , {" 0" }} // Disabled as modern browsers have better protections
45+ };
46+ } requestHeaders;
47+ };
48+
49+ // / \brief Middleware inspired by express.js concept of helmets.
50+ class helmet
51+ {
52+ struct impl ;
53+ std::unique_ptr<impl> impl_;
54+
55+ public:
56+ // / \brief Builds an helmet and compute its options for caching purposes.
57+ BOOST_HTTP_PROTO_DECL
58+ explicit helmet (
59+ helmet_options options = {}) noexcept ;
60+
61+ // / \brief Iterates over cachedHeaders and apply its rules to the response params.
62+ // / \param p route parameter argument
63+ // / \return route_result an error_code signaling the route's status.
64+ BOOST_HTTP_PROTO_DECL
65+ route_result
66+ operator ()(route_params& p) const ;
67+
68+ private:
69+ helmet_options options_;
70+ };
71+ }
72+
73+ }
74+ #endif
0 commit comments