diff --git a/.changeset/admin-http-surface.md b/.changeset/admin-http-surface.md new file mode 100644 index 000000000..a845151cc --- /dev/null +++ b/.changeset/admin-http-surface.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/Cargo.lock b/Cargo.lock index 44bbdc173..9cab4b723 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,6 +125,58 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "axum" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90" +dependencies = [ + "axum-core", + "bytes", + "form_urlencoded", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "serde_core", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "sync_wrapper", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "base64" version = "0.22.1" @@ -137,6 +189,12 @@ version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" +[[package]] +name = "base64ct" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + [[package]] name = "bincode" version = "1.3.3" @@ -430,6 +488,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer 0.10.4", "crypto-common 0.1.6", + "subtle", ] [[package]] @@ -792,7 +851,16 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018" dependencies = [ - "hmac", + "hmac 0.13.0", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest 0.10.7", ] [[package]] @@ -843,6 +911,12 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + [[package]] name = "hybrid-array" version = "0.4.15" @@ -866,6 +940,7 @@ dependencies = [ "http", "http-body", "httparse", + "httpdate", "itoa", "pin-project-lite", "smallvec", @@ -1196,6 +1271,12 @@ dependencies = [ "regex-automata", ] +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + [[package]] name = "md-5" version = "0.11.0" @@ -1212,6 +1293,12 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "mime" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5848f5a9e8117cf94f2f90faac24a87ff81600ff8628c1e3deaab572e76bfa1" + [[package]] name = "mio" version = "1.2.4" @@ -1233,10 +1320,27 @@ checksum = "a3d66662cb5ca384d4991509755ed5a42dd8e31c4282bf881938e506fc3146ec" name = "nostream-admin" version = "0.1.0" dependencies = [ + "axum", + "base64 0.22.1", + "chrono", + "futures-util", + "hex", + "hmac 0.13.0", "nostream-config", "nostream-core", + "nostream-relay", + "nostream-services", "nostream-storage", + "scrypt", + "serde", + "serde_json", + "serde_yaml", + "sha2 0.11.1", + "tempfile", "thiserror", + "tokio", + "tracing", + "uuid", ] [[package]] @@ -1265,6 +1369,7 @@ dependencies = [ name = "nostream-config" version = "0.1.0" dependencies = [ + "chrono", "nostream-core", "serde", "serde_json", @@ -1285,7 +1390,7 @@ dependencies = [ "getrandom 0.3.4", "hex", "hkdf", - "hmac", + "hmac 0.13.0", "negentropy", "secp256k1", "serde", @@ -1304,7 +1409,7 @@ dependencies = [ "chrono", "futures-util", "hex", - "hmac", + "hmac 0.13.0", "nostream-config", "nostream-core", "nostream-storage", @@ -1324,7 +1429,7 @@ dependencies = [ "chrono", "futures-util", "hex", - "hmac", + "hmac 0.13.0", "nostream-config", "nostream-core", "nostream-relay", @@ -1368,8 +1473,12 @@ name = "nostreamd" version = "0.1.0" dependencies = [ "anyhow", + "axum", "clap", "futures-util", + "hyper", + "hyper-util", + "nostream-admin", "nostream-config", "nostream-core", "nostream-relay", @@ -1516,6 +1625,27 @@ dependencies = [ "windows-link", ] +[[package]] +name = "password-hash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166" +dependencies = [ + "base64ct", + "rand_core 0.6.4", + "subtle", +] + +[[package]] +name = "pbkdf2" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" +dependencies = [ + "digest 0.10.7", + "hmac 0.12.1", +] + [[package]] name = "percent-encoding" version = "2.3.2" @@ -1783,6 +1913,9 @@ name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom 0.2.17", +] [[package]] name = "rand_core" @@ -1952,12 +2085,33 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" +[[package]] +name = "salsa20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97a22f5af31f73a954c10289c93e8a50cc23d971e80ee446f1f6f7137a088213" +dependencies = [ + "cipher 0.4.4", +] + [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "scrypt" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0516a385866c09368f0b5bcd1caff3366aace790fcd46e2bb032697bb172fd1f" +dependencies = [ + "password-hash", + "pbkdf2", + "salsa20", + "sha2 0.10.9", +] + [[package]] name = "secp256k1" version = "0.33.1" @@ -2020,6 +2174,17 @@ dependencies = [ "zmij", ] +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -2278,7 +2443,7 @@ dependencies = [ "futures-util", "hex", "hkdf", - "hmac", + "hmac 0.13.0", "itoa", "log", "md-5", diff --git a/Cargo.toml b/Cargo.toml index 852232b2f..a3ec61dfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ repository = "https://github.com/cameri/nostream" [workspace.dependencies] aes = "0.8" anyhow = "1" +axum = "0.8" base64 = "0.22.1" cbc = { version = "0.1", features = ["alloc", "block-padding"] } chacha20 = "0.10.2" @@ -22,6 +23,8 @@ futures-util = "0.3" getrandom = "0.3" heed = { version = "0.20", features = ["read-txn-no-tls"] } hex = "0.4.3" +hyper = { version = "1", features = ["server", "http1"] } +hyper-util = { version = "0.1", features = ["tokio", "service"] } hkdf = "0.13.0" hmac = "0.13.0" negentropy = "0.5.2" @@ -37,6 +40,7 @@ opentelemetry = "0.33.1" opentelemetry-otlp = { version = "0.33.1", default-features = false, features = ["trace", "grpc-tonic"] } opentelemetry_sdk = { version = "0.33.1", default-features = false, features = ["trace", "rt-tokio"] } reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] } +scrypt = "0.11" secp256k1 = "0.33.1" serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/crates/nostream-admin/Cargo.toml b/crates/nostream-admin/Cargo.toml index 85787fb91..380263bd2 100644 --- a/crates/nostream-admin/Cargo.toml +++ b/crates/nostream-admin/Cargo.toml @@ -8,10 +8,29 @@ repository.workspace = true description = "Admin controllers, routes, middleware and authentication" [dependencies] +axum.workspace = true +base64.workspace = true +chrono.workspace = true +futures-util.workspace = true +hex.workspace = true +hmac.workspace = true nostream-config.workspace = true nostream-core.workspace = true +nostream-relay.workspace = true +nostream-services.workspace = true nostream-storage.workspace = true +scrypt.workspace = true +serde.workspace = true +serde_json.workspace = true +serde_yaml.workspace = true +sha2.workspace = true thiserror.workspace = true +tokio.workspace = true +tracing.workspace = true +uuid.workspace = true + +[dev-dependencies] +tempfile.workspace = true [lints] workspace = true diff --git a/crates/nostream-admin/src/auth.rs b/crates/nostream-admin/src/auth.rs index ca927c9f2..cf1762084 100644 --- a/crates/nostream-admin/src/auth.rs +++ b/crates/nostream-admin/src/auth.rs @@ -1,28 +1,136 @@ -//! Admin authentication. Mirrors `src/admin/password-admin-auth-provider.ts` and -//! `src/utils/admin-{password,session,rate-limit}.ts`. +//! Admin authentication. Mirrors `src/admin/password-admin-auth-provider.ts` and the +//! `src/utils/admin-{password,session}.ts` it composes. +//! +//! The provider's semantics, which are the capture's: +//! +//! - a non-empty `ADMIN_PASSWORD` wins as a constant-time plaintext compare (the daemon +//! resolves the environment into [`AuthSettings::password_override`]; the crate reads none); +//! - otherwise `admin.passwordHash` in `scrypt::` form is verified with scrypt; +//! - an absent hash is a failed login, never a `500`; +//! - a missing `SECRET` is a `500` for minting and verifying, because a token cannot be signed +//! or checked without the key. + +use nostream_config::Settings; use crate::error::AdminError; +use crate::utils::password::{ + hash_admin_password, verify_admin_password_hash, verify_plaintext_password, +}; +use crate::utils::session; + +/// The pieces of the settings and environment the provider needs. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct AuthSettings { + /// `ADMIN_PASSWORD`, when the environment carried a non-empty one. + pub password_override: Option, + /// `admin.passwordHash`. + pub password_hash: Option, + /// `admin.sessionTtlSeconds`. + pub session_ttl_seconds: Option, +} + +impl AuthSettings { + /// Reads the block out of the loaded settings. + pub fn from_settings(settings: &Settings, password_override: Option<&str>) -> Self { + let admin = settings.admin.as_ref(); + Self { + password_override: password_override + .filter(|password| !password.is_empty()) + .map(str::to_owned), + password_hash: admin + .and_then(|admin| admin.password_hash.as_ref()) + .map(|hash| hash.expose().to_owned()), + session_ttl_seconds: admin.and_then(|admin| admin.session_ttl_seconds), + } + } + + /// `verifyPassword`: the override first, then the hash. + pub fn verify_password(&self, password: &str) -> bool { + if let Some(override_password) = &self.password_override { + return verify_plaintext_password(password, override_password); + } + match &self.password_hash { + Some(hash) => verify_admin_password_hash(password, hash), + None => false, + } + } + + /// The `ADMIN_PASSWORD` override, which wins over the hash (`verifyPassword`). + pub fn password_override(&self) -> Option<&str> { + self.password_override.as_deref() + } + + /// The scrypt hash when one is configured, so the caller can run the work off-thread. + pub fn password_hash(&self) -> Option<&str> { + self.password_hash.as_deref() + } -/// The authentication provider the admin routes accept. -#[derive(Debug, Clone, Default)] -pub struct PasswordAuthProvider; + /// The resolved session lifetime in whole seconds (Value 2). + pub fn session_ttl(&self) -> u64 { + session::resolve_ttl(self.session_ttl_seconds) + } -impl PasswordAuthProvider { - /// Verifies a password against the configured hash. - /// - /// Phase 1 stub — job 005; will verify with argon2 and mint a session; see `src/admin/password-admin-auth-provider.ts`. - pub async fn verify(_password: &str, _hash: &str) -> Result<(), AdminError> { - Err(AdminError::NotImplemented { - module: "admin::auth::PasswordAuthProvider::verify", - }) + /// Mints the token for a session starting now. + pub fn mint( + &self, + secret: Option<&[u8]>, + now_seconds: i64, + ) -> Result<(String, i64), AdminError> { + let secret = secret.ok_or_else(|| { + AdminError::Internal("the admin session signer has no SECRET".to_owned()) + })?; + let expires_at = now_seconds + i64::try_from(self.session_ttl()).unwrap_or(i64::MAX); + let token = session::create_token(&session::session_key(secret), expires_at); + Ok((token, expires_at)) } } -/// Issues a session token with the configured TTL. -/// -/// Phase 1 stub — job 005; will mint and store a session; see `src/utils/admin-session.ts`. -pub fn issue_session(_ttl_seconds: u64) -> Result { - Err(AdminError::NotImplemented { - module: "admin::auth::issue_session", - }) +/// `hashAdminPassword` over a fresh salt: the operator's tool for writing +/// `admin.passwordHash`. +pub fn hash_password( + password: &str, + salt: &[u8; crate::utils::password::SALT_LENGTH], +) -> Result { + hash_admin_password(password, salt) +} + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used, clippy::expect_used)] + + use super::*; + + const SALT: [u8; 16] = [3u8; 16]; + + #[test] + fn the_override_wins_over_the_hash() { + let settings = AuthSettings { + password_override: Some("plain".to_owned()), + password_hash: Some(hash_password("hashed", &SALT).unwrap()), + session_ttl_seconds: None, + }; + assert!(settings.verify_password("plain")); + assert!(!settings.verify_password("hashed")); + } + + #[test] + fn an_empty_override_falls_through_to_the_hash() { + let settings = AuthSettings { + password_override: None, + password_hash: Some(hash_password("hashed", &SALT).unwrap()), + session_ttl_seconds: Some(60), + }; + assert!(settings.verify_password("hashed")); + assert_eq!(settings.session_ttl(), 60); + } + + #[test] + fn an_absent_hash_is_a_failed_login() { + let settings = AuthSettings::default(); + assert!(!settings.verify_password("anything")); + assert_eq!( + settings.session_ttl(), + session::DEFAULT_ADMIN_SESSION_TTL_SECONDS + ); + } } diff --git a/crates/nostream-admin/src/controllers/admin_auth_middleware.rs b/crates/nostream-admin/src/controllers/admin_auth_middleware.rs index 3b8be1fd6..8f04a397c 100644 --- a/crates/nostream-admin/src/controllers/admin_auth_middleware.rs +++ b/crates/nostream-admin/src/controllers/admin_auth_middleware.rs @@ -1,10 +1,65 @@ -//! `admin-auth-middleware.ts`. +//! The session gate. Mirrors the cookie half of +//! `src/handlers/request-handlers/admin-auth-middleware.ts` with NIP-98 left out (D6). -use crate::error::AdminError; +use std::sync::Arc; -/// Phase 1 stub — job 005; will authenticates admin requests; see `src/controllers/admin/admin-auth-middleware.ts` or `src/handlers/request-handlers/admin-auth-middleware.ts`. -pub async fn handle() -> Result<(), AdminError> { - Err(AdminError::NotImplemented { - module: "admin::controllers::admin_auth_middleware", - }) +use axum::extract::FromRequestParts; +use axum::http::StatusCode; +use axum::http::request::Parts; +use axum::response::Response; + +use crate::controllers::with_admin_controller_request_handler::error_message; +use crate::state::AdminState; +use crate::utils::session; + +/// A verified session: the `expiresAt` the token carries. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Session { + pub expires_at: i64, +} + +/// The token's `expiresAt` when the request carries a valid session, without failing. +pub fn session_expires_at( + state: &AdminState, + authorization: Option<&str>, + cookie: Option<&str>, +) -> Option { + let key = session::session_key(state.secret()?); + let token = session::token_from_headers(authorization, cookie)?; + if session::is_valid_token(&key, &token, state.clock().now_secs()) { + session::parse_token(&token) + } else { + None + } +} + +impl FromRequestParts> for Session { + type Rejection = Response; + + async fn from_request_parts( + parts: &mut Parts, + state: &Arc, + ) -> Result { + let header = |name: &str| { + parts + .headers + .get(name) + .and_then(|value| value.to_str().ok()) + }; + let authorization = header("authorization"); + let cookie = header("cookie"); + + if state.secret().is_none() { + tracing::warn!("SECRET is not set; the admin session cannot be verified"); + return Err(error_message( + StatusCode::INTERNAL_SERVER_ERROR, + "Internal Server Error", + )); + } + + match session_expires_at(state, authorization, cookie) { + Some(expires_at) => Ok(Self { expires_at }), + None => Err(error_message(StatusCode::UNAUTHORIZED, "Unauthorized")), + } + } } diff --git a/crates/nostream-admin/src/controllers/admin_enabled_middleware.rs b/crates/nostream-admin/src/controllers/admin_enabled_middleware.rs index 97b65559e..668b529d5 100644 --- a/crates/nostream-admin/src/controllers/admin_enabled_middleware.rs +++ b/crates/nostream-admin/src/controllers/admin_enabled_middleware.rs @@ -1,10 +1,169 @@ -//! `admin-enabled-middleware.ts`. +//! The surface gate and the security header. Mirrors +//! `src/handlers/request-handlers/admin-enabled-middleware.ts` and the header half of +//! `src/factories/web-app-factory.ts`. +//! +//! The gate is the outermost layer: `admin.enabled != true` answers `404 text/plain "Not Found"` +//! for the page, the assets and every API route. The header layer mints the per-request CSP +//! nonce, hands it to the page controller and stamps the same value into the +//! `Content-Security-Policy` of every admin response. -use crate::error::AdminError; +use std::sync::Arc; -/// Phase 1 stub — job 005; will gates the admin surface on the feature flag; see `src/handlers/request-handlers/admin-enabled-middleware.ts`. -pub async fn handle() -> Result<(), AdminError> { - Err(AdminError::NotImplemented { - module: "admin::controllers::admin_enabled_middleware", - }) +use axum::extract::{Request, State}; +use axum::http::{HeaderName, HeaderValue}; +use axum::middleware::Next; +use axum::response::Response; + +use crate::controllers::request_context::not_found; +use crate::state::AdminState; + +/// The CSP directive map's order and shape, from `src/factories/web-app-factory.ts:25-45`. +fn content_security_policy(nonce: &str, relay_url: &str, grafana_origin: &str) -> String { + let nonce = nonce_directive(nonce); + let relay_origin = public_origin(relay_url); + let directives: [(&str, Vec); 8] = [ + ( + "img-src", + vec![ + "'self'".to_owned(), + "data:".to_owned(), + "https://cdn.zebedee.io/an/nostr/".to_owned(), + ], + ), + ( + "connect-src", + vec!["'self'".to_owned(), relay_url.to_owned(), relay_origin], + ), + ("default-src", vec!["'self'".to_owned()]), + ( + "frame-src", + vec!["'self'".to_owned(), grafana_origin.to_owned()], + ), + ("script-src-attr", vec![nonce.clone()]), + ( + "script-src", + vec![ + "'self'".to_owned(), + nonce, + "https://cdn.jsdelivr.net/npm/".to_owned(), + "https://unpkg.com/".to_owned(), + "https://cdnjs.cloudflare.com/ajax/libs/".to_owned(), + ], + ), + ( + "style-src", + vec![ + "'self'".to_owned(), + "https://cdn.jsdelivr.net/npm/".to_owned(), + ], + ), + ( + "font-src", + vec![ + "'self'".to_owned(), + "https://cdn.jsdelivr.net/npm/".to_owned(), + ], + ), + ]; + directives + .iter() + .map(|(name, values)| format!("{name} {}", values.join(" "))) + .collect::>() + .join("; ") +} + +fn nonce_directive(nonce: &str) -> String { + format!("'nonce-{nonce}'") +} + +/// The relay URL with its protocol swapped for the browser's (`getWebProtocolForRelay`). +fn public_origin(relay_url: &str) -> String { + match relay_url.find("://") { + Some(index) => { + let scheme = &relay_url[..index]; + let rest = &relay_url[index + 3..]; + let host_end = rest.find('/').unwrap_or(rest.len()); + let web_scheme = if scheme == "wss" { "https" } else { "http" }; + format!("{web_scheme}://{}", &rest[..host_end]) + } + None => relay_url.to_owned(), + } +} + +/// The layer that answers `404 Not Found` while `admin.enabled` is not true. +pub async fn admin_enabled( + State(state): State>, + request: Request, + next: Next, +) -> Response { + let enabled = state + .settings() + .ok() + .and_then(|settings| settings.admin.map(|admin| admin.enabled)) + .unwrap_or(false); + if !enabled { + return not_found(); + } + next.run(request).await +} + +/// The layer that mints the CSP nonce, publishes it as a request extension and stamps the +/// header on every response. +pub async fn content_security( + State(state): State>, + mut request: Request, + next: Next, +) -> Response { + let nonce = state + .random() + .bytes(16) + .map(|bytes| { + use base64::Engine; + base64::engine::general_purpose::STANDARD.encode(bytes) + }) + .unwrap_or_default(); + + request.extensions_mut().insert(Nonce(nonce.clone())); + + let relay_url = state + .settings() + .map(|settings| settings.info.relay_url) + .unwrap_or_default(); + let grafana_origin = crate::utils::grafana::frame_origin(state.grafana()); + + let mut response = next.run(request).await; + let policy = content_security_policy(&nonce, &relay_url, &grafana_origin); + if let Ok(value) = HeaderValue::from_str(&policy) { + response + .headers_mut() + .insert(HeaderName::from_static("content-security-policy"), value); + } + response +} + +/// The per-request CSP nonce the page's `