security_headers

Security headers middleware.

Adds standard security headers to every HTTP response:

The nonce is produced by crate::web::nonce::attach_nonce and read from the request extensions before the inner handler runs, so it is guaranteed to match the nonce the nonce middleware later stamps into the HTML body.

const PERMISSIONS_POLICY

source

const PERMISSIONS_POLICY: &str = "accelerometer=(), camera=(), clipboard-read=(), clipboard-write=(), display-capture=(), fullscreen=(), geolocation=(), gyroscope=(), hid=(), interest-cohort=(), magnetometer=(), microphone=(), midi=(), payment=(), publickey-credentials-get=(), screen-wake-lock=(), serial=(), usb=(), xr-spatial-tracking=()";

Permissions-Policy value denying every capability the dashboard does not use.

Browsers treat an empty allowlist (()) as “no origin can use this feature”, which is what we want for a purely read/edit oriented dashboard.

fn add_security_headers

source

pub async fn add_security_headers(request: Request, next: Next) -> Response

Middleware that appends security headers to every response.

The Content-Security-Policy is built per request so it can reference the nonce stored in the request extensions by crate::web::nonce::attach_nonce. If the nonce middleware is missing the CSP falls back to 'self' only, which is safer than silently accepting inline content.

Parameter Type Description
request Request the incoming request, whose extensions may hold a [CspNonce]
next Next the rest of the middleware stack and the handler
returns Response The handler’s response with the security headers set, replacing any of the same name.

fn build_csp_header

source

fn build_csp_header(nonce: Option<&str>) -> String

Build the Content-Security-Policy header value for a request.

When a per-request nonce is available, inline <script> tags are authorized via 'nonce-<value>'. style-src always includes 'unsafe-inline' because Mermaid.js injects <style> elements at render time. The jsDelivr CDN is allowed in script-src so the browser can load the Mermaid library.

Parameter Type Description
nonce Option<&str> the per-request nonce, or None when the nonce middleware did not run
returns String The header value, with directives separated by ; .