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
//! [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) implementation for Kvarn.
//!
//! See [`Cors`] for usage details.

use crate::prelude::*;
use extensions::RuleSet;

/// A [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) ruleset for Kvarn.
///
/// Use [`Extensions::with_cors`] to allow selected CORS requests.
///
/// By default, Kvarn uses a empty [`RuleSet`]; all CORS requests are rejected.
///
/// # Examples
///
/// ```
/// # use kvarn::prelude::*;
/// // Allow `https://icelk.dev` and `https://kvarn.org` to access all images.
/// // Also allow all requests from `http://example.org` access to the api.
/// let cors =
///     Cors::empty()
///         .add(
///             "/images/*",
///             CorsAllowList::new(Duration::from_secs(60*60*24*365))
///                 .add_origin("https://icelk.dev")
///                 .add_origin("https://kvarn.org")
///             )
///         .add(
///             "/api/*",
///             CorsAllowList::default()
///                 .add_origin("http://example.org")
///                 .add_method(Method::PUT)
///                 .add_method(Method::POST)
///         );
/// ```
pub type Cors = RuleSet<AllowList>;
impl Cors {
    /// Check if the (cross-origin) request's `origin` [`Uri`] is allowed by the CORS rules.
    ///
    /// See [`CorsAllowList::check`] for info about the return types.
    pub fn check_origin(
        &self,
        origin: &Uri,
        uri_path: &str,
    ) -> Option<(MethodAllowList, &[HeaderName], Duration)> {
        self.get(uri_path).and_then(|cal| cal.check(origin))
    }
    /// Check if the [`Request::headers`] and [`Request::uri`] is allowed with this ruleset.
    ///
    /// > This will not check for errors in `access-control-request-headers`.
    ///
    /// Use this over [`Self::check_origin`] because this checks for `same_origin` requests.
    ///
    /// See [`CorsAllowList::check`] for info about the return types.
    pub fn check_cors_request<T>(
        &self,
        request: &Request<T>,
    ) -> Option<(MethodAllowList, &[HeaderName], Duration)> {
        let same_origin_allowed_headers = (
            MethodAllowList::All,
            &[][..],
            Duration::from_secs(60 * 60 * 24 * 7),
        );
        match request.headers().get("origin") {
            None => Some(same_origin_allowed_headers),
            Some(origin)
                if origin.to_str().map_or(false, |origin| {
                    Cors::is_part_of_origin(origin, request.uri())
                }) =>
            {
                Some(same_origin_allowed_headers)
            }
            Some(origin) => match Uri::try_from(origin.as_bytes()) {
                Ok(origin) => match self.check_origin(&origin, request.uri().path()) {
                    Some(allowed) if allowed.0.allowed(request.method()) => Some(allowed),
                    _ => None,
                },
                Err(_) => None,
            },
        }
    }
    /// Checks if `uri` is the same origin as `origin`.
    fn is_part_of_origin(origin: &str, uri: &Uri) -> bool {
        let uri_parts = origin.split_once("://");

        let (origin_scheme, origin_authority) = match uri_parts {
            Some((s, o)) => (s, o),
            None => return origin == "localhost" || origin == "null",
        };
        if Some(origin_scheme) != uri.scheme_str() {
            return false;
        }
        uri.authority()
            .map(uri::Authority::as_str)
            .map_or(false, |authority| authority == origin_authority)
    }
}

/// A CORS allow list which allowes hosts, methods, and headers from a associated path.
/// This is a builder-like struct.
/// Use the `add_*` methods to add allowed origins, methods, and headers.
/// Multiple allow lists can be added to a [`Cors`] instance.
/// See the example at [`Cors`].
///
/// Use [`RuleSet::add`] to add a rule.
#[must_use]
#[derive(Debug)]
pub struct AllowList {
    allowed: Vec<Uri>,
    allow_all_origins: bool,
    methods: Option<Vec<Method>>,
    headers: Vec<HeaderName>,
    cache_for: Duration,
}
impl AllowList {
    /// Creates a empty CORS allow list with the client cache duration of `cache_for`.
    pub fn new(cache_for: Duration) -> Self {
        Self {
            allowed: Vec::new(),
            allow_all_origins: false,
            methods: Some(vec![Method::GET, Method::HEAD, Method::OPTIONS]),
            headers: Vec::new(),
            cache_for,
        }
    }
    /// Allows CORS request from `allowed_origin`.
    /// Note that the scheme (`https` / `http`) is sensitive.
    /// Use [`Self::add_origin_uri`] for a [`Uri`] input.
    ///
    /// # Panics
    ///
    /// Panics if `allowed_origin` is not a valid [`Uri`]
    /// or if it doesn't contain a host AND a scheme.
    pub fn add_origin(self, allowed_origin: impl AsRef<str>) -> Self {
        self.add_origin_uri(Uri::try_from(allowed_origin.as_ref()).unwrap())
    }
    /// Allows CORS request from `allowed_origin`.
    /// Note that the scheme (`https` / `http`) is sensitive.
    ///
    /// # Panics
    ///
    /// Panics if `allowed_origin` doesn't contain a host AND a scheme.
    pub fn add_origin_uri(mut self, allowed_origin: Uri) -> Self {
        assert!(allowed_origin.host().is_some());
        assert!(allowed_origin.scheme().is_some());
        self.allowed.push(allowed_origin);
        self
    }
    /// Enables the flag to allow all origins to use the set methods and headers in CORS requests.
    pub fn allow_all_origins(mut self) -> Self {
        self.allow_all_origins = true;
        self
    }
    /// Allows the listed origin(s) (added via [`Self::add_origin`])
    /// to request using `allowed_method`.
    pub fn add_method(mut self, allowed_method: Method) -> Self {
        let methods = self.methods.get_or_insert_with(Vec::new);
        if !methods.contains(&allowed_method) {
            methods.push(allowed_method);
        }
        self
    }
    /// Allows all methods.
    pub fn allow_all_methods(mut self) -> Self {
        self.methods = None;
        self
    }
    /// Allows the listed origin(s) (added via [`Self::add_origin`])
    /// to send the `allowed_header` in the request.
    pub fn add_header(mut self, allowed_header: HeaderName) -> Self {
        if !self.headers.contains(&allowed_header) {
            self.headers.push(allowed_header);
        }
        self
    }
    fn get_methods(&self) -> MethodAllowList {
        self.methods
            .as_deref()
            .map_or(MethodAllowList::All, MethodAllowList::Selected)
    }
    /// Checks if the `origin` is allowed according to the allow list.
    ///
    /// Returns [`Some`] if `origin` is allowed, with the [`Method`]s and [`HeaderName`]s
    /// allowed, with a cache max-age of [`Duration`].
    /// Returns [`None`] if `origin` isn't allowed.
    pub fn check(&self, origin: &Uri) -> Option<(MethodAllowList, &[HeaderName], Duration)> {
        if self.allow_all_origins {
            return Some((self.get_methods(), &self.headers, self.cache_for));
        }
        for allowed in &self.allowed {
            let scheme = allowed.scheme().map_or("https", uri::Scheme::as_str);
            // This is OK; we assert it has a host when we add it
            if Some(allowed.host().unwrap()) == origin.host()
                && allowed.port_u16() == origin.port_u16()
                && Some(scheme) == origin.scheme().map(uri::Scheme::as_str)
            {
                return Some((self.get_methods(), &self.headers, self.cache_for));
            }
        }
        None
    }
}
/// The default `cache_for` is 1 hour.
impl Default for AllowList {
    fn default() -> Self {
        Self::new(Duration::from_secs(60 * 60))
    }
}

/// The allowed methods.
#[derive(Debug)]
#[must_use]
pub enum MethodAllowList<'a> {
    /// All methods are allowed.
    All,
    /// Only the methods in the slice are allowed.
    Selected(&'a [Method]),
}
impl<'a> MethodAllowList<'a> {
    #[must_use]
    fn allowed(&self, method: &Method) -> bool {
        match self {
            Self::All => true,
            Self::Selected(list) => list.contains(method),
        }
    }
    fn to_bytes(&self) -> Bytes {
        match self {
            Self::All => Bytes::from_static(b"*"),
            Self::Selected(list) => list
                .iter()
                .enumerate()
                .fold(BytesMut::with_capacity(24), |mut acc, (pos, method)| {
                    acc.extend_from_slice(method.as_str().as_bytes());
                    if pos + 1 != list.len() {
                        acc.extend_from_slice(b", ");
                    }
                    acc
                })
                .freeze(),
        }
    }
}

fn options_prepare(options_cors_settings: Arc<Cors>) -> Prepare {
    prepare!(request, _, _, _, move |options_cors_settings: Arc<Cors>| {
        let allowed = options_cors_settings.check_cors_request(request);

        if allowed.is_none() {
            return {
                let response = Response::builder()
                    .status(StatusCode::FORBIDDEN)
                    .body(Bytes::from_static(b"CORS request denied"))
                    .expect("we know this is a good request.");
                FatResponse::new(response, comprash::ServerCachePreference::Full)
            };
        }

        let mut builder = Response::builder().status(StatusCode::NO_CONTENT);

        if let Some((methods, headers, cache_for)) = allowed {
            let methods = methods.to_bytes();
            let headers = headers
                .iter()
                .enumerate()
                .fold(BytesMut::with_capacity(24), |mut acc, (pos, header)| {
                    acc.extend_from_slice(header.as_str().as_bytes());
                    if pos + 1 != headers.len() {
                        acc.extend_from_slice(b", ");
                    }
                    acc
                })
                .freeze();

            builder = builder
                .header(
                    "access-control-allow-methods",
                    // We know all the characters from [`Method::as_str`] are valid.
                    HeaderValue::from_maybe_shared(methods).unwrap(),
                )
                .header(
                    "access-control-allow-headers",
                    // We know all the characters from [`HeaderName::as_str()`] are valid.
                    // See https://docs.rs/http/0.2.4/http/header/struct.HeaderValue.html#impl-From%3CHeaderName%3E
                    HeaderValue::from_maybe_shared(headers).unwrap(),
                )
                .header(
                    "access-control-max-age",
                    // We know a number is valid
                    HeaderValue::try_from(
                        // if > second integer, add 1 second (ceil the duration).
                        // i64::from(bool) returns 1 if true.
                        (cache_for.as_secs() + u64::from(cache_for.subsec_nanos() > 0)).to_string(),
                    )
                    .unwrap(),
                );
        }

        let response = builder.body(Bytes::new()).unwrap_or_else(|_| {
            Response::builder()
                .status(StatusCode::INTERNAL_SERVER_ERROR)
                .body(utils::hardcoded_error_body(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    None,
                ))
                .expect("this is a good response.")
        });
        FatResponse::new(response, comprash::ServerCachePreference::None)
    })
}
impl Extensions {
    /// Adds extensions to disallow all CORS requests.
    /// This is added when calling [`Extensions::new`].
    pub fn with_disallow_cors(&mut self) -> &mut Self {
        self.add_prime(
            prime!(request, _, _, {
                let missmatch = request
                    .headers()
                    .get("origin")
                    .and_then(|origin| origin.to_str().ok())
                    .map_or(false, |origin| {
                        !Cors::is_part_of_origin(origin, request.uri())
                    });
                if missmatch {
                    Some(Uri::from_static("/./cors_fail"))
                } else {
                    None
                }
            }),
            Id::new(16_777_216, "Reroute all CORS requests to /./cors_fail"),
        );

        self.add_prepare_single(
            "/./cors_fail",
            prepare!(_, _, _, _, {
                let response = Response::builder()
                    .status(StatusCode::FORBIDDEN)
                    .body(Bytes::from_static(b"CORS request denied"))
                    .expect("we know this is a good request.");
                FatResponse::new(response, comprash::ServerCachePreference::Full)
            }),
        );
        self.add_prepare_single("/./cors_options", options_prepare(Cors::empty().arc()));
        self.add_prime(
            prime!(request, _, _, {
                if request.method() == Method::OPTIONS
                    && request.headers().get("origin").is_some()
                    && request
                        .headers()
                        .get("access-control-request-method")
                        .is_some()
                {
                    Some(Uri::from_static("/./cors_options"))
                } else {
                    None
                }
            }),
            Id::new(16_777_215, "Provides CORS preflight request support"),
        );
        self
    }
    /// Overrides the default handling (deny all) of CORS requests to be `cors_settings`.
    ///
    /// See [`Cors`] for an example and more info.
    pub fn with_cors(&mut self, cors_settings: Arc<Cors>) -> &mut Self {
        self.with_disallow_cors();

        let options_cors_settings = Arc::clone(&cors_settings);
        self.add_prepare_single("/./cors_options", options_prepare(options_cors_settings));
        let package_cors_settings = Arc::clone(&cors_settings);

        // This priority have to be higher than the one in the [`Self::add_disallow_cors`]'s prime
        // extension.
        self.add_prime(
            prime!(request, _, _, move |cors_settings: Arc<Cors>| {
                let allow = cors_settings.check_cors_request(request);
                if allow.is_some() {
                    None
                } else {
                    Some(Uri::from_static("/./cors_fail"))
                }
            }),
            Id::new(
                16_777_216,
                "Reroute not allowed CORS request to /./cors_fail",
            ),
        );

        // Low priority so it runs last.
        self.add_package(
            package!(
                response,
                request,
                _,
                _,
                move |package_cors_settings: Arc<Cors>| {
                    if let Some(origin) = request.headers().get("origin") {
                        let allowed = package_cors_settings.check_cors_request(request).is_some();
                        if allowed {
                            response
                                .headers_mut()
                                .insert("access-control-allow-origin", origin.clone());
                        }
                    }
                }
            ),
            Id::new(
                -1024,
                "Adds access-control-allow-origin depending on if CORS request is allowed",
            ),
        );

        self
    }
}