Expand description
CORS handling, ported from the reference response-side CORS surface.
The engine owns the verdict; adapters (and the conformance runner)
apply the returned map to their responses, composing on top of the
security-header set exactly like apply_cors_headers. Reference
sources: get_cors_headers (_security_headers_cors.py), the config
resolution (_compute_cors_config), and the SecurityConfig CORS
field defaults.
The wildcard + credentials misconfiguration is NOT rejected: the
reference _compute_cors_config logs an error and drops the
credentials flag at resolution time, so the wildcard policy answers
without the allow-credentials header (the browser blocks credentialed
CORS), and a disallowed origin simply gets no CORS headers (the browser
enforces).
§Example
use guard_core_engine::cors::{CorsConfig, cors_response_headers};
let cors = CorsConfig {
enabled: true,
allow_origins: vec!["https://app.example.com".to_owned()],
..CorsConfig::default()
};
let headers = cors_response_headers(&cors, Some("https://app.example.com"));
assert_eq!(
headers.get("Access-Control-Allow-Origin").map(String::as_str),
Some("https://app.example.com")
);
assert_eq!(
headers.get("Access-Control-Allow-Methods").map(String::as_str),
Some("GET, POST, PUT, PATCH, DELETE, OPTIONS")
);
// A disallowed origin gets nothing (the browser enforces).
assert!(cors_response_headers(&cors, Some("https://evil.example.com")).is_empty());Structs§
- Cors
Config - The resolved CORS configuration:
_compute_cors_config’s returned state, with theSecurityConfigCORS field defaults. - Cors
Preflight Response - The reference
CorsPreflightResponse: the status, the header set, and the plain-text body the preflight answer renders. - Preflight
Request - The reference
build_preflight_response’s request view: the three headers the preflight validation reads (each already extracted by the adapter from its own header map).
Constants§
- ALLOWED_
PREFLIGHT_ REQUEST_ HEADER - The preflight request header the reference gates an OPTIONS request on
(
ALLOWED_PREFLIGHT_REQUEST_HEADER).
Functions§
- build_
preflight_ response - The reference
build_preflight_response(cors_handler.py). - cors_
response_ headers get_cors_headerscomposed with_build_cors_headers.- downgrade_
wildcard_ credentials _compute_cors_config’s wildcard + credentials downgrade, in place.- is_
origin_ allowed _is_origin_allowed: the wildcard or exact membership.- is_
preflight - The reference
is_preflight: anOPTIONSmethod (case-insensitive) carrying theaccess-control-request-methodheader.