Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_net/src/http/origin.rs

3.3 KiB, 1 run

created by r1870400018:68846, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Which posts a browser sent from another site.
2//!
3//! A state-changing post on a public endpoint is a form that any page can aim a visitor's browser
4//! at. A browser declares where the post came from, in `Sec-Fetch-Site` and in `Origin`, and a page
5//! cannot forge either. A client that is not a browser sends neither, and is left to whatever limit
6//! applies to its own address.
7
8use crate::http::{
9 fields::{
10 HeaderFields,
11 HeaderName,
12 },
13 loc::Url,
14};
15
16use oxedyne_fe2o3_core::prelude::*;
17
18/// Did a browser send this post from another site?
19///
20/// `Sec-Fetch-Site`, where present, decides: the post passes only if every line says `same-origin`.
21/// Otherwise an `Origin` must name `expected_origin` on every line (scheme and host without case, a
22/// default port as good as none), or the request's own origin, which is the same scheme with the
23/// `Host` the visitor addressed, so that a site's second hostname works in a browser that sends no
24/// `Sec-Fetch-Site`. `null` does not pass. Neither header present passes. Neither field is a
25/// singleton, so a caller can send several lines, and one foreign line refuses the post.
26///
27/// Where `expected_origin` is empty or is no URL, the `Host` the visitor addressed stands in for it
28/// and the scheme goes unchecked. An `Origin` that then cannot be checked at all is refused.
29pub fn cross_site(headers: &HeaderFields, expected_origin: &str) -> bool {
30 if let Some(list) = headers.get_list(&HeaderName::SecFetchSite) {
31 if !list.is_empty() {
32 return !list.iter().all(|v| fmt!("{}", v).trim().eq_ignore_ascii_case("same-origin"));
33 }
34 }
35 let origins = match headers.get_list(&HeaderName::Origin) {
36 Some(list) if !list.is_empty() => list,
37 _ => return false,
38 };
39 let want = Url::parse(expected_origin).ok();
40 let host = headers.get_one(&HeaderName::Host).map(|v| fmt!("{}", v).trim().to_lowercase());
41 origins.iter().any(|v| !origin_is_ours(fmt!("{}", v).trim(), want.as_ref(), host.as_deref()))
42}
43
44fn origin_is_ours(raw: &str, want: Option<&Url>, host: Option<&str>) -> bool {
45 // Userinfo is no part of an origin, and `Url::parse` would discard it.
46 if raw.contains('@') {
47 return false;
48 }
49 let url = match Url::parse(raw) {
50 Ok(u) => u,
51 Err(_) => return false, // `null`, and anything else that is no origin
52 };
53 if url.target != "/" {
54 return false; // an origin has no path
55 }
56 match (want, host) {
57 (Some(w), _) => {
58 if url.origin() == w.origin() {
59 return true;
60 }
61 // The request's own origin: the site's scheme with the host the visitor addressed.
62 match host {
63 Some(h) => match Url::parse(&fmt!("{}://{}", w.scheme, h)) {
64 Ok(own) => url.origin() == own.origin(),
65 Err(_) => false,
66 },
67 None => false,
68 }
69 },
70 (None, Some(h)) => {
71 let bare = if url.port == url.scheme.default_port() {
72 url.host.clone()
73 } else {
74 fmt!("{}:{}", url.host, url.port)
75 };
76 h == bare || h == fmt!("{}:{}", url.host, url.port)
77 },
78 (None, None) => false,
79 }
80}