Expand description
A pure-Rust parser for the Content Security Policy (CSP) directive grammar.
Phases 02-05 (see plan/): the generic top-level split (policy-list
-> policy -> directive name/raw value), the serialized-source-list
value grammar, and the CSP3 directive registry are implemented so
far.
Structs§
- Directive
- A single directive within a policy: a name and an optional raw value
(CSP3 §2.3,
serialized-directive). - Hash
Expression - A parsed
hash-algorithm "-" base64-valuepair. - Host
Source - A parsed
host-source(CSP3 §2.3.1). - Policy
- A single serialized CSP policy: an ordered list of directives
(CSP3 §2.2,
serialized-policy). - Policy
List - A parsed list of CSP policies, as found in a
Content-Security-PolicyHTTP header (a comma-separated list ofserialized-policy, CSP3 §2.2). - Source
List Entry - A single whitespace-separated token from a
SourceList::Sourceslist, together with its recognizedSourceExpression(if any).
Enums§
- Directive
Status - Whether a registered directive is CSP3-current or deprecated.
- Directive
Value - A directive’s value, interpreted according to its registered
ValueGrammar(orDirectiveValue::Unknownif the directive name isn’t in the registry). - Hash
Algorithm - One of the three hash algorithms CSP3 recognizes (
hash-algorithm). - Host
Part - A parsed
host-part(CSP3 §2.3.1). - Keyword
- All
keyword-sourcevalues (CSP3 §2.3.1). Seeplan/03-source-list-grammar.md: this list was pulled from an automated spec fetch, not verified character-for-character against the current spec text – re-check before treating it as exhaustive. - Port
Part - A parsed
port-part(CSP3 §2.3.1). Kept as a digit string rather than a numeric type: the ABNF (1*DIGIT) does not bound the value to a valid 16-bit port number, and this crate does not normalize. - Source
Expression - One recognized
source-expressionalternative (CSP3 §2.3.1). - Source
List - A parsed
serialized-source-list(CSP3 §2.3.1). - Value
Grammar - Which value grammar a directive’s raw value should be parsed with.
Functions§
- ancestor_
source_ list_ is_ valid - Whether
listonly contains expressions valid inframe-ancestors’ restrictedancestor-source-list: scheme-source, host-source, and'self'– unlike a regularsource-list, no'unsafe-inline'(or any other keyword), no nonce-source, no hash-source. Unrecognized entries (expression: None) also make the list invalid. - parse_
hash_ expression - Parses
hash-algorithm "-" base64-value(CSP3 §2.3.1) frominput, without CSP’s surrounding'...'quotes – callers parsing ahash-sourcetoken strip the quotes first and pass the inner string here; callers parsing an SRIintegrity=""hash token pass it as-is. - parse_
policy_ list - Parses a serialized CSP or comma-separated CSP list (as sent in the
Content-Security-PolicyHTTP header) into aPolicyList. - parse_
source_ list - Parses a directive’s raw value as a
serialized-source-list. - registry_
lookup - Looks up a directive name in the CSP3 directive registry.
ASCII-case-insensitive, per CSP3’s directive-name matching rule (see
plan/02-directive-splitting.md). ReturnsNonefor unregistered names – per CSP3’s forward-compatibility design, that is not itself a syntax error (seeplan/DECISIONS.md, 2026-08-22).