Skip to content

Commit 29ba553

Browse files
authored
Merge pull request #2986 from itowlson/reserved-routes-no-no-no
Warn if application uses a reserved route
2 parents 0817a0e + c725514 commit 29ba553

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

crates/http/src/routes.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ impl Router {
141141
.map(|(_spec, handler)| (&handler.parsed_based_route, &handler.component_id))
142142
}
143143

144+
/// true if one or more routes is under the reserved `/.well-known/spin/*`
145+
/// prefix; otherwise false.
146+
pub fn contains_reserved_route(&self) -> bool {
147+
self.router
148+
.iter()
149+
.any(|(_spec, handker)| handker.based_route.starts_with(crate::WELL_KNOWN_PREFIX))
150+
}
151+
144152
/// This returns the component ID that should handle the given path, or an error
145153
/// if no component matches.
146154
///
@@ -593,4 +601,30 @@ mod route_tests {
593601
let m = routes.route("/1/2/3").expect("/1/2/3 should have matched");
594602
assert_eq!("2", m.named_wildcards()["two"]);
595603
}
604+
605+
#[test]
606+
fn reserved_routes_are_reserved() {
607+
let (routes, _dups) =
608+
Router::build("/", vec![("comp", &"/.well-known/spin/...".into())]).unwrap();
609+
assert!(routes.contains_reserved_route());
610+
611+
let (routes, _dups) =
612+
Router::build("/", vec![("comp", &"/.well-known/spin/fie".into())]).unwrap();
613+
assert!(routes.contains_reserved_route());
614+
}
615+
616+
#[test]
617+
fn unreserved_routes_are_unreserved() {
618+
let (routes, _dups) =
619+
Router::build("/", vec![("comp", &"/.well-known/spindle/...".into())]).unwrap();
620+
assert!(!routes.contains_reserved_route());
621+
622+
let (routes, _dups) =
623+
Router::build("/", vec![("comp", &"/.well-known/spi/...".into())]).unwrap();
624+
assert!(!routes.contains_reserved_route());
625+
626+
let (routes, _dups) =
627+
Router::build("/", vec![("comp", &"/.well-known/spin".into())]).unwrap();
628+
assert!(!routes.contains_reserved_route());
629+
}
596630
}

crates/trigger-http/src/server.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ impl<F: RuntimeFactors> HttpServer<F> {
9090
);
9191
}
9292
}
93+
if router.contains_reserved_route() {
94+
tracing::error!(
95+
"Routes under {} are handled by the Spin runtime and will never be reached",
96+
spin_http::WELL_KNOWN_PREFIX
97+
);
98+
}
9399
tracing::trace!(
94100
"Constructed router: {:?}",
95101
router.routes().collect::<Vec<_>>()

0 commit comments

Comments
 (0)