BarrierLab

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

header or meta value

parse_policy_list

policy · directive name · raw value

registry_lookup

name → grammar + status

Directive::value

DirectiveValue

parse_source_list

SourceList

parse_hash_expression

HashExpression

Public surface

EntryPurpose
parse_policy_listtext → PolicyList (Policy → Directive with name and raw value); never fails
Directive::name_is_valid, value_is_validper-item validity, queried separately from parsing
Directive::valueraw value → DirectiveValue according to the registered grammar, Unknown for an unregistered name
registry_lookupdirective name → ValueGrammar and DirectiveStatus (current or deprecated)
parse_source_list, SourceList, SourceListEntry, SourceExpressionthe serialized-source-list grammar: keywords, hosts, schemes, nonces, hashes
ancestor_source_list_is_validthe restricted list of frame-ancestors
parse_hash_expressionhash-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::Unknown with its raw name and value.
  • Some value grammars stay raw tokens: report-uri URIs, plugin-types MIME patterns and the policy names of trusted-types.
  • Syntax only. No matching of a URL against a source list, no nonce or hash comparison, no reporting.

Edit this page on GitHub