Skip to content

Commit

Permalink
Merge pull request #2986 from itowlson/reserved-routes-no-no-no
Browse files Browse the repository at this point in the history
Warn if application uses a reserved route
  • Loading branch information
itowlson authored Jan 22, 2025
2 parents 0817a0e + c725514 commit 29ba553
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crates/http/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ impl Router {
.map(|(_spec, handler)| (&handler.parsed_based_route, &handler.component_id))
}

/// true if one or more routes is under the reserved `/.well-known/spin/*`
/// prefix; otherwise false.
pub fn contains_reserved_route(&self) -> bool {
self.router
.iter()
.any(|(_spec, handker)| handker.based_route.starts_with(crate::WELL_KNOWN_PREFIX))
}

/// This returns the component ID that should handle the given path, or an error
/// if no component matches.
///
Expand Down Expand Up @@ -593,4 +601,30 @@ mod route_tests {
let m = routes.route("/1/2/3").expect("/1/2/3 should have matched");
assert_eq!("2", m.named_wildcards()["two"]);
}

#[test]
fn reserved_routes_are_reserved() {
let (routes, _dups) =
Router::build("/", vec![("comp", &"/.well-known/spin/...".into())]).unwrap();
assert!(routes.contains_reserved_route());

let (routes, _dups) =
Router::build("/", vec![("comp", &"/.well-known/spin/fie".into())]).unwrap();
assert!(routes.contains_reserved_route());
}

#[test]
fn unreserved_routes_are_unreserved() {
let (routes, _dups) =
Router::build("/", vec![("comp", &"/.well-known/spindle/...".into())]).unwrap();
assert!(!routes.contains_reserved_route());

let (routes, _dups) =
Router::build("/", vec![("comp", &"/.well-known/spi/...".into())]).unwrap();
assert!(!routes.contains_reserved_route());

let (routes, _dups) =
Router::build("/", vec![("comp", &"/.well-known/spin".into())]).unwrap();
assert!(!routes.contains_reserved_route());
}
}
6 changes: 6 additions & 0 deletions crates/trigger-http/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ impl<F: RuntimeFactors> HttpServer<F> {
);
}
}
if router.contains_reserved_route() {
tracing::error!(
"Routes under {} are handled by the Spin runtime and will never be reached",
spin_http::WELL_KNOWN_PREFIX
);
}
tracing::trace!(
"Constructed router: {:?}",
router.routes().collect::<Vec<_>>()
Expand Down

0 comments on commit 29ba553

Please sign in to comment.