From 339dc4e38d961922bcbb273c3549f850413dc28d Mon Sep 17 00:00:00 2001 From: ldm0 Date: Sun, 8 Nov 2026 15:41:14 +0800 Subject: [PATCH] fix(service-workers): deny container access for opaque Window origins Check the Navigator's captured native Document origin before returning its ServiceWorkerContainer, including the lazy cache path. Sandboxed HTTP pages must throw SecurityError at the getter even though their URL has a tuple origin. Keep WorkerNavigator exposure separate and preserve inherited origins. Cover HTTP and srcdoc sandboxes, removing the sandbox attribute without a navigation, author overrides, borrowed getters, and inherited blank windows. --- .../navigator_runtime/navigator.rs | 19 +++ .../extracted/service_worker_registration.rs | 111 ++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/moli-renderer-v8/src/context_bootstrap/navigator_runtime/navigator.rs b/moli-renderer-v8/src/context_bootstrap/navigator_runtime/navigator.rs index 7452d16e578..6733c5122dd 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigator_runtime/navigator.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigator_runtime/navigator.rs @@ -665,6 +665,25 @@ fn navigator_runtime_data_getter_callback<'s>( rv.set(v8::undefined(scope).into()); return; }; + if key == "serviceWorker" { + // Window Navigators retain their Document's native origin, including + // sandboxed HTTP documents and inherited about:blank/srcdoc origins. + // Check before returning a cached container. WorkerNavigator has its + // own exposure policy and does not carry this Window-only slot. + let origin = moli_webapi_declare::web_api_object_target(scope, args.this()) + .and_then(|navigator| { + get_private_value(scope, navigator, super::protocol_handlers::ORIGIN_SLOT) + }) + .and_then(|value| v8::Local::::try_from(value).ok()); + if origin.is_some_and(|origin| origin.to_rust_string_lossy(scope) == "null") { + crate::webidl::throw_dom_exception( + scope, + "SecurityError", + "Access to service workers is denied in this document origin.", + ); + return; + } + } if matches!(key, "language" | "languages" | "userAgentData") && let Some(identity) = worker_navigator_identity(scope, backing) { diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/service_worker_registration.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/service_worker_registration.rs index abab10736f0..bbb7d62e0ba 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/service_worker_registration.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/service_worker_registration.rs @@ -1,5 +1,116 @@ use super::*; +#[tokio::test] +async fn service_worker_getter_uses_native_window_origin_before_exposing_container() { + const PROBE: &str = r#""#; + let (base_url, server) = spawn_service_worker_response_server(vec![ + ("/opaque.html", "text/html", PROBE), + ("/allowed.html", "text/html", PROBE), + ("/normal.html", "text/html", PROBE), + ]) + .await; + let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap(); + let (mut vm, runtime) = new_service_worker_page_test_vm_with_loader_and_browser_context_runtime( + &format!("{base_url}/app/page.html"), + &loader, + ); + vm.eval(&format!( + "globalThis.__getterProbeSource = {};", + serde_json::to_string(PROBE).unwrap() + )) + .unwrap(); + vm.eval(r#" + globalThis.__getterOriginDone = false; + globalThis.__getterOriginPhase = 'start'; + (async () => { + const checks = []; + let probeId = 0; + const makeFrame = async (sandbox, src, srcdoc) => { + __getterOriginPhase = 'load:' + src + ':' + sandbox; + const frame = document.createElement('iframe'); + if (sandbox !== null) frame.sandbox = sandbox; + if (src !== null) frame.src = src; + if (srcdoc !== null) frame.srcdoc = srcdoc; + const loaded = new Promise(resolve => { frame.onload = resolve; }); + document.body.appendChild(frame); + await loaded; + __getterOriginPhase = 'loaded:' + src + ':' + sandbox; + return frame; + }; + const probe = frame => new Promise(resolve => { + const id = ++probeId; + const handler = event => { + if (!event.data || event.data.id !== id) return; + removeEventListener('message', handler); + resolve(event.data.result); + }; + addEventListener('message', handler); + frame.contentWindow.postMessage(id, '*'); + }); + const opaque = await makeFrame('allow-scripts', '/opaque.html', null); + checks.push(await probe(opaque)); + opaque.removeAttribute('sandbox'); + checks.push(await probe(opaque)); + checks.push(await probe(await makeFrame('allow-scripts allow-same-origin', '/allowed.html', null))); + checks.push(await probe(await makeFrame(null, '/normal.html', null))); + checks.push(await probe(await makeFrame('allow-scripts', null, __getterProbeSource))); + checks.push(await probe(await makeFrame('allow-scripts allow-same-origin', null, __getterProbeSource))); + const blank = await makeFrame(null, 'about:blank', null); + const getter = Object.getOwnPropertyDescriptor(Navigator.prototype, 'serviceWorker').get; + const childGetter = Object.getOwnPropertyDescriptor(blank.contentWindow.Navigator.prototype, 'serviceWorker').get; + const childNavigator = blank.contentWindow.navigator; + checks.push(getter.call(childNavigator) === childNavigator.serviceWorker); + checks.push(childGetter.call(navigator) === navigator.serviceWorker); + try { getter.call({}); checks.push('accepted'); } + catch (error) { checks.push(error.name); } + const popup = open('about:blank', 'service-worker-getter-origin'); + checks.push(popup.navigator.serviceWorker === popup.navigator.serviceWorker); + popup.close(); + globalThis.__getterOrigin = checks.join(';'); + })().catch(error => { __getterOrigin = 'error:' + error.name + ':' + error.message; }) + .finally(() => { __getterOriginPhase = 'done'; __getterOriginDone = true; }); + "#).unwrap(); + drain_service_worker_test_until_eval_equals( + &mut vm, + &runtime, + &loader, + "String(__getterOriginDone) + ':' + __getterOriginPhase", + "true:done", + ) + .await; + assert_eq!( + vm.eval("String(__getterOrigin)").unwrap(), + concat!( + "SecurityError:true|SecurityError:true;SecurityError:true|SecurityError:true;", + "true|true;true|true;SecurityError:true|SecurityError:true;true|true;", + "true;true;TypeError;true" + ) + ); + server.await.unwrap(); +} + #[tokio::test] async fn service_worker_get_registration_separates_api_base_url_from_native_origin() { let (base_url, server) =