[core] Share paths and errors: refuse overlong UTF-8, %u, look-alike dots and slashes and trimmed dot segments; never repeat URL credentials in an error
This commit is contained in:
@@ -397,11 +397,25 @@ pub fn check_prefix(prefix: &str) -> Result<String, &'static str> {
|
||||
Ok(trimmed.to_owned())
|
||||
}
|
||||
|
||||
/// Percent-encodings no plain path needs, which some upstream reads as `.`, `/` or `\`
|
||||
/// (lowercased before matching):
|
||||
/// - `.`, `/`, `\`, `%` itself (double encoding) and NUL;
|
||||
/// - IIS `%u` escapes;
|
||||
/// - overlong UTF-8 (`%c0`, `%c1`, `%e0%80`–`%e0%9f`, `%f0%80`–`%f0%8f`) and bytes that never
|
||||
/// start UTF-8 (`%f5`–`%ff`);
|
||||
/// - fullwidth and look-alike dots and slashes that NFKC turns into `.` and `/`: U+FF0E,
|
||||
/// U+FF0F, U+FF3C, U+FE52, U+2024, U+2215, U+2044, U+29F8.
|
||||
pub const REFUSED_ENCODINGS: &[&str] = &[
|
||||
"%2e", "%2f", "%5c", "%25", "%00", "%u", "%c0", "%c1", "%e0%8", "%e0%9", "%f0%8", "%f5",
|
||||
"%f6", "%f7", "%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff", "%ef%bc%8e",
|
||||
"%ef%bc%8f", "%ef%bc%bc", "%ef%b9%92", "%e2%80%a4", "%e2%88%95", "%e2%81%84", "%e2%a7%b8",
|
||||
];
|
||||
|
||||
/// Is `raw_path` (the request path exactly as sent, still percent-encoded) under `prefix`?
|
||||
///
|
||||
/// Refused outright, whatever the prefix: anything but visible ASCII, a backslash, an empty
|
||||
/// segment (`//`), a dot segment (`.`, `..`, also with `;params`), and the percent-encoded
|
||||
/// forms of `.`, `/`, `\`, `%` and NUL. Other paths match on whole segments, case-sensitively:
|
||||
/// segment (`//`), a dot segment (`.`, `..`, also with `;params`, `%3b`, or trailing `%20` and
|
||||
/// `%09`), and every encoding in [`REFUSED_ENCODINGS`]. Other paths match on whole segments, case-sensitively:
|
||||
/// prefix `/a` covers `/a` and `/a/x`, not `/ab` or `/A`.
|
||||
pub fn path_within(prefix: &str, raw_path: &str) -> bool {
|
||||
if !raw_path.starts_with('/')
|
||||
@@ -411,19 +425,23 @@ pub fn path_within(prefix: &str, raw_path: &str) -> bool {
|
||||
return false;
|
||||
}
|
||||
let lower = raw_path.to_ascii_lowercase();
|
||||
if ["%2e", "%2f", "%5c", "%25", "%00"]
|
||||
.iter()
|
||||
.any(|bad| lower.contains(bad))
|
||||
{
|
||||
if REFUSED_ENCODINGS.iter().any(|bad| lower.contains(bad)) {
|
||||
return false;
|
||||
}
|
||||
// An empty segment anywhere but at the end (a trailing slash) is `//`.
|
||||
if raw_path.contains("//") {
|
||||
return false;
|
||||
}
|
||||
for seg in raw_path[1..].split('/') {
|
||||
// `..;x` is `..` to Tomcat and friends.
|
||||
let name = seg.split(';').next().unwrap_or(seg);
|
||||
for seg in lower[1..].split('/') {
|
||||
// `..;x` is `..` to Tomcat and friends; `.. ` is `..` to Windows, which trims
|
||||
// trailing spaces; `..%3b` is `..;` to servers that decode before splitting.
|
||||
let name = seg
|
||||
.replace("%3b", ";")
|
||||
.split(';')
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.replace("%20", "")
|
||||
.replace("%09", "");
|
||||
if !name.is_empty() && name.bytes().all(|c| c == b'.') {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -406,14 +406,14 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
|
||||
} else {
|
||||
errors.push(E::DuplicateUpstream {
|
||||
file: f(),
|
||||
url: u.clone(),
|
||||
url: shown_url(u),
|
||||
index,
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(reason) => errors.push(E::BadUpstreamUrl {
|
||||
file: f(),
|
||||
url: u.clone(),
|
||||
url: shown_url(u),
|
||||
reason,
|
||||
index: Some(index),
|
||||
}),
|
||||
@@ -423,7 +423,12 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
|
||||
if upstreams.windows(2).any(|w| w[0].scheme != w[1].scheme) {
|
||||
errors.push(E::BadUpstreamUrl {
|
||||
file: f(),
|
||||
url: raw.upstreams.join(", "),
|
||||
url: raw
|
||||
.upstreams
|
||||
.iter()
|
||||
.map(|u| shown_url(u))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", "),
|
||||
reason: r::URL_MIXED,
|
||||
index: None,
|
||||
});
|
||||
@@ -768,3 +773,31 @@ fn parse_url(raw: &str, allow_path: bool) -> Result<(Upstream, String), &'static
|
||||
}
|
||||
Ok((Upstream { scheme, host, port }, path))
|
||||
}
|
||||
|
||||
/// A URL as an error may show it: a user name and password (`user:pass@`) are replaced by
|
||||
/// `…@`, so a credential typed into an upstream never reaches a message, a log or the API.
|
||||
pub(crate) fn shown_url(raw: &str) -> String {
|
||||
let (scheme, rest) = match raw.find("://") {
|
||||
Some(i) => (&raw[..i + 3], &raw[i + 3..]),
|
||||
None => ("", raw),
|
||||
};
|
||||
let end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
|
||||
let (authority, tail) = rest.split_at(end);
|
||||
match authority.rfind('@') {
|
||||
Some(at) => format!("{scheme}\u{2026}@{}{tail}", &authority[at + 1..]),
|
||||
None => raw.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod shown_tests {
|
||||
use super::shown_url;
|
||||
|
||||
#[test]
|
||||
fn userinfo_is_hidden() {
|
||||
assert_eq!(shown_url("http://user:hunter2@10.0.0.1:80/x"), "http://\u{2026}@10.0.0.1:80/x");
|
||||
assert_eq!(shown_url("user:hunter2@host:80"), "\u{2026}@host:80");
|
||||
assert_eq!(shown_url("http://10.0.0.1:80"), "http://10.0.0.1:80");
|
||||
assert_eq!(shown_url("http://h/a@b"), "http://h/a@b");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,3 +238,17 @@ fn params_carry_suggestions() {
|
||||
assert_eq!(e.params()["suggestion"], "http://10.0.0.2:3000");
|
||||
assert_eq!(e.params()["url"], "http://10.0.0.2:3000/app");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_password_in_an_upstream_url_is_never_repeated() {
|
||||
let errs = errors_for(
|
||||
"hostname = \"app.test\"\nupstreams = [\"http://admin:hunter2@10.0.0.2:3000\", \"ftp://u:hunter2@x\", \"http://admin:hunter2@10.0.0.2:3000\"]\n",
|
||||
);
|
||||
assert!(!errs.is_empty());
|
||||
for e in &errs {
|
||||
let all = format!("{e} {:?} {:?}", e.params(), e.field());
|
||||
assert!(!all.contains("hunter2"), "{all}");
|
||||
}
|
||||
let e = errs.iter().find(|e| e.code() == "host.upstream.credentials").unwrap();
|
||||
assert_eq!(e.params()["url"], "http://\u{2026}@10.0.0.2:3000");
|
||||
}
|
||||
|
||||
@@ -422,7 +422,9 @@ fn rejects_bad_upstream_urls() {
|
||||
] {
|
||||
let e = single_error(&format!("hostname = \"app.test\"\nupstreams = [{bad:?}]\n"));
|
||||
assert!(
|
||||
matches!(e, ValidationError::BadUpstreamUrl { ref url, .. } if url == bad),
|
||||
// Credentials are never repeated in an error.
|
||||
matches!(e, ValidationError::BadUpstreamUrl { ref url, .. }
|
||||
if url == bad || (bad.contains('@') && url == "http://\u{2026}@127.0.0.1:8080")),
|
||||
"upstream {bad:?} gave {e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -352,6 +352,9 @@ fn paths_under_the_prefix_are_within() {
|
||||
("/a/b", "/a/b/c"),
|
||||
("/a", "/a/b;v=1"),
|
||||
("/a", "/a/.well"),
|
||||
("/a", "/a/%C3%A9t%C3%A9.jpg"),
|
||||
("/a", "/a/%e2%9c%93"),
|
||||
("/a", "/a/a%20b"),
|
||||
] {
|
||||
assert!(path_within(prefix, path), "{prefix} should cover {path}");
|
||||
}
|
||||
@@ -395,6 +398,28 @@ fn no_path_trick_escapes_the_prefix() {
|
||||
("/", "/../etc/passwd"),
|
||||
("/", "//evil"),
|
||||
("/", "/%2e%2e/x"),
|
||||
// QC fix round 1: overlong UTF-8, IIS %u, fullwidth dots, Windows-trimmed and
|
||||
// encoded-semicolon dot segments.
|
||||
("/a", "/a/%c0%ae%c0%ae/b"),
|
||||
("/a", "/a/%C0%AE%C0%AE/secret"),
|
||||
("/a", "/a/..%c0%afb"),
|
||||
("/a", "/a/%e0%80%ae%e0%80%ae/b"),
|
||||
("/a", "/a/%f0%80%80%ae/b"),
|
||||
("/a", "/a/%u002e%u002e/b"),
|
||||
("/a", "/a/%u002E%u002E/b"),
|
||||
("/a", "/a/..%u2215b"),
|
||||
("/a", "/a/%ef%bc%8e%ef%bc%8e/b"),
|
||||
("/a", "/a/..%ef%bc%8fb"),
|
||||
("/a", "/a/..%e2%88%95b"),
|
||||
("/a", "/a/..%20/b"),
|
||||
("/a", "/a/.%20/b"),
|
||||
("/a", "/a/..%09/b"),
|
||||
("/a", "/a/..%3b/b"),
|
||||
("/a", "/a/..%3B/b"),
|
||||
("/a", "/a/..%3bx/b"),
|
||||
("/a", "/a/.;x/b"),
|
||||
("/a", "/a;/../b"),
|
||||
("/a", "/a/%2E./b"),
|
||||
];
|
||||
for (prefix, path) in cases {
|
||||
assert!(
|
||||
|
||||
Reference in New Issue
Block a user