Expand description
Security headers management, ported from the reference handler.
The engine owns the logic; adapters (and the conformance runner) apply
the returned map to their responses. security_headers ports
get_headers resolved against a security_headers dict: a disabled
configuration yields no headers, the class defaults apply, the
configured overrides replace the matching class header, the CSP and
HSTS blocks extend them, and the custom headers land last.
Reference sources: security_headers_handler.py, its config mixin
(_security_headers_config.py), and the field defaults
(_security_config_fields.py).
§Example
use guard_core_engine::security_headers::{security_headers, SecurityHeadersConfig};
// The reference default block: the class defaults plus the
// max-age=31536000; includeSubDomains HSTS header.
let headers = security_headers(&SecurityHeadersConfig::reference_default());
assert_eq!(headers.get("X-Content-Type-Options").map(String::as_str), Some("nosniff"));
assert_eq!(
headers.get("Strict-Transport-Security").map(String::as_str),
Some("max-age=31536000; includeSubDomains")
);
// Disabled: no headers at all (the reference `get_headers` early return).
let off = SecurityHeadersConfig {
enabled: false,
..SecurityHeadersConfig::default()
};
assert!(security_headers(&off).is_empty());Structs§
- CspDirective
- One Content-Security-Policy directive with its sources, in the insertion order the reference dict keeps (a Vec keeps the built header deterministic).
- Hsts
Config - The reference
security_headers["hsts"]block:max_age,include_subdomains,preload. - Security
Headers Config - The reference
security_headersdict:enabled,hsts,csp,frame_options,content_type_options,xss_protection,referrer_policy,permissions_policy, andcustom.
Enums§
- Security
Headers Error - A rejected security-headers configuration: the reference raises out of
configure(), so a deployment never starts with these values.
Constants§
- CLASS_
DEFAULT_ HEADERS SecurityHeadersManager.default_headers, in table order.
Functions§
- build_
csp _build_csp: “directive source1 source2” joined with “; “, bare directives kept when they carry no sources.- build_
hsts _build_hstsplus the_compute_hsts_configpreload corrections.- is_
valid_ header_ name _HEADER_NAME_TOKEN_RE: a non-empty RFC 7230 token.- security_
headers SecurityHeadersManager.get_headersresolved against the referencesecurity_headersdict.- validate_
header_ value _validate_header_value: no CR/LF, at most 8192 bytes, control characters below 0x20 dropped except tab.