use crate::route::RouteTable;
pub fn ambiguous_route(table: &RouteTable, raw_path: &str) -> bool {
let norm = crate::route::normalize(raw_path);
if !(suspicious(raw_path) || suspicious(&norm)) {
return false;
}
fn pick<'t>(t: &'t RouteTable, p: &str) -> Option<&'t str> {
t.lookup(p).map(|r| r.prefix.as_str())
}
let canon = pick(table, raw_path);
let ambiguous = [raw_path, &*norm].into_iter().any(|s| {
let stripped = strip_params(s);
[
flatten(s),
strip_params(&flatten(s)),
stripped.clone(),
flatten(&stripped),
]
.iter()
.any(|r| pick(table, r) != canon)
});
ambiguous
}
fn suspicious(s: &str) -> bool {
let b = s.as_bytes();
b.iter().enumerate().any(|(i, &c)| match c {
b'\\' | b';' => true,
b'%' => {
matches!(
b.get(i + 1..i + 3),
Some([b'2', b'f' | b'F'] | [b'5', b'c' | b'C'])
) || matches!(
b.get(i + 1..i + 5),
Some([b'2', b'5', b'2', b'f' | b'F'] | [b'2', b'5', b'5', b'c' | b'C'])
)
}
_ => false,
})
}
fn flatten(s: &str) -> String {
let b = s.as_bytes();
let mut out = String::with_capacity(s.len());
let mut i = 0;
while i < b.len() {
match b[i..] {
[b'%', b'2', b'5', b'2', b'f' | b'F', ..]
| [b'%', b'2', b'5', b'5', b'c' | b'C', ..] => {
out.push('/');
i += 5;
}
[b'%', b'2', b'f' | b'F', ..] | [b'%', b'5', b'c' | b'C', ..] => {
out.push('/');
i += 3;
}
[b'\\', ..] => {
out.push('/');
i += 1;
}
_ => {
let n = s[i..].chars().next().map_or(1, char::len_utf8);
out.push_str(&s[i..i + n]);
i += n;
}
}
}
out
}
fn strip_params(s: &str) -> String {
s.split('/')
.map(|seg| seg.split(';').next().unwrap_or(""))
.collect::<Vec<_>>()
.join("/")
}
pub fn bad_path(path: &str) -> bool {
let b = path.as_bytes();
let (mut start, mut i) = (0, 0);
while i <= b.len() {
let sep = match b[i..] {
[] => Some(0),
[b'/' | b'\\', ..] => Some(1),
[b'%', b'2', b'f' | b'F', ..] | [b'%', b'5', b'c' | b'C', ..] => Some(3),
[b'%', b'0', b'0', ..] | [b'%', b'u' | b'U', ..] => return true,
[b'%', b'2', b'5', b'2', b'e' | b'E' | b'f' | b'F', ..]
| [b'%', b'2', b'5', b'5', b'c' | b'C', ..] => return true,
_ => None,
};
match sep {
Some(n) => {
if dot_piece(&b[start..i]) {
return true;
}
i += n.max(1);
start = i;
}
None => i += 1,
}
}
false
}
pub fn bad_path_normalized(raw: &str) -> bool {
bad_path(raw) || (raw.contains('%') && bad_path(&crate::route::normalize(raw)))
}
fn dot_piece(piece: &[u8]) -> bool {
let end = piece.iter().position(|&c| c == b';').unwrap_or(piece.len());
let b = &piece[..end];
let (mut i, mut dots) = (0, 0);
while i < b.len() {
match b[i..] {
[b'.', ..] => i += 1,
[b'%', b'2', b'e' | b'E', ..] => i += 3,
_ => return false,
}
dots += 1;
}
matches!(dots, 1 | 2)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::route::{Route, Upstream};
use std::time::Duration;
#[test]
fn mixed_encodings_hiding_dot_segments_are_rejected() {
for p in ["/api/%2%65%2%65/x", "/api/%%32%65%%32%65/x", "/api/%2%65/x"] {
assert!(!bad_path(p), "{p} is invisible to the raw check");
assert!(bad_path_normalized(p), "{p}");
}
for p in ["/api/x", "/api/a%2eb", "/api/%41", "/api/%zz"] {
assert!(!bad_path_normalized(p), "{p}");
}
}
#[test]
fn dot_segments_are_rejected() {
for p in [
"/api/../admin",
"/api/%2e%2e/admin",
"/api/..%2fadmin",
"/api/./../admin",
"/api/..",
"/api/%2E%2E/x",
"/api/%2e%2E/x",
"/api/.%2e/x",
"/api/..;/admin",
"/api/.;x/y",
"/api/%2e%2e;/x",
"/api/..%5cx",
"/api/..%5Cx",
"/api/a\\..\\b",
"/api/%2e/x",
"/..",
"/api/a%2f..",
"/api/%2e%2e%2f",
"/api/..%00",
"/api/.%00.",
"/api/%u002e%u002e",
"/api/%U002e",
"/api/%252e%252e",
"/api/%252E",
"/api/%252f",
"/api/%252F",
"/api/%255c",
"/api/a%00b",
"/api/a%5c..",
"/api/a%5C..%5Cb",
] {
assert!(bad_path(p), "{p}");
}
}
#[test]
fn legitimate_paths_are_allowed() {
for p in [
"/a..b/",
"/.well-known/acme",
"/file.tar.gz",
"/api/v1.2/x",
"/",
"/api/...",
"/api/a;..",
"/api/%2e%2e%2e/x",
"/api/%41/x",
"/api/.a/x",
"/api/a%2/",
"/api/v4/projects/group%2Fproject",
"/api/queues/%2F/q",
"/@scope%2fpkg",
"/%2F",
"/api/%25",
"/api/100%25",
"/api/%c0%ae",
] {
assert!(!bad_path(p), "{p}");
}
}
fn table(prefixes: &[&str]) -> RouteTable {
let up =
Upstream::new("http://localhost:8001".parse().unwrap(), Default::default()).unwrap();
let routes = prefixes
.iter()
.map(|p| Route::new(*p, up.clone()))
.collect();
RouteTable::new(routes, Duration::from_secs(30))
}
#[test]
fn ambiguous_paths_are_flagged() {
let t = table(&["/api", "/"]);
for p in [
"/api%2Fsecret",
"/api%2fsecret",
"/api%5Csecret",
"/api%5csecret",
"/api\\secret",
"/api;x/secret",
"/api;x",
"/api%2%46secret",
"/api%25%32%46secret",
] {
assert!(ambiguous_route(&t, p), "{p}");
}
assert!(ambiguous_route(&table(&["/api"]), "/api%2Fx"));
let t = table(&["/", "/admin"]);
for p in ["/;%2Fx/admin", "/;x%2Fy/admin", "/;%5Cz/admin"] {
assert!(ambiguous_route(&t, p), "{p}");
}
assert!(ambiguous_route(&table(&["/", "/a", "/a/b"]), "/a/;x%2Fq/b"));
}
#[test]
fn unambiguous_paths_are_allowed() {
let t = table(&["/api", "/"]);
for p in [
"/api/v4/projects/group%2Fproject",
"/api/secret",
"/other/a%2Fb;c",
"/api/a;x/b",
"/api/x;jsessionid=1",
"/",
] {
assert!(!ambiguous_route(&t, p), "{p}");
}
assert!(!ambiguous_route(&table(&["/"]), "/files/a%2Fb"));
}
#[test]
fn exhaustive_short_paths_match_reference_model() {
fn flat(s: &str) -> String {
["%2F", "%2f", "%5C", "%5c", "\\"]
.iter()
.fold(s.to_string(), |s, k| s.replace(k, "/"))
}
fn strip(s: &str) -> String {
s.split('/')
.map(|g| g.split(';').next().unwrap())
.collect::<Vec<_>>()
.join("/")
}
let toks = ["a", "b", "/", ";", "%2F", "x", "\\"];
for routes in [
&["/", "/a", "/a/b", "/b"][..],
&["/a", "/a/b"][..],
&["/a/", "/a/b/", "/"][..],
] {
let t = table(routes);
let pre = |p: &str| t.lookup(p).map(|r| r.prefix.clone());
let mut stack = vec![("/".to_string(), 0)];
while let Some((p, d)) = stack.pop() {
if d < 4 {
stack.extend(toks.iter().map(|k| (format!("{p}{k}"), d + 1)));
}
if bad_path(&p) {
continue;
}
let c = pre(&p);
let differs = [strip(&flat(&p)), flat(&strip(&p)), flat(&p), strip(&p)]
.iter()
.any(|r| pre(r) != c);
assert_eq!(ambiguous_route(&t, &p), differs, "{routes:?} {p}");
}
}
}
}