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
//! The names CSRF protection goes by, in one place: the session layer
//! (navree-session) sets and checks the token under them, and pages and
//! forms (navree-ui) carry it under them, so the two cannot drift apart.
/// The cookie holding the CSRF token for scripts (not `HttpOnly`). A script
/// sends its URL-decoded value back as [`XSRF_HEADER`].
pub const XSRF_COOKIE: &str = "XSRF-TOKEN";
/// The header carrying the URL-encoded [`XSRF_COOKIE`] value, as Axios sends
/// it.
pub const XSRF_HEADER: &str = "x-xsrf-token";
/// The header carrying the CSRF token as it is.
pub const CSRF_HEADER: &str = "x-csrf-token";
/// The field a plain HTML form carries the CSRF token in.
pub const CSRF_FIELD: &str = "_csrf";
/// Whether a browser's `Origin` names the host the request was sent to: the request's
/// `Host`, else its URI's authority (HTTP/2 and HTTP/3 send `:authority` and may send no
/// `Host`). The hosts are compared without case and the ports as numbers, a scheme's
/// default port (80 for `http`, 443 for `https`) written or not. An `Origin` that is
/// `null`, has another scheme, a path, a user or a port that isn't one, never matches.
///
/// The scheme is not compared: an HTTP/1.1 request doesn't carry it, and behind a proxy
/// that ends TLS the app sees `http` for a page the browser loaded over `https`. So
/// `http://app.example` matches a request to `app.example`. A browser that sends
/// `Sec-Fetch-Site` (every current one) calls such a page `same-site`, not
/// `same-origin`, and the checks that read it refuse it before this comparison.
///
/// ```
/// use http::{HeaderMap, Uri};
/// use navree_http::csrf::origin_is_host;
///
/// let mut headers = HeaderMap::new();
/// headers.insert("host", "App.Example:443".parse().unwrap());
/// let uri = Uri::from_static("/x");
/// assert!(origin_is_host("https://app.example", &headers, &uri));
/// assert!(!origin_is_host("https://app.example.evil.example", &headers, &uri));
/// // HTTP/2: no Host, the URI's authority.
/// let h2 = Uri::from_static("https://app.example/x");
/// assert!(origin_is_host("https://app.example", &HeaderMap::new(), &h2));
/// ```
/// `authority` as its host and its port, `default` when none is written; `None` for one
/// that doesn't read as a host and a port.
/// A path whose posts the session layer checks by their origin instead of the token: a
/// route that changes nothing on the server and nothing of the user's, only a preference
/// the browser keeps for itself (a theme cookie), so that a page cached for everyone,
/// which carries no token, can post to it. A module contributes one with
/// `m.contribute(SameOriginOnly("/_navree/ui/appearance"))`; navree-ui's appearance route
/// is one.
///
/// A request with a state-changing method (any of them, not only `POST`) to the path
/// passes when the browser says it came from this origin (`Sec-Fetch-Site: same-origin`),
/// or, from a browser that doesn't say, when its `Origin` is this host
/// ([`origin_is_host`]); one from another site, a sibling subdomain included, is refused
/// with a 403 as a token mismatch is. A request that sends neither header passes only
/// without a session cookie (a script, which has no session to ride on); with one it is
/// refused. The path is matched exactly, and the route should refuse requests from other
/// sites itself as well.
///
/// The check is weaker than the token: it trusts the browser's headers, and so any page
/// of this origin (and any script that can make the browser post from it). Use it only
/// for a route whose worst outcome is a changed preference; the session layer logs every
/// such path on the `navree::security` target when it starts.
;