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
use topcoat_core::context::Cx;
use crate::{
Body, IntoPath, Layer, LayerFuture, Method, Next, Path, PathBuf,
error::forbidden,
header,
request::{headers, method, uri},
};
/// The cross-origin request policy the router applies to every request.
///
/// By default, rejects state-changing browser requests and WebSocket
/// handshakes from untrusted origins. This helps protect against cross-site
/// request forgery and cross-site WebSocket hijacking.
///
/// Requests identified as same-origin or direct navigations pass. Requests
/// without origin or fetch metadata headers also pass. Register a policy with
/// [`RouterBuilder::origin_policy`](crate::RouterBuilder::origin_policy) to
/// trust origins, exempt paths, or disable verification.
///
/// # Examples
///
/// ```rust
/// use topcoat::router::{OriginPolicy, Router};
///
/// let router = Router::builder()
/// .origin_policy(OriginPolicy::new().trust_origins(["https://accounts.example.com"]))
/// .build();
/// ```
#[derive(Debug)]
pub struct OriginPolicy {
inner: Inner,
}
#[derive(Debug)]
enum Inner {
/// Verifies every request, trusting the listed cross-origin peers and
/// skipping the exempted paths.
Verify {
trusted_origins: Vec<String>,
exempt_paths: Vec<PathBuf>,
},
/// No verification at all.
Disabled,
}
impl OriginPolicy {
/// Creates the default policy: every request is verified, with no trusted
/// cross-origin peers and no exempt paths.
///
/// Loosen it with [`trust_origins`](Self::trust_origins) and
/// [`exempt_paths`](Self::exempt_paths).
#[must_use]
pub fn new() -> Self {
Self {
inner: Inner::Verify {
trusted_origins: Vec::new(),
exempt_paths: Vec::new(),
},
}
}
/// Trusts `origins` to send state-changing cross-origin requests and to
/// open cross-origin WebSockets.
///
/// Each value is compared against the request's `Origin` header, so pass
/// the full serialized origin: scheme, host, and any non-default port
/// (`"https://accounts.example.com"`), with no trailing slash. The
/// comparison is ASCII case-insensitive.
#[must_use]
pub fn trust_origins<I>(mut self, origins: I) -> Self
where
I: IntoIterator,
I::Item: Into<String>,
{
if let Inner::Verify {
trusted_origins, ..
} = &mut self.inner
{
trusted_origins.extend(origins.into_iter().map(Into::into));
}
self
}
/// Exempts the routes at `paths` from origin verification.
///
/// A request whose URL matches one of the paths passes unchecked, no
/// matter where it comes from. Use this for a route that must accept
/// cross-origin requests from anywhere and handles its own protection,
/// like a WebSocket endpoint open to other sites. Paths use the route
/// path syntax, so `{param}` and `{*rest}` segments match like a route.
///
/// # Examples
///
/// ```rust
/// use topcoat::router::OriginPolicy;
///
/// let policy = OriginPolicy::new().exempt_paths(["/feed/{*rest}"]);
/// ```
#[must_use]
pub fn exempt_paths<I>(mut self, paths: I) -> Self
where
I: IntoIterator,
I::Item: IntoPath,
{
if let Inner::Verify { exempt_paths, .. } = &mut self.inner {
exempt_paths.extend(paths.into_iter().map(|path| path.into_path().into_owned()));
}
self
}
/// Turns origin verification off entirely.
///
/// Without it, nothing rejects state-changing cross-origin requests or
/// cross-origin WebSocket handshakes; only use this if the application
/// enforces its own defense against cross-site request forgery.
#[must_use]
pub fn dangerous_disable() -> Self {
Self {
inner: Inner::Disabled,
}
}
/// Classifies the current request under this policy.
fn check(&self, cx: &Cx) -> OriginVerdict {
match &self.inner {
Inner::Disabled => OriginVerdict::Allow,
Inner::Verify {
trusted_origins,
exempt_paths,
} => {
let headers = headers(cx);
let header = |name: &str| headers.get(name).and_then(|value| value.to_str().ok());
// Only a request that can do damage needs verification: any
// method beyond the safe ones, and a WebSocket handshake, which
// arrives as a GET but opens a credentialed two-way connection.
let upgrades_to_websocket = header(header::UPGRADE.as_str())
.is_some_and(|value| value.trim().eq_ignore_ascii_case("websocket"));
if !upgrades_to_websocket
&& matches!(method(cx), &Method::GET | &Method::HEAD | &Method::OPTIONS)
{
return OriginVerdict::Allow;
}
// An exempted route handles cross-origin requests itself.
if exempt_paths.iter().any(|path| path.matches(uri(cx).path())) {
return OriginVerdict::Allow;
}
// An explicitly trusted origin passes regardless of how the browser
// classified the request.
let origin = header(header::ORIGIN.as_str());
if origin.is_some_and(|origin| {
trusted_origins
.iter()
.any(|trusted| trusted.eq_ignore_ascii_case(origin))
}) {
return OriginVerdict::Allow;
}
// The "sec-fetch-site" header is the browser's way to declare that the request
// comes from the same origin. If so, it is safe.
if let Some(site) = header("sec-fetch-site") {
return if matches!(site, "same-origin" | "none") {
OriginVerdict::Allow
} else {
OriginVerdict::Deny
};
}
// Fallback check for older browsers: Compare the "origin" and "host" header
// manually. If they are the same, this is a safe same-origin
// request.
if let Some(origin) = origin {
let origin = origin.split_once("://").map(|(_, host)| host);
let host = header(header::HOST.as_str())
.or_else(|| uri(cx).authority().map(http::uri::Authority::as_str));
return if let Some(host) = host
&& let Some(origin) = origin
&& origin.eq_ignore_ascii_case(host)
{
OriginVerdict::Allow
} else {
OriginVerdict::Deny
};
}
// The request had neither an "origin" nor a "sec-fetch-site" header.
// We assume it comes from a non-browser client and allow the request.
OriginVerdict::Allow
}
}
}
}
/// Verifies with no trusted cross-origin peers and no exempt paths.
impl Default for OriginPolicy {
fn default() -> Self {
Self::new()
}
}
/// The policy's decision about a request's origin.
#[derive(Debug, PartialEq, Eq)]
enum OriginVerdict {
/// The request passes.
Allow,
/// A dangerous request from an untrusted origin.
Deny,
}
/// A [`Layer`] enforcing an [`OriginPolicy`].
///
/// The router wraps one around every request as its outermost step, built
/// from the policy registered with
/// [`RouterBuilder::origin_policy`](crate::RouterBuilder::origin_policy). A
/// request the policy denies is rejected with `403 Forbidden` before any
/// inner layer or route runs.
#[derive(Debug)]
pub struct OriginLayer {
policy: OriginPolicy,
}
impl OriginLayer {
/// Creates a layer enforcing `policy`.
#[must_use]
pub fn new(policy: OriginPolicy) -> Self {
Self { policy }
}
}
impl Layer for OriginLayer {
fn path(&self) -> Option<&Path> {
None
}
fn handle<'a>(&'a self, cx: &'a Cx, body: Body, next: Next<'a>) -> LayerFuture<'a> {
match self.policy.check(cx) {
OriginVerdict::Allow => next.run(cx, body),
OriginVerdict::Deny => Box::pin(async { Err(forbidden().into()) }),
}
}
}
#[cfg(test)]
mod tests {
use http::Request;
use topcoat_core::context::CxTestBuilder;
use super::*;
/// Builds a `Cx` for a request with the given method, URL, and headers.
fn cx_with(method: Method, uri: &str, headers: &[(&str, &str)]) -> Cx {
let mut builder = Request::builder().method(method).uri(uri);
for (name, value) in headers {
builder = builder.header(*name, *value);
}
let (parts, ()) = builder.body(()).expect("request should build").into_parts();
CxTestBuilder::new().request_context(parts).build()
}
fn check(policy: &OriginPolicy, method: Method, headers: &[(&str, &str)]) -> OriginVerdict {
policy.check(&cx_with(method, "/x", headers))
}
// -- default policy --
#[test]
fn non_browser_requests_pass() {
// Neither an "origin" nor a "sec-fetch-site" header.
let policy = OriginPolicy::new();
assert_eq!(check(&policy, Method::POST, &[]), OriginVerdict::Allow);
}
#[test]
fn same_origin_requests_pass() {
let policy = OriginPolicy::new();
for site in ["same-origin", "none"] {
assert_eq!(
check(&policy, Method::POST, &[("sec-fetch-site", site)]),
OriginVerdict::Allow
);
}
}
#[test]
fn cross_origin_safe_methods_pass() {
let policy = OriginPolicy::new();
for method in [Method::GET, Method::HEAD, Method::OPTIONS] {
assert_eq!(
check(&policy, method, &[("sec-fetch-site", "cross-site")]),
OriginVerdict::Allow
);
}
}
#[test]
fn cross_origin_unsafe_methods_are_denied() {
let policy = OriginPolicy::new();
for method in [Method::POST, Method::PUT, Method::DELETE, Method::PATCH] {
assert_eq!(
check(&policy, method, &[("sec-fetch-site", "cross-site")]),
OriginVerdict::Deny
);
}
}
#[test]
fn cross_origin_websocket_upgrades_are_denied() {
// The handshake is a GET, but upgrading makes it dangerous.
let policy = OriginPolicy::new();
assert_eq!(
check(
&policy,
Method::GET,
&[("sec-fetch-site", "cross-site"), ("upgrade", "WebSocket")]
),
OriginVerdict::Deny
);
}
#[test]
fn same_origin_websocket_upgrades_pass() {
let policy = OriginPolicy::new();
assert_eq!(
check(
&policy,
Method::GET,
&[("sec-fetch-site", "same-origin"), ("upgrade", "websocket")]
),
OriginVerdict::Allow
);
}
// -- the origin/host fallback for older browsers --
#[test]
fn matching_origin_and_host_pass() {
let policy = OriginPolicy::new();
assert_eq!(
check(
&policy,
Method::POST,
&[("origin", "https://example.com"), ("host", "EXAMPLE.com")]
),
OriginVerdict::Allow
);
}
#[test]
fn mismatched_origin_and_host_are_denied_for_unsafe_methods() {
let policy = OriginPolicy::new();
let headers = [("origin", "https://evil.example"), ("host", "example.com")];
assert_eq!(check(&policy, Method::POST, &headers), OriginVerdict::Deny);
assert_eq!(check(&policy, Method::GET, &headers), OriginVerdict::Allow);
}
// -- trusted origins --
#[test]
fn trusted_origins_pass() {
let policy = OriginPolicy::new().trust_origins(["https://accounts.example.com"]);
assert_eq!(
check(
&policy,
Method::POST,
&[
("origin", "https://ACCOUNTS.example.com"),
("sec-fetch-site", "cross-site"),
]
),
OriginVerdict::Allow
);
assert_eq!(
check(
&policy,
Method::POST,
&[
("origin", "https://other.example.com"),
("sec-fetch-site", "cross-site"),
]
),
OriginVerdict::Deny
);
}
// -- exempt paths --
#[test]
fn exempt_paths_pass_unchecked() {
let policy = OriginPolicy::new().exempt_paths(["/ws/{id}"]);
let headers = [("sec-fetch-site", "cross-site"), ("upgrade", "websocket")];
assert_eq!(
policy.check(&cx_with(Method::GET, "/ws/42", &headers)),
OriginVerdict::Allow
);
// Other paths are still verified.
assert_eq!(
policy.check(&cx_with(Method::GET, "/other", &headers)),
OriginVerdict::Deny
);
}
// -- disabled policy --
#[test]
fn disabled_policy_passes_everything() {
let policy = OriginPolicy::dangerous_disable();
assert_eq!(
check(
&policy,
Method::POST,
&[("sec-fetch-site", "cross-site"), ("upgrade", "websocket")]
),
OriginVerdict::Allow
);
}
}