From 38756502263c1e100414b64411e22267176be6aa Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 07:53:23 +0000 Subject: [PATCH] fix(app-shell): refuse encoded separators in the UI scope A file URL whose path holds an encoded slash or backslash keeps the UI directory prefix while a loader may decode it into a path outside that directory. Navigation and the bridge now refuse such URLs. appd removes the system token file at shutdown only while it still holds its own token, and the HELLO deadline in unit tests is one second. --- crates/weft-app-shell/src/embedder.rs | 15 +++++++++++++-- crates/weft-appd/src/main.rs | 8 ++++++-- crates/weft-appd/src/ws.rs | 2 +- docs/architecture.md | 4 ++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/crates/weft-app-shell/src/embedder.rs b/crates/weft-app-shell/src/embedder.rs index 4d30cee..37880f1 100644 --- a/crates/weft-app-shell/src/embedder.rs +++ b/crates/weft-app-shell/src/embedder.rs @@ -76,7 +76,7 @@ impl WebViewDelegate for WeftWebViewDelegate { /// The webview carries the session's bridge, so it stays within the /// application's own UI files. fn request_navigation(&self, _webview: servo::WebView, request: NavigationRequest) { - if request.url.as_str().starts_with(&self.scope) { + if in_scope(request.url.as_str(), &self.scope) { request.allow(); } else { tracing::warn!(url = %request.url, "navigation outside the application UI denied"); @@ -132,6 +132,16 @@ fn ui_scope(entry: &ServoUrl) -> String { url[..end].to_owned() } +/// Whether `url` lies inside the UI directory `scope`. Encoded path +/// separators are refused, since a file loader may decode them into a path +/// that leaves the directory. +fn in_scope(url: &str, scope: &str) -> bool { + url.strip_prefix(scope).is_some_and(|rest| { + let rest = rest.to_ascii_lowercase(); + !rest.contains("%2f") && !rest.contains("%5c") + }) +} + /// `value` as a single-quoted JavaScript string literal. fn js_string(value: &str) -> String { let mut literal = String::with_capacity(value.len() + 2); @@ -158,7 +168,8 @@ fn bridge_script(port: u16, session_id: u64, token: &str, scope: &str) -> String let scope = js_string(scope); format!( r#"(function () {{ - if (window !== window.top || location.href.indexOf({scope}) !== 0) return; + var rest = location.href.indexOf({scope}) === 0 ? location.href.slice({scope}.length) : null; + if (window !== window.top || rest === null || /%2f|%5c/i.test(rest)) return; var ws = new WebSocket('ws://127.0.0.1:{port}/app'); var queue = []; var open = false; diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 6754373..f9a6af1 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -29,7 +29,7 @@ pub(crate) type Registry = Arc>; struct SessionEntry { app_id: String, state: AppStateKind, - /// Credential binding one application bridge connection to this session. + /// Credential that binds application bridge connections to this session. bridge_token: Option, } @@ -285,7 +285,11 @@ async fn run() -> anyhow::Result<()> { registry.lock().await.shutdown_all(); tokio::time::sleep(tokio::time::Duration::from_millis(200)).await; let _ = std::fs::remove_file(&socket_path); - if let Ok(path) = system_token_path() { + // A newer appd may already have replaced the file; only this instance's + // own token is removed. + if let Ok(path) = system_token_path() + && std::fs::read_to_string(&path).is_ok_and(|t| t == ws_auth.system_token) + { let _ = std::fs::remove_file(path); } Ok(()) diff --git a/crates/weft-appd/src/ws.rs b/crates/weft-appd/src/ws.rs index 38238c4..e482adc 100644 --- a/crates/weft-appd/src/ws.rs +++ b/crates/weft-appd/src/ws.rs @@ -28,7 +28,7 @@ use crate::ipc::{AppStateKind, Request, Response}; use crate::{Registry, dispatch}; const HELLO_TIMEOUT: Duration = if cfg!(test) { - Duration::from_millis(500) + Duration::from_secs(1) } else { Duration::from_secs(5) }; diff --git a/docs/architecture.md b/docs/architecture.md index db9a4d6..ca77f3c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -12,11 +12,11 @@ Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` proto ### weft-servo-shell -System UI host. Renders one WebView pointing at `system-ui.html` using the embedded Servo engine (feature-gated behind `servo-embed`). Connects to the compositor as window type `panel`. Dispatches the `zweft_shell_manager_v1` event queue each frame. Forwards navigation gestures received from the compositor to `weft-appd` over WebSocket. Reads the appd WebSocket port and system token from `$XDG_RUNTIME_DIR/weft/` and hands both to the page through `window.weftAppdEndpoint(port, token)`; the shell hands the token only to the system UI document and denies navigation away from it, and the launcher shows package names as text, never markup. +System UI host. Renders one WebView pointing at `system-ui.html` using the embedded Servo engine (feature-gated behind `servo-embed`). Connects to the compositor as window type `panel`. Dispatches the `zweft_shell_manager_v1` event queue each frame. Forwards navigation gestures received from the compositor to `weft-appd` over WebSocket. Reads the appd WebSocket port and system token from `$XDG_RUNTIME_DIR/weft/` and hands both to the page through `window.weftAppdEndpoint(port, token)`; the shell hands the token only to the system UI document and denies top-level navigation away from it, and the launcher shows package names as text, never markup. ### weft-app-shell -Per-application Servo host. Spawned by `weft-appd` after the Wasm runtime signals READY. Takes `` and `` as arguments. Resolves `weft-app:///ui/index.html` and injects the `weftIpc` WebSocket bridge into the page. The bridge authenticates with the per-session token from `WEFT_BRIDGE_TOKEN` and can only exchange messages with its own session. It is installed only in top-level documents inside the application's UI directory, and navigation outside that directory is denied. Registers with the compositor as window type `application`. Exits when the appd session ends. +Per-application Servo host. Spawned by `weft-appd` after the Wasm runtime signals READY. Takes `` and `` as arguments. Resolves `weft-app:///ui/index.html` and injects the `weftIpc` WebSocket bridge into the page. The bridge authenticates with the per-session token from `WEFT_BRIDGE_TOKEN` and can only exchange messages with its own session. It is installed only in top-level documents inside the application's UI directory, and top-level navigation outside that directory is denied; framed documents never receive the bridge. Registers with the compositor as window type `application`. Exits when the appd session ends. ### weft-appd