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 try_generate() -> Result<Self, NonceError>
pub fn try_generate() -> Result<Self, NonceError>
Draw a cryptographically random nonce (32 bytes → 44 base64 chars), or
say that the system random source failed. The render path uses this
one, so a failed draw is a crate::RenderError, not a panic.
Sourcepub fn generate() -> Self
pub fn generate() -> Self
Generate a cryptographically random nonce (32 bytes → 44 base64 chars).
§Panics
If the system random source fails, as at 0.2.1; its signature has no
room for the error. CspNonce::try_generate returns it instead.
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.