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
//! Some items for implementing [Content-Security-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/) headers with [axum](https://crates.io/crates/axum)
#![deny(unsafe_code)]

use axum::http::HeaderValue;
use regex::RegexSet;
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter};

#[derive(Hash, Eq, PartialEq, Debug, Clone, Copy, Ord, PartialOrd)]
pub enum CspDirectiveType {
    ChildSrc,
    ConnectSrc,
    DefaultSrc,
    FontSrc,
    FrameSrc,
    ImgSrc,
    ManifestSrc,
    MediaSrc,
    ObjectSrc,
    PrefetchSrc,
    ScriptSource,
    ScriptSourceElem,
    StyleSource,
    StyleSourceElem,
    WorkerSource,
    BaseUri,
    Sandbox,
    FormAction,
    FrameAncestors,
    // Experimental!
    NavigateTo,
    // Experimental/Deprecated, you should use this AND report-to
    ReportUri,
    // Experimental/Deprecated, you should use this AND report-uri
    ReportTo,
    // Experimental!
    RequireTrustedTypesFor,
    // Experimental!
    TrustedTypes,
    UpgradeInsecureRequests,
}

impl AsRef<str> for CspDirectiveType {
    fn as_ref(&self) -> &str {
        match self {
            CspDirectiveType::ChildSrc => "child-src",
            CspDirectiveType::ConnectSrc => "connect-src",
            CspDirectiveType::DefaultSrc => "default-src",
            CspDirectiveType::FrameSrc => "frame-src",
            CspDirectiveType::FontSrc => "font-src",
            CspDirectiveType::ImgSrc => "img-src",
            CspDirectiveType::ManifestSrc => "manifest-src",
            CspDirectiveType::MediaSrc => "media-src",
            CspDirectiveType::ObjectSrc => "object-src",
            CspDirectiveType::PrefetchSrc => "prefetch-src",
            CspDirectiveType::ScriptSource => "script-src",
            CspDirectiveType::ScriptSourceElem => "script-src-elem",
            CspDirectiveType::StyleSource => "style-src",
            CspDirectiveType::StyleSourceElem => "style-src-elem",
            CspDirectiveType::WorkerSource => "worker-src",
            CspDirectiveType::BaseUri => "base-uri",
            CspDirectiveType::Sandbox => "sandbox",
            CspDirectiveType::FormAction => "form-action",
            CspDirectiveType::FrameAncestors => "frame-ancestors",
            // Experimental!
            CspDirectiveType::NavigateTo => "navigate-to",
            // Experimental/Deprecated, you should use this AND report-to
            CspDirectiveType::ReportUri => "report-uri",
            // Experimental/Deprecated, you should use this AND report-uri
            CspDirectiveType::ReportTo => "report-to",
            // Experimental!
            CspDirectiveType::RequireTrustedTypesFor => "require-trusted-types-for",
            // Experimental!
            CspDirectiveType::TrustedTypes => "trusted-types",
            CspDirectiveType::UpgradeInsecureRequests => "upgrade-insecure-requests",
        }
    }
}

impl Display for CspDirectiveType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_ref())
    }
}

impl From<CspDirectiveType> for String {
    fn from(input: CspDirectiveType) -> String {
        input.as_ref().to_string()
    }
}

#[derive(Debug, Clone)]
pub struct CspDirective {
    pub directive_type: CspDirectiveType,
    pub values: Vec<CspValue>,
}

impl CspDirective {
    #[must_use]
    pub fn from(directive_type: CspDirectiveType, values: Vec<CspValue>) -> Self {
        Self {
            directive_type,
            values,
        }
    }

    /// Build a default-src 'self' directive
    pub fn default_self() -> Self {
        Self {
            directive_type: CspDirectiveType::DefaultSrc,
            values: vec![CspValue::SelfSite],
        }
    }
}

impl Display for CspDirective {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let space = if self.values.is_empty() { "" } else { " " };
        f.write_fmt(format_args!(
            "{}{}{}",
            self.directive_type.as_ref(),
            space,
            self.values
                .iter()
                .map(|v| String::from(v.to_owned()))
                .collect::<Vec<String>>()
                .join(" ")
        ))
    }
}

impl From<CspDirective> for HeaderValue {
    fn from(input: CspDirective) -> HeaderValue {
        match HeaderValue::from_str(&input.to_string()) {
            Ok(val) => val,
            Err(e) => panic!("Failed to build HeaderValue from CspDirective: {}", e),
        }
    }
}

/// Build these to find urls to add headers to
#[derive(Clone, Debug)]
pub struct CspUrlMatcher {
    pub matcher: RegexSet,
    pub directives: Vec<CspDirective>,
}

impl CspUrlMatcher {
    #[must_use]
    pub fn new(matcher: RegexSet) -> Self {
        Self {
            matcher,
            directives: vec![],
        }
    }
    pub fn with_directive(&mut self, directive: CspDirective) -> &mut Self {
        self.directives.push(directive);
        self
    }

    /// Exposes the internal matcher.is_match as a struct method
    pub fn is_match(&self, text: &str) -> bool {
        self.matcher.is_match(text)
    }

    /// build a matcher which will emit `default-src 'self';` for all matches
    pub fn default_all_self() -> Self {
        Self {
            matcher: RegexSet::new([r#".*"#]).unwrap(),
            directives: vec![CspDirective {
                directive_type: CspDirectiveType::DefaultSrc,
                values: vec![CspValue::SelfSite],
            }],
        }
    }

    /// build a matcher which will emit `default-src 'self';` for given matches
    pub fn default_self(matcher: RegexSet) -> Self {
        Self {
            matcher,
            directives: vec![CspDirective {
                directive_type: CspDirectiveType::DefaultSrc,
                values: vec![CspValue::SelfSite],
            }],
        }
    }
}

/// Returns the statement as it should show up in the headers
impl From<CspUrlMatcher> for HeaderValue {
    fn from(input: CspUrlMatcher) -> HeaderValue {
        let mut res = String::new();
        for directive in input.directives {
            res.push_str(directive.directive_type.as_ref());
            for val in directive.values {
                res.push_str(&format!(" {}", String::from(val)));
            }
            res.push_str("; ");
        }
        HeaderValue::from_str(res.trim()).unwrap()
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
/// Enum for [CSP source values](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/Sources#sources)
pub enum CspValue {
    None,
    /// Equivalent to 'self' but can't just `Self` in rust
    SelfSite,
    StrictDynamic,
    ReportSample,

    UnsafeInline,
    UnsafeEval,
    UnsafeHashes,
    /// Experimental!
    UnsafeAllowRedirects,
    Host {
        value: String,
    },
    SchemeHttps,
    SchemeHttp,
    SchemeData,
    SchemeOther {
        value: String,
    },
    Nonce {
        value: String,
    },
    Sha256 {
        value: String,
    },
    Sha384 {
        value: String,
    },
    Sha512 {
        value: String,
    },
}

impl From<CspValue> for String {
    fn from(input: CspValue) -> String {
        match input {
            CspValue::None => "'none'".to_string(),
            CspValue::SelfSite => "'self'".to_string(),
            CspValue::StrictDynamic => "'strict-dynamic'".to_string(),
            CspValue::ReportSample => "'report-sample'".to_string(),
            CspValue::UnsafeInline => "'unsafe-inline'".to_string(),
            CspValue::UnsafeEval => "'unsafe-eval'".to_string(),
            CspValue::UnsafeHashes => "'unsafe-hashes'".to_string(),
            CspValue::UnsafeAllowRedirects => "'unsafe-allow-redirects'".to_string(),
            CspValue::SchemeHttps => "https:".to_string(),
            CspValue::SchemeHttp => "http:".to_string(),
            CspValue::SchemeData => "data:".to_string(),
            CspValue::Host { value } | CspValue::SchemeOther { value } => value.to_string(),
            CspValue::Nonce { value } => format!("nonce-{value}"),
            CspValue::Sha256 { value } => format!("sha256-{value}"),
            CspValue::Sha384 { value } => format!("sha384-{value}"),
            CspValue::Sha512 { value } => format!("sha512-{value}"),
        }
    }
}

#[derive(Clone, Debug, Default)]
/// Builder that ends up in a HeaderValue
pub struct CspHeaderBuilder {
    pub directive_map: HashMap<CspDirectiveType, Vec<CspValue>>,
}

impl CspHeaderBuilder {
    pub fn new() -> Self {
        Self {
            directive_map: HashMap::new(),
        }
    }

    pub fn add(mut self, directive: CspDirectiveType, values: Vec<CspValue>) -> Self {
        self.directive_map.entry(directive).or_default();

        values.into_iter().for_each(|val| {
            if !self.directive_map.get(&directive).unwrap().contains(&val) {
                self.directive_map.get_mut(&directive).unwrap().push(val);
            }
        });
        self
    }

    pub fn finish(self) -> HeaderValue {
        let mut keys = self
            .directive_map
            .keys()
            .collect::<Vec<&CspDirectiveType>>();
        keys.sort();

        let directive_strings: Vec<String> = keys
            .iter()
            .map(|directive| {
                let mut directive_string = String::new();
                directive_string.push_str(&format!(" {}", directive));
                let mut values = match self.directive_map.get(directive) {
                    Some(val) => val.to_owned(),
                    None => vec![],
                };
                values.sort();
                values.into_iter().for_each(|val| {
                    directive_string.push_str(&format!(" {}", String::from(val)));
                });
                directive_string.trim().to_string()
            })
            .collect();

        HeaderValue::from_str(&directive_strings.join("; "))
            .expect("Failed to build header value from directive strings")
    }
}