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+ helmet_map requestHeaders = {
28+ {" Content-Security-Policy" , {" default-src 'self'" , " base-uri 'self'" , " font-src 'self' https: data:" , " form-action 'self'" , " frame-ancestors 'self'" ,
29+ " img-src 'self' data:" , " object-src 'none'" , " script-src 'self'" , " script-src-attr 'none'" , " style-src 'self' https: 'unsafe-inline'" , " upgrade-insecure-requests" }},
30+ {" Cross-Origin-Embedder-Policy" , {" require-corp" }},
31+ {" Cross-Origin-Opener-Policy" , {" same-origin" }},
32+ {" Cross-Origin-Resource-Policy" , {" same-origin" }},
33+ {" X-DNS-Prefetch-Control" , {" off" }},
34+ {" Expect-CT" , {" max-age=86400, enforce" }},
35+ {" X-Frame-Options" , {" SAMEORIGIN" }},
36+ {" X-Powered-By" , {" " }}, // Remove this header
37+ {" Strict-Transport-Security" , {" max-age=15552000" , " includeSubDomains" }},
38+ {" X-Download-Options" , {" noopen" }},
39+ {" X-Content-Type-Options" , {" nosniff" }},
40+ {" Origin-Agent-Cluster" , {" ?1" }},
41+ {" X-Permitted-Cross-Domain-Policies" , {" none" }},
42+ {" Referrer-Policy" , {" no-referrer" }},
43+ {" X-XSS-Protection" , {" 0" }} // Disabled as modern browsers have better protections
44+ };
45+ };
46+
47+ // / \brief Middleware inspired by express.js concept of helmets.
48+ class helmet
49+ {
50+ struct impl ;
51+ std::unique_ptr<impl> impl_;
52+
53+ public:
54+ // / \brief Builds an helmet and compute its options for caching purposes.
55+ BOOST_HTTP_PROTO_DECL
56+ explicit helmet (
57+ helmet_options options = {}) noexcept ;
58+
59+ // / \brief Iterates over cachedHeaders and apply its rules to the response params.
60+ // / \param p route parameter argument
61+ // / \return route_result an error_code signaling the route's status.
62+ BOOST_HTTP_PROTO_DECL
63+ route_result
64+ operator ()(route_params& p) const ;
65+
66+ private:
67+ helmet_options options_;
68+ };
69+ }
70+
71+ }
72+ #endif
0 commit comments