From b4c5177540df4db73f49b5cd13c20f1ba7959a8a Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 01:16:37 -0600 Subject: [PATCH] fix(id): validate [interfaces.web] route/socket before axum registration panics (#4779) web_interface_from_app_toml now rejects route/socket values missing a leading slash, both sides naming the same path, and either side colliding with a host-registered route, with named WebInterfaceError variants instead of a router-construction panic. Adds regression tests for the five verified panic cases plus the valid control. --- pkgs/id/src/web/app_interface.rs | 222 ++++++++++++++++++++++++++++++- 1 file changed, 219 insertions(+), 3 deletions(-) diff --git a/pkgs/id/src/web/app_interface.rs b/pkgs/id/src/web/app_interface.rs index 83e3a8b097..af2f90845d 100644 --- a/pkgs/id/src/web/app_interface.rs +++ b/pkgs/id/src/web/app_interface.rs @@ -78,6 +78,76 @@ pub enum WebInterfaceError { Toml(toml::de::Error), /// `mode` named something other than `adapter` or `direct`. UnknownMode(String), + /// `route` does not start with `/` (axum panics on such paths). + RouteMustStartWithSlash(String), + /// `socket` does not start with `/` (axum panics on such paths). + SocketMustStartWithSlash(String), + /// `route` collides with a host-registered route, so binding it + /// would panic router construction with a duplicate route. + RouteReserved(String), + /// `socket` collides with a host-registered route, so binding it + /// would panic router construction with a duplicate route. + SocketReserved(String), + /// `route` and `socket` name the same path, so binding both would + /// panic router construction with a duplicate route. + RouteSocketOverlap(String), +} + +/// Paths the host always registers in `create_router_inner` and the +/// shared world HTTP surface merged from `world_http_routes` (page +/// routes, `/api/*`, `/ws/tags`, `/ws/collab/{doc_id}`, the explore +/// and world-bridge routes, and the asset/blob catch-alls). A +/// packaged app binding that reuses any of these would panic router +/// construction — axum rejects duplicate/conflicting routes — so +/// the parser rejects them instead (#4779). Keep this list mirrored +/// with `routes.rs` (`create_router_inner`) and `world_ws.rs` +/// (`world_http_routes`). +pub(crate) const HOST_RESERVED_ROUTES: &[&str] = &[ + "/", + "/files", + "/settings", + "/peers", + "/edit/{*name}", + "/hash/{hash}", + "/view/{*name}", + "/blob/{hash}", + "/api/files", + "/api/home/feed", + "/api/peers", + "/api/save", + "/api/new", + "/api/rename", + "/api/copy", + "/api/download", + "/api/delete", + "/api/restore", + "/api/hard-delete", + "/api/upload", + "/api/images", + "/api/tags", + "/api/tags/search", + "/api/identity/register", + "/api/identity/me", + "/api/identity/name", + "/ws/collab/{doc_id}", + "/ws/tags", + "/assets/{*path}", + "/explore", + "/explore/login", + "/explore/act", + "/invite", + "/api/world/invite", + "/api/world/host-view", + "/api/world/metrics", + "/api/world/directory", +]; + +/// Whether `path` collides with the host's own route table: an exact +/// hit on [`HOST_RESERVED_ROUTES`], or anything on the shared +/// `/api/world/*` surface (which grows with the world bridge, so it +/// is guarded by prefix rather than enumerated). +fn is_host_reserved(path: &str) -> bool { + path == "/api/world" || path.starts_with("/api/world/") || HOST_RESERVED_ROUTES.contains(&path) } impl std::fmt::Display for WebInterfaceError { @@ -90,6 +160,30 @@ impl std::fmt::Display for WebInterfaceError { "[interfaces.web] mode {mode:?} is not \"adapter\" or \"direct\"" ) } + Self::RouteMustStartWithSlash(route) => { + write!(f, "[interfaces.web] route {route:?} must start with '/'") + } + Self::SocketMustStartWithSlash(socket) => { + write!(f, "[interfaces.web] socket {socket:?} must start with '/'") + } + Self::RouteReserved(route) => { + write!( + f, + "[interfaces.web] route {route:?} collides with a host-registered route" + ) + } + Self::SocketReserved(socket) => { + write!( + f, + "[interfaces.web] socket {socket:?} collides with a host-registered route" + ) + } + Self::RouteSocketOverlap(path) => { + write!( + f, + "[interfaces.web] route and socket both bind {path:?}; they must differ" + ) + } } } } @@ -112,8 +206,11 @@ impl From for WebInterfaceError { /// /// # Errors /// -/// Returns [`WebInterfaceError`] when the TOML is malformed or the -/// mode is neither `adapter` nor `direct`. +/// Returns [`WebInterfaceError`] when the TOML is malformed, the mode +/// is neither `adapter` nor `direct`, a route/socket value does not +/// start with `/`, both sides of the binding name the same path, or +/// either side collides with a host-registered route (any of which +/// would panic router construction, #4779). pub fn web_interface_from_app_toml( source: &str, ) -> Result, WebInterfaceError> { @@ -126,9 +223,31 @@ pub fn web_interface_from_app_toml( Some("direct") => WebMode::Direct, Some(other) => return Err(WebInterfaceError::UnknownMode(other.to_owned())), }; + let route = section.route.unwrap_or_else(|| "/world".to_owned()); + if !route.starts_with('/') { + return Err(WebInterfaceError::RouteMustStartWithSlash(route)); + } + if let Some(ref socket) = section.socket + && !socket.starts_with('/') + { + return Err(WebInterfaceError::SocketMustStartWithSlash(socket.clone())); + } + if let Some(ref socket) = section.socket + && *socket == route + { + return Err(WebInterfaceError::RouteSocketOverlap(route.clone())); + } + if is_host_reserved(&route) { + return Err(WebInterfaceError::RouteReserved(route)); + } + if let Some(ref socket) = section.socket + && is_host_reserved(socket) + { + return Err(WebInterfaceError::SocketReserved(socket.clone())); + } Ok(Some(WebInterface { mode, - route: section.route.unwrap_or_else(|| "/world".to_owned()), + route, socket: section.socket, })) } @@ -195,4 +314,101 @@ socket = "/ws/arena" Err(WebInterfaceError::UnknownMode(_)) )); } + + #[test] + fn route_without_a_leading_slash_is_rejected() { + // Packet case 1: `route = "arena"` parsed Ok and panicked in + // `create_router_for_app` (routes.rs:322). + let source = "[interfaces.web]\nroute = \"arena\"\n"; + assert!(matches!( + web_interface_from_app_toml(source), + Err(WebInterfaceError::RouteMustStartWithSlash(value)) + if value == "arena" + )); + } + + #[test] + fn host_page_routes_are_reserved() { + // Packet cases 2-3: `route = "/"` and `route = "/files"` parsed + // Ok and panicked on the duplicate host route (routes.rs:322). + for reserved in ["/", "/files", "/settings", "/peers"] { + let source = format!("[interfaces.web]\nroute = {reserved:?}\n"); + assert!( + matches!( + web_interface_from_app_toml(&source), + Err(WebInterfaceError::RouteReserved(_)) + ), + "{reserved} must be rejected as host-reserved" + ); + } + } + + #[test] + fn socket_without_a_leading_slash_is_rejected() { + // Packet case 4: `socket = "ws/arena"` parsed Ok and panicked + // in `world_routes_at` (world_ws.rs:60). + let source = "[interfaces.web]\nroute = \"/arena\"\nsocket = \"ws/arena\"\n"; + assert!(matches!( + web_interface_from_app_toml(source), + Err(WebInterfaceError::SocketMustStartWithSlash(value)) + if value == "ws/arena" + )); + } + + #[test] + fn host_socket_routes_are_reserved() { + // Packet case 5: `socket = "/ws/tags"` parsed Ok and panicked + // on the duplicate host route (routes.rs:382 via + // world_ws.rs:60). + let source = "[interfaces.web]\nroute = \"/arena\"\nsocket = \"/ws/tags\"\n"; + assert!(matches!( + web_interface_from_app_toml(source), + Err(WebInterfaceError::SocketReserved(value)) if value == "/ws/tags" + )); + } + + #[test] + fn world_api_surface_is_reserved_for_both_sides() { + let source = "[interfaces.web]\nroute = \"/api/world/invite\"\n"; + assert!(matches!( + web_interface_from_app_toml(source), + Err(WebInterfaceError::RouteReserved(_)) + )); + let source = "[interfaces.web]\nroute = \"/arena\"\nsocket = \"/api/world/metrics\"\n"; + assert!(matches!( + web_interface_from_app_toml(source), + Err(WebInterfaceError::SocketReserved(_)) + )); + } + + #[test] + fn route_and_socket_naming_the_same_path_is_rejected() { + let source = "[interfaces.web]\nroute = \"/arena\"\nsocket = \"/arena\"\n"; + assert!(matches!( + web_interface_from_app_toml(source), + Err(WebInterfaceError::RouteSocketOverlap(value)) if value == "/arena" + )); + } + + #[test] + fn world_app_binding_still_parses() { + // The built-in world app's binding (`/world` + `/ws/world`) + // and the shipped tic-tac-toe app must keep working. + let world = WebInterface::world_app(); + let source = format!( + "[interfaces.web]\nroute = {:?}\nsocket = {:?}\n", + world.route, + world.socket.as_deref().unwrap_or_default() + ); + let parsed = web_interface_from_app_toml(&source) + .expect("world binding parses") + .expect("web section present"); + assert_eq!(parsed, world); + let source = "[interfaces.web]\nmode = \"adapter\"\nroute = \"/tic-tac-toe\"\n"; + let parsed = web_interface_from_app_toml(source) + .expect("tic-tac-toe binding parses") + .expect("web section present"); + assert_eq!(parsed.route, "/tic-tac-toe"); + assert_eq!(parsed.socket, None); + } }