diff --git a/.github/workflows/ping-tests.yml b/.github/workflows/ping-tests.yml index ea837b5..f6b8e95 100644 --- a/.github/workflows/ping-tests.yml +++ b/.github/workflows/ping-tests.yml @@ -63,3 +63,13 @@ jobs: # Get request with store='storeId' and key=k1 hex=0A0773746F7265496412026B31 curl -f --data-binary "$(echo "$hex" | xxd -r -p)" http://localhost:8080/vss/getObject + + # A base URL ending in `/` yields a doubled slash after the prefix; this must still + # route to the endpoint rather than 400. + curl -f --data-binary "$(echo "$hex" | xxd -r -p)" http://localhost:8080/vss//getObject + + # A trailing slash on the endpoint must be tolerated as well. + curl -f --data-binary "$(echo "$hex" | xxd -r -p)" http://localhost:8080/vss/getObject/ + + # Tolerating slashes must not turn every path into a match: an unknown path still 400s. + test "$(curl -s -o /dev/null -w '%{http_code}' http://localhost:8080/vss/nope)" = 400 diff --git a/server/src/vss_service.rs b/server/src/vss_service.rs index eca7d2c..031729e 100644 --- a/server/src/vss_service.rs +++ b/server/src/vss_service.rs @@ -67,6 +67,22 @@ impl VssService { pub(crate) const BASE_PATH_PREFIX: &str = "/vss"; +/// Collapses runs of `/` in `path` into a single `/`, and drops one trailing `/`. +fn normalize_path(path: &str) -> String { + let mut normalized = String::with_capacity(path.len()); + for c in path.chars() { + if c == '/' && normalized.ends_with('/') { + continue; + } + normalized.push(c); + } + + if normalized.len() > 1 && normalized.ends_with('/') { + normalized.pop(); + } + normalized +} + impl Service> for VssService { type Response = Response>; type Error = hyper::Error; @@ -75,7 +91,7 @@ impl Service> for VssService { fn call(&self, req: Request) -> Self::Future { let store = Arc::clone(&self.store); let authorizer = Arc::clone(&self.authorizer); - let path = req.uri().path().to_owned(); + let path = normalize_path(req.uri().path()); let maximum_request_body_size = self.config.maximum_request_body_size; Box::pin(async move { @@ -323,4 +339,66 @@ mod tests { PROTOCOL_VERSION, ); } + + #[test] + fn normalize_path_collapses_repeated_slashes_and_drops_trailing_slash() { + let cases = [ + // Already canonical: must come out untouched. + ("/vss/getObject", "/vss/getObject"), + ("/vss/putObjects", "/vss/putObjects"), + ("/vss/deleteObject", "/vss/deleteObject"), + ("/vss/listKeyVersions", "/vss/listKeyVersions"), + // A base URL ending in `/` puts a doubled slash after the prefix. + ("/vss//getObject", "/vss/getObject"), + ("/vss//putObjects", "/vss/putObjects"), + ("/vss//deleteObject", "/vss/deleteObject"), + ("/vss//listKeyVersions", "/vss/listKeyVersions"), + // Longer runs, trailing slashes, and both together. + ("/vss///getObject", "/vss/getObject"), + ("/vss/getObject/", "/vss/getObject"), + ("/vss//getObject//", "/vss/getObject"), + ("//vss/getObject", "/vss/getObject"), + // Collapsing applies anywhere in the path, but a `.`/`..` segment is left alone + ("//vss/../../metrics", "/vss/../../metrics"), + // `/metrics` is dispatched before the authorizer runs, so it has to tolerate the same variants. + ("/metrics", "/metrics"), + ("//metrics", "/metrics"), + ("/metrics/", "/metrics"), + ("///metrics//", "/metrics"), + // The bare prefix collapses to "/vss", which is not an endpoint and must still 400. + ("/vss", "/vss"), + ("/vss/", "/vss"), + ("/vss//", "/vss"), + // Trimming must not turn the root into the empty string, and the empty path stays empty rather than gaining a slash. + ("/", "/"), + ("//", "/"), + ("///", "/"), + ("", ""), + // Asterisk-form (`OPTIONS *`) is not a slash variant of anything. + ("*", "*"), + // Collapsing applies anywhere, but a path that merely shares a character prefix with `/vss` is left alone. + ("/vssx/getObject", "/vssx/getObject"), + ("/vssx//getObject", "/vssx/getObject"), + ]; + + for (input, expected) in cases { + assert_eq!(normalize_path(input), expected, "input {input:?}"); + } + } + + #[test] + fn normalize_path_does_not_resolve_dot_segments_or_percent_decode() { + let cases = [ + ("/vss/../metrics", "/vss/../metrics"), + ("/vss/./getObject", "/vss/./getObject"), + // Percent-encoded separators and dot segments stay encoded, so they keep failing to match rather than being decoded into a valid route. + ("/vss/%2FgetObject", "/vss/%2FgetObject"), + ("/vss/%2e%2e/metrics", "/vss/%2e%2e/metrics"), + ("/vss/getObject%2F", "/vss/getObject%2F"), + ]; + + for (input, expected) in cases { + assert_eq!(normalize_path(input), expected, "input {input:?}"); + } + } }