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. Parsing an existing nonce is useful for
diagnostics and policy inspection. Public page renderers always generate a
fresh nonce themselves; no renderer accepts a caller-supplied nonce.
Implementations§
Source§impl CspNonce
impl CspNonce
Sourcepub fn generate() -> Result<Self, NonceError>
pub fn generate() -> Result<Self, NonceError>
Draw a cryptographically random nonce (32 bytes → 44 base64 chars), or
return NonceError if the system random source fails. The render path
carries a failed draw as a crate::RenderError.
Sourcepub fn try_generate() -> Result<Self, NonceError>
pub fn try_generate() -> Result<Self, NonceError>
Compatibility alias for CspNonce::generate, with the same fallible result.
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.