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
use http::Uri;
use super::ConfigError;
/// Internal extension methods on [`http::Uri`] used by the CSRF middleware to
/// validate trusted-origin strings.
pub(crate) trait UriExt: Sized {
/// Parses a trusted-origin string of the form `scheme://host[:port]`.
///
/// Rejects inputs that can't represent a browser `Origin`:
///
/// - unparseable URIs ([`ConfigError::InvalidOriginUrl`]);
/// - non-`http`/`https` schemes or missing host ([`ConfigError::OpaqueOrigin`]);
/// - any path, query, or fragment component
/// ([`ConfigError::InvalidOriginUrlComponents`] — including a bare trailing
/// `/` and fragments that `http::Uri` would otherwise silently strip);
/// - non-ASCII hostnames ([`ConfigError::NonAsciiHostname`] — IDN hosts
/// must be supplied in punycode, since that's what browsers send).
///
/// The returned [`Uri`] is parsed but not normalized; the origin is matched
/// against the request's `Origin` header byte-for-byte.
fn parse_origin(input: &str) -> Result<Self, ConfigError>;
}
impl UriExt for Uri {
fn parse_origin(input: &str) -> Result<Self, ConfigError> {
if input.contains('#') {
return Err(ConfigError::InvalidOriginUrlComponents {
origin: input.to_owned(),
});
}
// browsers will send punycode anyways
if !input.is_ascii() {
return Err(ConfigError::NonAsciiHostname {
origin: input.to_owned(),
});
}
let uri: Uri =
input
.parse()
.map_err(|e: http::uri::InvalidUri| ConfigError::InvalidOriginUrl {
origin: input.to_owned(),
message: e.to_string(),
})?;
if !matches!(uri.scheme_str(), Some("http" | "https"))
|| uri.host().map_or(true, |h| h.is_empty())
{
return Err(ConfigError::OpaqueOrigin {
origin: input.to_owned(),
});
}
// Reject any path/query (fragments are rejected above). `http::Uri`
// reports `path()` as "/" for both `scheme://host` and `scheme://host/`,
// so detect a path from the raw input (everything after "://") to reach
// parity with Go, which rejects a non-empty path — including a bare "/".
let after_scheme = input.split_once("://").map_or("", |(_, rest)| rest);
if after_scheme.contains('/') || uri.query().is_some() {
return Err(ConfigError::InvalidOriginUrlComponents {
origin: input.to_owned(),
});
}
Ok(uri)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_origin_accepts() {
for input in [
"https://example.com",
"http://example.com",
"https://example.com:8443",
"HTTPS://Example.COM",
] {
assert!(
Uri::parse_origin(input).is_ok(),
"expected Ok for {input:?}, got {:?}",
Uri::parse_origin(input)
);
}
}
#[test]
fn test_parse_origin_rejects() {
// Each row maps an input to the expected ConfigError variant.
// Marker functions over closures because PartialEq on the enum already
// makes equality the easy assertion shape.
type Check = fn(&ConfigError) -> bool;
let cases: &[(&str, Check)] = &[
// http::Uri rejects these outright at parse time.
("not a valid url", |e| {
matches!(e, ConfigError::InvalidOriginUrl { .. })
}),
("https://", |e| {
matches!(e, ConfigError::InvalidOriginUrl { .. })
}),
("file:///", |e| {
matches!(e, ConfigError::InvalidOriginUrl { .. })
}),
// Parse OK but scheme is not http/https (or absent).
("example.com", |e| {
matches!(e, ConfigError::OpaqueOrigin { .. })
}),
("file://host/path", |e| {
matches!(e, ConfigError::OpaqueOrigin { .. })
}),
("mailto:x@y.z", |e| {
matches!(e, ConfigError::OpaqueOrigin { .. })
}),
("javascript:alert(1)", |e| {
matches!(e, ConfigError::OpaqueOrigin { .. })
}),
// Path/query/fragment not allowed on a trusted origin. A bare
// trailing slash is a (non-empty) path too — rejected, matching Go.
("https://example.com/", |e| {
matches!(e, ConfigError::InvalidOriginUrlComponents { .. })
}),
("https://example.com/path", |e| {
matches!(e, ConfigError::InvalidOriginUrlComponents { .. })
}),
("https://example.com/path?query=value", |e| {
matches!(e, ConfigError::InvalidOriginUrlComponents { .. })
}),
("https://example.com/path#fragment", |e| {
matches!(e, ConfigError::InvalidOriginUrlComponents { .. })
}),
// http::Uri silently strips fragments; the `contains('#')` pre-check
// surfaces these as component errors instead of letting them slip in.
("https://example.com#fragment", |e| {
matches!(e, ConfigError::InvalidOriginUrlComponents { .. })
}),
("https://example.com/#fragment", |e| {
matches!(e, ConfigError::InvalidOriginUrlComponents { .. })
}),
// IDN hosts must be supplied in punycode.
("https://ümlaut.de", |e| {
matches!(e, ConfigError::NonAsciiHostname { .. })
}),
("https://日本.jp", |e| {
matches!(e, ConfigError::NonAsciiHostname { .. })
}),
];
for (input, predicate) in cases {
match Uri::parse_origin(input) {
Err(e) if predicate(&e) => {}
other => panic!("unexpected result for {:?}: {:?}", input, other),
}
}
}
}