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.
This commit is contained in:
Marco Allegretti 2026-10-09 07:53:23 +00:00
parent db712e056e
commit 3875650226
No known key found for this signature in database
4 changed files with 22 additions and 7 deletions

View file

@ -76,7 +76,7 @@ impl WebViewDelegate for WeftWebViewDelegate {
/// The webview carries the session's bridge, so it stays within the /// The webview carries the session's bridge, so it stays within the
/// application's own UI files. /// application's own UI files.
fn request_navigation(&self, _webview: servo::WebView, request: NavigationRequest) { 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(); request.allow();
} else { } else {
tracing::warn!(url = %request.url, "navigation outside the application UI denied"); 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() 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. /// `value` as a single-quoted JavaScript string literal.
fn js_string(value: &str) -> String { fn js_string(value: &str) -> String {
let mut literal = String::with_capacity(value.len() + 2); 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); let scope = js_string(scope);
format!( format!(
r#"(function () {{ 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 ws = new WebSocket('ws://127.0.0.1:{port}/app');
var queue = []; var queue = [];
var open = false; var open = false;

View file

@ -29,7 +29,7 @@ pub(crate) type Registry = Arc<Mutex<SessionRegistry>>;
struct SessionEntry { struct SessionEntry {
app_id: String, app_id: String,
state: AppStateKind, state: AppStateKind,
/// Credential binding one application bridge connection to this session. /// Credential that binds application bridge connections to this session.
bridge_token: Option<String>, bridge_token: Option<String>,
} }
@ -285,7 +285,11 @@ async fn run() -> anyhow::Result<()> {
registry.lock().await.shutdown_all(); registry.lock().await.shutdown_all();
tokio::time::sleep(tokio::time::Duration::from_millis(200)).await; tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
let _ = std::fs::remove_file(&socket_path); 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); let _ = std::fs::remove_file(path);
} }
Ok(()) Ok(())

View file

@ -28,7 +28,7 @@ use crate::ipc::{AppStateKind, Request, Response};
use crate::{Registry, dispatch}; use crate::{Registry, dispatch};
const HELLO_TIMEOUT: Duration = if cfg!(test) { const HELLO_TIMEOUT: Duration = if cfg!(test) {
Duration::from_millis(500) Duration::from_secs(1)
} else { } else {
Duration::from_secs(5) Duration::from_secs(5)
}; };

View file

@ -12,11 +12,11 @@ Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` proto
### weft-servo-shell ### 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 ### weft-app-shell
Per-application Servo host. Spawned by `weft-appd` after the Wasm runtime signals READY. Takes `<app_id>` and `<session_id>` as arguments. Resolves `weft-app://<app_id>/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 `<app_id>` and `<session_id>` as arguments. Resolves `weft-app://<app_id>/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 ### weft-appd