csp-parse 0.1.0

A pure-Rust parser for the Content Security Policy (CSP) directive grammar
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! The CSP3 directive registry (§6 and subsections): maps a known
//! directive name to its value grammar, and interprets a [`Directive`]'s
//! raw value accordingly -- turning "name + raw value" (Phase 02) plus
//! the source-list parser (Phase 03) into a fully structured directive,
//! as sketched by the architecture diagram in `CLAUDE.md`.
//!
//! **Registry completeness is a snapshot, not a permanent guarantee** —
//! see `plan/04-directive-registry.md`'s risks section and the README's
//! status/scope notes (Phase 06). `webrtc` is deliberately left
//! unregistered pending verification against the then-current spec text
//! (its standardization status was in flux); `referrer` (CSP1, removed
//! before CSP3) is intentionally out of scope entirely -- this crate's
//! normative basis is CSP3 only (`CLAUDE.md`).

use crate::ast::Directive;
use crate::source_list::{Keyword, SourceExpression, SourceList, parse_source_list};

/// Which value grammar a directive's raw value should be parsed with.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ValueGrammar {
    /// `serialized-source-list` (CSP3 §2.3.1).
    SourceList,
    /// `frame-ancestors`' restricted `ancestor-source-list`: parsed with
    /// the same [`crate::parse_source_list`] as `SourceList`, but only
    /// scheme-source, host-source, and `'self'` are valid there -- see
    /// [`ancestor_source_list_is_valid`].
    AncestorSourceList,
    /// `sandbox`'s whitespace-separated, unquoted token list.
    SandboxTokens,
    /// No value expected (e.g. `upgrade-insecure-requests`).
    Boolean,
    /// A single raw token (e.g. `report-to`, `require-trusted-types-for`).
    Token,
    /// Whitespace-separated raw tokens with no further structure parsed
    /// by this crate (e.g. `report-uri`'s URI references, `plugin-types`'
    /// MIME-type patterns -- both grammars this crate deliberately does
    /// not implement, matching the narrow, generic scope from
    /// `CLAUDE.md`).
    TokenList,
    /// `trusted-types`' policy-name list (plus the `'allow-duplicates'`/
    /// `'none'` sentinel tokens), kept as raw tokens for the same reason
    /// as `TokenList`.
    TrustedTypes,
}

/// Whether a registered directive is CSP3-current or deprecated.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DirectiveStatus {
    /// Part of the current CSP3 directive set.
    Current,
    /// Kept for backward compatibility, superseded by another directive
    /// (e.g. `report-uri` by `report-to`) or removed from later spec
    /// drafts, but still commonly seen in the wild.
    Deprecated,
}

const REGISTRY: &[(&str, ValueGrammar, DirectiveStatus)] = &[
    // Fetch directives (CSP3 §6.1).
    (
        "child-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "connect-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "default-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "font-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "frame-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "img-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "manifest-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "media-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "object-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "script-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "script-src-elem",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "script-src-attr",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "style-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "style-src-elem",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "style-src-attr",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "worker-src",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    // Document directives (CSP3 §6.3).
    (
        "base-uri",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "sandbox",
        ValueGrammar::SandboxTokens,
        DirectiveStatus::Current,
    ),
    // Navigation directives (CSP3 §6.4).
    (
        "form-action",
        ValueGrammar::SourceList,
        DirectiveStatus::Current,
    ),
    (
        "frame-ancestors",
        ValueGrammar::AncestorSourceList,
        DirectiveStatus::Current,
    ),
    // Reporting directives (CSP3 §6.5).
    ("report-to", ValueGrammar::Token, DirectiveStatus::Current),
    (
        "report-uri",
        ValueGrammar::TokenList,
        DirectiveStatus::Deprecated,
    ),
    // Boolean/valueless directives.
    (
        "upgrade-insecure-requests",
        ValueGrammar::Boolean,
        DirectiveStatus::Current,
    ),
    (
        "block-all-mixed-content",
        ValueGrammar::Boolean,
        DirectiveStatus::Deprecated,
    ),
    // Trusted Types directives.
    (
        "require-trusted-types-for",
        ValueGrammar::Token,
        DirectiveStatus::Current,
    ),
    (
        "trusted-types",
        ValueGrammar::TrustedTypes,
        DirectiveStatus::Current,
    ),
    // Deprecated, CSP2-era.
    (
        "plugin-types",
        ValueGrammar::TokenList,
        DirectiveStatus::Deprecated,
    ),
];

/// 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`). Returns `None` for unregistered
/// names -- per CSP3's forward-compatibility design, that is not itself
/// a syntax error (see `plan/DECISIONS.md`, 2026-08-22).
pub fn registry_lookup(name: &str) -> Option<(ValueGrammar, DirectiveStatus)> {
    REGISTRY
        .iter()
        .find(|(registered_name, _, _)| registered_name.eq_ignore_ascii_case(name))
        .map(|(_, grammar, status)| (*grammar, *status))
}

/// A directive's value, interpreted according to its registered
/// [`ValueGrammar`] (or [`DirectiveValue::Unknown`] if the directive
/// name isn't in the registry).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum DirectiveValue {
    /// See [`ValueGrammar::SourceList`].
    SourceList(SourceList),
    /// See [`ValueGrammar::AncestorSourceList`].
    AncestorSourceList(SourceList),
    /// See [`ValueGrammar::SandboxTokens`].
    Sandbox(Vec<String>),
    /// See [`ValueGrammar::Boolean`].
    Boolean,
    /// See [`ValueGrammar::Token`].
    Token(Option<String>),
    /// See [`ValueGrammar::TokenList`].
    TokenList(Vec<String>),
    /// See [`ValueGrammar::TrustedTypes`].
    TrustedTypes(Vec<String>),
    /// The directive name isn't in the registry (see this module's docs
    /// for what that does and doesn't imply).
    Unknown,
}

impl Directive {
    /// Interprets [`Directive::raw_value`] via the CSP3 directive
    /// registry for [`Directive::name`]. Computed on demand rather than
    /// stored on `Directive` itself, so [`crate::parse_policy_list`]
    /// (Phase 02) stays a pure, registry-independent split -- see
    /// `plan/DECISIONS.md`, 2026-08-22.
    pub fn value(&self) -> DirectiveValue {
        let Some((grammar, _status)) = registry_lookup(&self.name) else {
            return DirectiveValue::Unknown;
        };
        let raw = self.raw_value.as_deref().unwrap_or("");
        match grammar {
            ValueGrammar::SourceList => DirectiveValue::SourceList(parse_source_list(raw)),
            ValueGrammar::AncestorSourceList => {
                DirectiveValue::AncestorSourceList(parse_source_list(raw))
            }
            ValueGrammar::SandboxTokens => DirectiveValue::Sandbox(tokenize(raw)),
            ValueGrammar::Boolean => DirectiveValue::Boolean,
            ValueGrammar::Token => {
                DirectiveValue::Token(raw.split_ascii_whitespace().next().map(str::to_string))
            }
            ValueGrammar::TokenList => DirectiveValue::TokenList(tokenize(raw)),
            ValueGrammar::TrustedTypes => DirectiveValue::TrustedTypes(tokenize(raw)),
        }
    }

    /// For a [`ValueGrammar::Boolean`] directive (e.g.
    /// `upgrade-insecure-requests`): whether it unexpectedly carries a
    /// value. CSP3 defines these directives as valueless -- a present
    /// value is a caller-visible anomaly (a diagnostic), not silently
    /// dropped or a panic. `false` for non-boolean or unregistered
    /// directives.
    pub fn boolean_value_is_unexpected(&self) -> bool {
        matches!(
            registry_lookup(&self.name),
            Some((ValueGrammar::Boolean, _))
        ) && self.raw_value.is_some()
    }
}

fn tokenize(raw: &str) -> Vec<String> {
    raw.split_ascii_whitespace().map(str::to_string).collect()
}

/// Whether `list` only contains expressions valid in `frame-ancestors`'
/// restricted `ancestor-source-list`: scheme-source, host-source, and
/// `'self'` -- unlike a regular `source-list`, no `'unsafe-inline'` (or
/// any other keyword), no nonce-source, no hash-source. Unrecognized
/// entries (`expression: None`) also make the list invalid.
pub fn ancestor_source_list_is_valid(list: &SourceList) -> bool {
    match list {
        SourceList::None => true,
        SourceList::Sources(entries) => entries.iter().all(|entry| {
            matches!(
                entry.expression,
                Some(SourceExpression::Scheme(_))
                    | Some(SourceExpression::Host(_))
                    | Some(SourceExpression::Keyword(Keyword::SelfKeyword))
            )
        }),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parse_policy_list;

    fn directive_value(policy_str: &str) -> DirectiveValue {
        parse_policy_list(policy_str).policies[0].directives[0].value()
    }

    #[test]
    fn fetch_directive_is_source_list() {
        assert!(matches!(
            directive_value("default-src 'self'"),
            DirectiveValue::SourceList(_)
        ));
    }

    #[test]
    fn sandbox_tokens() {
        assert_eq!(
            directive_value("sandbox allow-scripts allow-forms"),
            DirectiveValue::Sandbox(vec!["allow-scripts".to_string(), "allow-forms".to_string()])
        );
    }

    #[test]
    fn base_uri_and_form_action_are_source_list() {
        assert!(matches!(
            directive_value("base-uri 'self'"),
            DirectiveValue::SourceList(_)
        ));
        assert!(matches!(
            directive_value("form-action 'self'"),
            DirectiveValue::SourceList(_)
        ));
    }

    #[test]
    fn frame_ancestors_accepts_self_and_hosts_but_rejects_unsafe_inline_nonce_hash() {
        let allowed = directive_value("frame-ancestors 'self' example.com https:");
        match allowed {
            DirectiveValue::AncestorSourceList(list) => {
                assert!(ancestor_source_list_is_valid(&list));
            }
            other => panic!("expected AncestorSourceList, got {other:?}"),
        }

        for rejected_raw in [
            "frame-ancestors 'unsafe-inline'",
            "frame-ancestors 'nonce-abc123'",
            "frame-ancestors 'sha256-abc123'",
        ] {
            match directive_value(rejected_raw) {
                DirectiveValue::AncestorSourceList(list) => {
                    assert!(!ancestor_source_list_is_valid(&list), "{rejected_raw}");
                }
                other => panic!("expected AncestorSourceList, got {other:?}"),
            }
        }
    }

    #[test]
    fn report_to_and_report_uri() {
        assert_eq!(
            directive_value("report-to endpoint-1"),
            DirectiveValue::Token(Some("endpoint-1".to_string()))
        );
        assert_eq!(
            directive_value("report-uri https://example.com/csp-report"),
            DirectiveValue::TokenList(vec!["https://example.com/csp-report".to_string()])
        );
        assert_eq!(
            registry_lookup("report-uri").map(|(_, status)| status),
            Some(DirectiveStatus::Deprecated)
        );
    }

    #[test]
    fn boolean_directives() {
        let list = parse_policy_list("upgrade-insecure-requests");
        let directive = &list.policies[0].directives[0];
        assert_eq!(directive.value(), DirectiveValue::Boolean);
        assert!(!directive.boolean_value_is_unexpected());

        let list = parse_policy_list("upgrade-insecure-requests 'self'");
        let directive = &list.policies[0].directives[0];
        assert_eq!(directive.value(), DirectiveValue::Boolean);
        assert!(directive.boolean_value_is_unexpected());

        assert_eq!(
            registry_lookup("block-all-mixed-content").map(|(_, status)| status),
            Some(DirectiveStatus::Deprecated)
        );
    }

    #[test]
    fn trusted_types_directives() {
        assert_eq!(
            directive_value("require-trusted-types-for 'script'"),
            DirectiveValue::Token(Some("'script'".to_string()))
        );
        assert_eq!(
            directive_value("trusted-types my-policy 'allow-duplicates'"),
            DirectiveValue::TrustedTypes(vec![
                "my-policy".to_string(),
                "'allow-duplicates'".to_string(),
            ])
        );
    }

    #[test]
    fn plugin_types_is_deprecated_token_list() {
        assert_eq!(
            directive_value("plugin-types application/pdf"),
            DirectiveValue::TokenList(vec!["application/pdf".to_string()])
        );
        assert_eq!(
            registry_lookup("plugin-types").map(|(_, status)| status),
            Some(DirectiveStatus::Deprecated)
        );
    }

    #[test]
    fn unknown_directive_stays_syntactically_valid_but_unstructured() {
        let list = parse_policy_list("default-src 'self'; totally-unknown-directive foo");
        assert_eq!(list.policies[0].directives.len(), 2);
        assert_eq!(
            list.policies[0].directives[1].value(),
            DirectiveValue::Unknown
        );
    }

    #[test]
    fn registry_lookup_is_case_insensitive() {
        assert!(registry_lookup("Default-Src").is_some());
        assert!(registry_lookup("DEFAULT-SRC").is_some());
    }
}