pub struct CspNonce(/* private fields */);Expand description
A validated CSP nonce.
The nonce is interpolated unescaped into HTML attributes
(nonce="{nonce}" on every script/style tag) and raw into the CSP header
value. That is only safe because the value comes from
CspNonce::generate, whose base64 alphabet contains nothing that is
attribute- or directive-significant. The newtype exists so a caller that
supplies its own nonce — which cache-aware callers must, see
crate::template::render_page_with_nonce — cannot hand in
" onload="alert(1) (attribute breakout on every script tag) or
x' 'unsafe-inline (rewriting the policy from outside).
It also makes generate() the obvious path, which blunts the other failure
mode: a caller hard-coding one static, publicly-known nonce for all users
defeats nonce-based CSP entirely.
Implementations§
Source§impl CspNonce
impl CspNonce
Sourcepub fn generate() -> Self
pub fn generate() -> Self
Generate a cryptographically random nonce (32 bytes → 44 base64 chars).
Sourcepub fn parse(value: &str) -> Result<Self, InvalidNonce>
pub fn parse(value: &str) -> Result<Self, InvalidNonce>
Validate a caller-supplied nonce.
Accepts the base64 and base64url alphabets only; anything that could break out of an HTML attribute or a CSP directive is rejected.