security_headers
Adds standard security headers to every HTTP response:
Content-Security-Policy– restricts resource origins. Inline<script>tags use a per-request nonce;style-srcallows'unsafe-inline'because Mermaid.js injects<style>elements at runtime. The jsDelivr CDN is allowed inscript-srcso the browser can load the Mermaid library.Permissions-Policy– denies access to browser capabilities the dashboard does not use (camera, microphone, geolocation, etc.).Referrer-Policy: no-referrer– prevents leaking URLs to third parties when the user follows an outbound link.X-Content-Type-Options: nosniff– disables MIME-type sniffing.X-Frame-Options: DENY– blocks framing to defeat clickjacking.
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
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
pub async fn add_security_headers(request: Request, next: Next) -> ResponseMiddleware 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
fn build_csp_header(nonce: Option<&str>) -> StringBuild 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 ; . |