csp-parse
The Content Security Policy Level 3 directive grammar in Rust: policy lists, directives, source lists and hash expressions.
Parses a serialized Content Security Policy, or the comma-separated list of policies of a
Content-Security-Policy header, into policies, directives and their values. It enforces
nothing: it does not decide whether a resource may load. Norm: Content Security Policy
Level 3.
Status
On crates.io, no dependencies. Used by html-conform
for the CSP datatypes and checks and, through parse_hash_expression, for the Subresource
Integrity integrity attribute.
No official grammar-only test suite exists: the CSP tests in web-platform-tests check
enforcement in a browser, not syntax, so nothing could be vendored. Confidence comes from
unit tests per grammar stage and tests/spec_examples.rs (spec-style policies and
robustness cases).
Structure
Public surface
| Entry | Purpose |
|---|---|
parse_policy_list | text → PolicyList (Policy → Directive with name and raw value); never fails |
Directive::name_is_valid, value_is_valid | per-item validity, queried separately from parsing |
Directive::value | raw value → DirectiveValue according to the registered grammar, Unknown for an unregistered name |
registry_lookup | directive name → ValueGrammar and DirectiveStatus (current or deprecated) |
parse_source_list, SourceList, SourceListEntry, SourceExpression | the serialized-source-list grammar: keywords, hosts, schemes, nonces, hashes |
ancestor_source_list_is_valid | the restricted list of frame-ancestors |
parse_hash_expression | hash-algorithm "-" base64-value, quoted or not, reusable for SRI |
Dependencies
Downward: none. Upward: html-conform (L1).
Limits
- Lenient by design. CSP is forward-compatible, so an unknown directive, an unknown source expression or a byte outside a production’s character class does not fail the parse; it is reported structurally and validity is a separate question.
- The directive registry is a snapshot of the CSP3 directive set. An unregistered name is
not an error; it gets
DirectiveValue::Unknownwith its raw name and value. - Some value grammars stay raw tokens:
report-uriURIs,plugin-typesMIME patterns and the policy names oftrusted-types. - Syntax only. No matching of a URL against a source list, no nonce or hash comparison, no reporting.