WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit e75183d

Browse files
feat: src/helmet.cpp: implement (cppalliance#230 (comment))
Signed-off-by: Amlal El Mahrouss <[email protected]>
1 parent c6c074b commit e75183d

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

include/boost/http_proto/server/helmet.hpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,33 @@ struct helmet_options
2121
{
2222
using helmet_pair = std::pair<std::string, std::vector<std::string>>;
2323
using helmet_map = std::vector<helmet_pair>;
24+
using cached_pair = std::pair<std::string, std::string>;
25+
using cached_vector = std::vector<cached_pair>;
2426

2527
/// \brief {key, enabled}
2628
/// \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-
};
29+
struct helmet_headers_option {
30+
helmet_map headers = {
31+
{"Content-Security-Policy", {"default-src 'self'", "base-uri 'self'", "font-src 'self' https: data:", "form-action 'self'", "frame-ancestors 'self'",
32+
"img-src 'self' data:", "object-src 'none'", "script-src 'self'", "script-src-attr 'none'", "style-src 'self' https: 'unsafe-inline'", "upgrade-insecure-requests"}},
33+
{"Cross-Origin-Embedder-Policy", {"require-corp"}},
34+
{"Cross-Origin-Opener-Policy", {"same-origin"}},
35+
{"Cross-Origin-Resource-Policy", {"same-origin"}},
36+
{"X-DNS-Prefetch-Control", {"off"}},
37+
{"Expect-CT", {"max-age=86400, enforce"}},
38+
{"X-Frame-Options", {"SAMEORIGIN"}},
39+
{"X-Powered-By", {""}}, // Remove this header
40+
{"Strict-Transport-Security", {"max-age=15552000", "includeSubDomains"}},
41+
{"X-Download-Options", {"noopen"}},
42+
{"X-Content-Type-Options", {"nosniff"}},
43+
{"Origin-Agent-Cluster", {"?1"}},
44+
{"X-Permitted-Cross-Domain-Policies", {"none"}},
45+
{"Referrer-Policy", {"no-referrer"}},
46+
{"X-XSS-Protection", {"0"}} // Disabled as modern browsers have better protections
47+
};
48+
49+
cached_vector cachedHeaders;
50+
} requestHeaders;
4551
};
4652

4753
/// \brief Middleware inspired by express.js concept of helmets.

src/server/helmet.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,40 @@ namespace detail {
2929

3030
return res_value;
3131
}
32+
33+
/// \note This function caches the function back to the **hdr** (here the cachedHeaders)
34+
auto setCachedHeaderValues(std::vector<std::pair<std::string, std::string>>& hdr,
35+
const std::pair<std::string, std::string>& pair) -> void {
36+
if (pair.second.empty())
37+
detail::throw_length_error();
38+
39+
hdr.push_back(pair);
40+
}
3241
}
3342

3443
helmet::
3544
helmet(
3645
helmet_options options) noexcept
3746
: options_(options)
3847
{
48+
for (auto& hdr : options_.requestHeaders.headers)
49+
{
50+
std::string cachedResult = detail::setHeaderValues(hdr.second);
3951

52+
detail::setCachedHeaderValues(
53+
options_.requestHeaders.cachedHeaders,
54+
std::make_pair(hdr.first, cachedResult));
55+
}
4056
}
4157

4258
route_result
4359
helmet::
4460
operator()(
4561
route_params& p) const
4662
{
47-
for (const auto& hdr : options_.requestHeaders)
63+
for (const auto& hdr : options_.requestHeaders.cachedHeaders)
4864
{
49-
p.res.set(hdr.first, detail::setHeaderValues(hdr.second));
65+
p.res.set(hdr.first, hdr.second);
5066
}
5167

5268
return route::next;

0 commit comments

Comments
 (0)