Skip to content

Commit 8e0e20b

Browse files
feat: helmet: add allow methods, drop experiment with hsts.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
1 parent 6e2d34b commit 8e0e20b

File tree

3 files changed

+25
-132
lines changed

3 files changed

+25
-132
lines changed

include/boost/http_proto/server/helmet.hpp

Lines changed: 3 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ class helmet
290290
@return A reference to this object for chaining.
291291
*/
292292
BOOST_HTTP_PROTO_DECL
293-
csp_policy& set(const core::string_view& allow,
293+
csp_policy& allow(const core::string_view& allow,
294294
const csp_type& type);
295295

296296
/** Remove a CSP directive.
@@ -314,161 +314,37 @@ class helmet
314314
@throws std::invalid_argument if parameters are empty.
315315
*/
316316
BOOST_HTTP_PROTO_DECL
317-
csp_policy& set(const core::string_view& allow,
317+
csp_policy& allow(const core::string_view& allow,
318318
const urls::url_view& source);
319319
};
320320
};
321321

322-
/** Return X-Download-Options header configuration.
323-
324-
Controls file download behavior in Internet Explorer 8+.
325-
326-
@param type The download restriction type.
327-
328-
@return Header name and value pair.
329-
*/
330322
option_pair x_download_options(const helmet_download_type& type);
331323

332-
/** Return X-Frame-Options header configuration.
333-
334-
Prevents clickjacking attacks by controlling frame embedding.
335-
336-
@param origin The frame embedding policy.
337-
338-
@return Header name and value pair.
339-
*/
340324
option_pair x_frame_origin(const helmet_origin_type& origin);
341325

342-
/** Return X-XSS-Protection header configuration.
343-
344-
Disables legacy XSS filtering which can create vulnerabilities.
345-
Modern browsers rely on CSP instead.
346-
347-
@return Header name and value pair (always "0").
348-
*/
349326
option_pair x_xss_protection();
350327

351-
/** Return X-Content-Type-Options header configuration.
352-
353-
Prevents MIME-type sniffing attacks.
354-
355-
@return Header name and value pair (always "nosniff").
356-
*/
357328
option_pair x_content_type_options();
358329

359-
/** Return Content-Security-Policy header configuration.
360-
361-
Configures CSP to prevent XSS and data injection attacks.
362-
363-
@param sp The CSP policy with directives.
364-
365-
@return Header name and value pair.
366-
367-
@throws std::invalid_argument if the policy is empty.
368-
*/
369330
option_pair content_security_policy(const helmet::csp_policy& sp);
370331

371-
/** Return Strict-Transport-Security header configuration.
372-
373-
Enforces HTTPS connections to prevent downgrade attacks.
374-
375-
@param options HSTS configuration parameters.
376-
377-
@return Header name and value pair.
378-
*/
379-
template <const std::size_t Age, const bool IncludeDomains = true, const bool Preload = true>
380-
inline option_pair strict_transport_security()
381-
{
382-
std::string value = "max-age=" + std::to_string(Age);
383-
384-
if constexpr (IncludeDomains)
385-
{
386-
value += "; includeSubDomains";
387-
}
388-
389-
if constexpr (Preload)
390-
{
391-
value += "; preload";
392-
}
393-
394-
return {"Strict-Transport-Security", {value}};
395-
}
396-
397-
/** Return Cross-Origin-Opener-Policy header configuration.
398-
399-
Controls cross-origin window isolation.
400-
401-
@param policy The COOP policy type.
332+
option_pair strict_transport_security(const std::size_t age, const bool include_domains = true, const bool preload = false);
402333

403-
@return Header name and value pair.
404-
*/
405334
option_pair cross_origin_opener_policy(const coop_policy_type& policy = coop_policy_type::same_origin);
406335

407-
/** Return Cross-Origin-Resource-Policy header configuration.
408-
409-
Controls cross-origin resource sharing.
410-
411-
@param policy The CORP policy type.
412-
413-
@return Header name and value pair.
414-
*/
415336
option_pair cross_origin_resource_policy(const corp_policy_type& policy = corp_policy_type::same_origin);
416337

417-
/** Return Cross-Origin-Embedder-Policy header configuration.
418-
419-
Controls embedding of cross-origin resources.
420-
421-
@param policy The COEP policy type.
422-
423-
@return Header name and value pair.
424-
*/
425338
option_pair cross_origin_embedder_policy(const coep_policy_type& policy = coep_policy_type::require_corp);
426339

427-
/** Return Referrer-Policy header configuration.
428-
429-
Controls how much referrer information is sent with requests.
430-
431-
@param policy The referrer policy type.
432-
433-
@return Header name and value pair.
434-
*/
435340
option_pair referrer_policy(const referrer_policy_type& policy = referrer_policy_type::no_referrer);
436341

437-
/** Return Origin-Agent-Cluster header configuration.
438-
439-
Requests origin-keyed agent clusters for better isolation.
440-
441-
@return Header name and value pair (always "?1").
442-
*/
443342
option_pair origin_agent_cluster();
444343

445-
/** Return X-DNS-Prefetch-Control header configuration.
446-
447-
Controls DNS prefetching to balance performance and privacy.
448-
449-
@param allow Whether to enable DNS prefetching.
450-
451-
@return Header name and value pair.
452-
*/
453344
option_pair dns_prefetch_control(bool allow = false);
454345

455-
/** Return X-Permitted-Cross-Domain-Policies header configuration.
456-
457-
Controls cross-domain policy files for Flash and Adobe Acrobat.
458-
459-
@param policy The cross-domain policy type.
460-
461-
@return Header name and value pair.
462-
*/
463346
option_pair permitted_cross_domain_policies(const cross_domain_policy_type& policy = cross_domain_policy_type::none);
464347

465-
/** Return configuration to remove X-Powered-By header.
466-
467-
Removes the X-Powered-By header to avoid revealing
468-
server technology details.
469-
470-
@return Header name with empty value (triggers removal).
471-
*/
472348
option_pair hide_powered_by();
473349

474350
}

src/server/helmet.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ helmet_options::helmet_options()
4242
this->set(x_frame_origin(helmet_origin_type::deny));
4343
this->set(x_xss_protection());
4444
this->set(x_content_type_options());
45-
this->set(strict_transport_security<hsts::default_age, hsts::include_subdomains, hsts::preload>());
45+
this->set(strict_transport_security(hsts::default_age, hsts::include_subdomains, hsts::preload));
4646
this->set(cross_origin_opener_policy());
4747
this->set(cross_origin_resource_policy());
4848
this->set(origin_agent_cluster());
@@ -143,7 +143,7 @@ operator()(
143143
return route::next;
144144
}
145145

146-
helmet::csp_policy& helmet::csp_policy::set(const core::string_view& allow,
146+
helmet::csp_policy& helmet::csp_policy::allow(const core::string_view& allow,
147147
const urls::url_view& source)
148148
{
149149
if (!source.scheme().starts_with("http"))
@@ -176,7 +176,7 @@ helmet::csp_policy& helmet::csp_policy::set(const core::string_view& allow,
176176
return *this;
177177
}
178178

179-
helmet::csp_policy& helmet::csp_policy::set(const core::string_view& allow, const csp_type& type)
179+
helmet::csp_policy& helmet::csp_policy::allow(const core::string_view& allow, const csp_type& type)
180180
{
181181
if (allow.empty())
182182
detail::throw_invalid_argument();
@@ -373,6 +373,23 @@ option_pair cross_origin_embedder_policy(const coep_policy_type& policy)
373373
return {"Cross-Origin-Embedder-Policy", {value}};
374374
}
375375

376+
option_pair strict_transport_security(const std::size_t age, const bool include_domains, const bool preload)
377+
{
378+
std::string value = "max-age=" + std::to_string(age);
379+
380+
if (include_domains)
381+
{
382+
value += "; includeSubDomains";
383+
}
384+
385+
if (preload)
386+
{
387+
value += "; preload";
388+
}
389+
390+
return {"Strict-Transport-Security", {value}};
391+
}
392+
376393
option_pair referrer_policy(const referrer_policy_type& policy)
377394
{
378395
std::string value;

test/unit/server/helmet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct helmet_test
9797
{
9898
helmet_options opt;
9999

100-
opt.set(strict_transport_security<hsts::default_age>());
100+
opt.set(strict_transport_security(hsts::default_age));
101101

102102
helmet helmet{opt};
103103
route_params p;
@@ -112,7 +112,7 @@ struct helmet_test
112112
{
113113
helmet_options opt;
114114

115-
opt.set(strict_transport_security<86400, hsts::include_subdomains, hsts::preload>());
115+
opt.set(strict_transport_security(86400, false, false));
116116

117117
helmet helmet{opt};
118118
route_params p;

0 commit comments

Comments
 (0)