Skip to main content

parse_policy_list

Function parse_policy_list 

Source
pub fn parse_policy_list(input: &str) -> PolicyList
Expand description

Parses a serialized CSP or comma-separated CSP list (as sent in the Content-Security-Policy HTTP header) into a PolicyList.

This only performs the generic, directive-independent split described by CSP3 §2.2/§2.3 (serialized-policy-list / serialized-policy / serialized-directive) – directive values are kept raw. Directive name/value byte-class conformance is available per directive via Directive::name_is_valid/Directive::value_is_valid.

Examples found in repository?
examples/parse.rs (line 16)
6fn main() {
7    let policy = "default-src 'self'; \
8                  script-src 'self' 'nonce-2726c7f26c' https://cdn.example.com; \
9                  img-src 'self' data:; \
10                  frame-ancestors 'none'; \
11                  upgrade-insecure-requests; \
12                  report-to csp-endpoint";
13
14    println!("Parsing: {policy}\n");
15
16    let policy_list = parse_policy_list(policy);
17    for policy in &policy_list.policies {
18        for directive in &policy.directives {
19            println!("{}:", directive.name);
20            match directive.value() {
21                DirectiveValue::SourceList(list) => println!("  source list: {list:?}"),
22                DirectiveValue::AncestorSourceList(list) => {
23                    println!("  ancestor source list: {list:?}");
24                }
25                DirectiveValue::Boolean => println!("  (no value)"),
26                DirectiveValue::Token(token) => println!("  token: {token:?}"),
27                other => println!("  {other:?}"),
28            }
29        }
30    }
31}