diff --git a/docs/validation/isolated-service-worker-child-892-264.md b/docs/validation/isolated-service-worker-child-892-264.md new file mode 100644 index 00000000..f1237839 --- /dev/null +++ b/docs/validation/isolated-service-worker-child-892-264.md @@ -0,0 +1,17 @@ +# Isolated iframe ServiceWorker — WebScene #892 / #264 + +## Exact installed-product trigger + +Unchanged Code OSS `645f29cc` on WebScene `d3c030fa` and AppScene `34ba9e5` runs from a signed package built through the verified installed SDK/CLI. Native keyboard **Markdown: Open Preview to the Side** loads its generated isolated HTTPS prelude and then throws at inline line 237: `Service Workers are not enabled. Webviews will not work.` The prelude checks `!!navigator.serviceWorker`. The screenshot and resource/exception trace are in `vscode-demo/build/appscene-webview-398/` and the reduced ownership record is WebScene #892. The preview is still blank. + +Nested context construction installed ServiceWorker only if the **parent** could access the child's DOM. A different-origin HTTPS child must have its own secure worker capability while remaining inaccessible to the parent. A second, initially masked security defect was exposed by the focused native test: the old sandbox check walked the child's body ancestors before its body had been attached to the owning iframe, so `sandbox="allow-scripts"` incorrectly exposed ServiceWorker to an opaque child. Pass the owning iframe to the installer, inspect it and every ancestor before child scripts run, and keep the parent DOM-access check only for parent DOM operations. + +## Focused native gate + +Run `WEBSCENE_NATIVE_ENGINE_TEST_FILTER=service-worker-cross-origin webscene_native_engine_tests` against the V8/graphics configuration of the installed macOS SDK. The product-neutral fixture uses a distinct HTTPS parent and child, with explicitly admitted installed document/worker bytes. The parent cannot access either child's `contentDocument`; the allowed child has `sandbox="allow-same-origin allow-scripts"` and registers its own same-origin module ServiceWorker. An opaque `allow-scripts` child and a nested `allow-same-origin allow-scripts` child under that opaque ancestor both lack `navigator.serviceWorker`. + +The first runtime-only change made the allowed child register but incorrectly reported `opaque-sw-visible:true`; the final owner/ancestor check makes all three positive and negative assertions pass. Native registration plus both sandbox checks took **230.711 ms** against the focused 1 s budget. Related existing native gates pass: 100 ServiceWorker register/update/unregister cycles p95 **6.60546 ms**, 100 MessagePort/Clients cycles p95 **0.053208 ms**, and the 100-cycle iframe sandbox gate. The ServiceWorker lifecycle fixture reports identical 1,849,600-byte V8 heap before and after forced collection. + +This is a macOS native source qualification with Inspector disabled in the local test configuration because the reused local V8 archive does not carry the separate Inspector console bridge ABI. The signed installed SDK/CLI package and full Code OSS Markdown/visual/lifecycle gate must be repeated after the focused PR merges. No installed Markdown success or Windows/Linux run is claimed here. + +The first PR metadata job exposed a pre-existing main-branch CI failure: `scripts/tests/test_v8_bootstrap_literals.py` expected an older IndexedDB JavaScript bootstrap SHA-256 (`a3c907...`) while the merged source hashes to `0c9d4e...`; the previous `main` metadata run fails the same exact assertion. The focused branch now pins the current bytes without weakening the literal-size/portability checks. All five local literal tests pass; rerun metadata on the updated PR head before merging. diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime.cpp b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime.cpp index 3adf11ae..835b5a5e 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime.cpp +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime.cpp @@ -5383,7 +5383,7 @@ struct v8_dom_runtime::implementation final { v8::Function::New(local_context, structured_clone, {}, 1).ToLocalChecked()).Check(); install_indexeddb(local_context); install_performance_timeline(local_context); - install_service_worker_control(local_context, local_context->Global()); + install_service_worker_control(local_context, local_context->Global(), nullptr); install_worker_constructor(local_context); install_clipboard_api(local_context); install_drag_data_transfer_api(local_context); diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_cache_and_frames.inc b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_cache_and_frames.inc index 65302068..caac14d3 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_cache_and_frames.inc +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_cache_and_frames.inc @@ -1516,9 +1516,10 @@ } install_navigator(isolate, local_context, global); - if (same_origin_access) { - install_service_worker_control(local_context, global); - } + // A sandbox-permitted HTTPS child has its own secure origin even when + // its parent cannot access its document. The installer checks the + // iframe sandbox chain; parent DOM access is a separate capability. + install_service_worker_control(local_context, global, &frame); install_console(local_context, global); diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_service_workers.inc b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_service_workers.inc index 39163b83..6f378339 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_service_workers.inc +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_service_workers.inc @@ -2516,19 +2516,19 @@ void install_service_worker_control( v8::Local local_context, - v8::Local global) + v8::Local global, + dom_node* frame_owner) { if (worker_owner != nullptr || service_worker_realm) return; - if (in_frame_context()) { - auto* root = static_cast( - local_context->GetAlignedPointerFromEmbedderData( - 1, - v8::kEmbedderDataTypeTagDefault)); - for (auto* ancestor = root; ancestor != nullptr; ancestor = ancestor->parent) { - if (ancestor->tag == "iframe" - && !iframe_sandbox_allows(*ancestor, "allow-same-origin")) { - return; - } + // The child body is attached to its iframe only after realm globals + // are installed. Inspect the owning iframe and its ancestors now, + // before any child script or ServiceWorker bridge can run. + if (in_frame_context() && frame_owner == nullptr) return; + for (auto* ancestor = frame_owner; ancestor != nullptr; + ancestor = ancestor->parent) { + if (ancestor->tag == "iframe" + && !iframe_sandbox_allows(*ancestor, "allow-same-origin")) { + return; } } global->Set( diff --git a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_service_worker_tests.inc b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_service_worker_tests.inc index 98cb0725..cdd20d05 100644 --- a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_service_worker_tests.inc +++ b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_service_worker_tests.inc @@ -1,3 +1,109 @@ +void test_service_worker_cross_origin_iframe_visibility_gate() +{ + resource_server server{.content = { + {"https://workspace.test/index.html", + "isolated worker owner"}, + {"https://preview.test/pre/index.html", R"HTML( + )HTML"}, + {"https://preview.test/pre/service-worker.js", R"JS( + self.addEventListener('install', event => event.waitUntil(skipWaiting())); + self.addEventListener('activate', event => event.waitUntil(clients.claim())); + )JS"}, + {"https://preview.test/pre/opaque.html", + ""}, + {"https://preview.test/pre/nested.html", + ""} + }}; + webscene_engine_options options{}; + options.struct_size = sizeof(options); + options.resource_load_callback = load_test_resource; + options.resource_load_user_data = &server; + auto* engine = webscene_engine_create_with_options(&options); + require(engine != nullptr, "isolated iframe worker engine creation failed"); + webscene_engine_configure_diagnostics( + engine, WEBSCENE_DIAGNOSTIC_CONSOLE | WEBSCENE_DIAGNOSTIC_EXCEPTIONS, + nullptr, nullptr); + constexpr std::string_view address{"https://workspace.test/index.html"}; + require(webscene_engine_load_url(engine, address.data(), address.size()) != 0, + "isolated iframe worker document load failed"); + require(evaluate_until_equals(engine, "location.href", "isolated-sw-parent.js", + R"("https://workspace.test/index.html")", 2'500) + == R"("https://workspace.test/index.html")", + "isolated iframe worker parent did not become ready"); + const auto frame_started = std::chrono::steady_clock::now(); + execute(engine, R"JS((() => { + const admitted = document.createElement('iframe'); + admitted.setAttribute('sandbox', 'allow-same-origin allow-scripts'); + admitted.src = 'https://preview.test/pre/index.html'; + document.body.appendChild(admitted); + const opaque = document.createElement('iframe'); + opaque.setAttribute('sandbox', 'allow-scripts'); + opaque.src = 'https://preview.test/pre/opaque.html'; + document.body.appendChild(opaque); + globalThis.__isolatedFrames = [admitted, opaque]; + })())JS", "isolated-sw-create.js"); + require(evaluate_until_equals(engine, + "__isolatedFrames.every(frame => frame.contentDocument === null)", + "isolated-sw-parent-denied.js", "true", 2'500) == "true", + "isolated worker clients leaked cross-origin parent DOM access"); + + bool visible = false, registered = false; + bool opaque_hidden = false, nested_opaque_hidden = false; + std::string records; + for (int attempt = 0; attempt < 1'500 + && !(visible && registered && opaque_hidden && nested_opaque_hidden); + ++attempt) { + const auto length = webscene_engine_take_diagnostic(engine, nullptr, 0); + if (length == 0U) { + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + continue; + } + std::vector payload(length); + require(webscene_engine_take_diagnostic(engine, payload.data(), length) + == length, "isolated worker diagnostic changed during copy"); + const auto record = std::string(payload.data()); + records += record + '\n'; + const bool admitted_document = record.find( + "https://preview.test/pre/index.html") != std::string::npos; + visible |= admitted_document && record.find("isolated-sw-visible:true") + != std::string::npos; + registered |= admitted_document && record.find("isolated-sw-registered:true") + != std::string::npos; + opaque_hidden |= record.find("https://preview.test/pre/opaque.html") + != std::string::npos + && record.find("opaque-sw-visible:false") != std::string::npos; + nested_opaque_hidden |= record.find("https://preview.test/pre/nested.html") + != std::string::npos + && record.find("nested-opaque-sw-visible:false") + != std::string::npos; + } + require(visible && registered && opaque_hidden && nested_opaque_hidden, + "ServiceWorker visibility/registration escaped iframe origin or sandbox: " + + records); + const auto elapsed_ms = std::chrono::duration( + std::chrono::steady_clock::now() - frame_started).count(); + require(elapsed_ms <= 1'000.0, + "isolated iframe registration exceeded the 1 s focused budget"); + std::cout << "Isolated ServiceWorker iframe: registrationAndSandboxMs=" + << elapsed_ms << " registered=1 opaqueHidden=1 nestedHidden=1\n"; + webscene_engine_destroy(engine); +} + void test_service_worker_lifecycle_performance_and_teardown_gate() { resource_server server{.content = { diff --git a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_tests.cpp b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_tests.cpp index bba3d14f..e82a5772 100644 --- a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_tests.cpp +++ b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_tests.cpp @@ -299,6 +299,10 @@ int main() test_resource_cache_policy_matrix(); return 0; } + if (selected == "service-worker-cross-origin") { + test_service_worker_cross_origin_iframe_visibility_gate(); + return 0; + } if (selected == "service-worker-lifecycle") { test_service_worker_lifecycle_performance_and_teardown_gate(); return 0; diff --git a/scripts/tests/test_v8_bootstrap_literals.py b/scripts/tests/test_v8_bootstrap_literals.py index 8b88ed87..33a85325 100644 --- a/scripts/tests/test_v8_bootstrap_literals.py +++ b/scripts/tests/test_v8_bootstrap_literals.py @@ -123,7 +123,7 @@ def test_indexeddb_bootstrap_is_portable_and_byte_exact(self) -> None: ) self.assertEqual( hashlib.sha256(joined.encode("utf-8")).hexdigest(), - "a3c907644e9b6c9d63eddfbd6a591802e99bbd3249a3b1437788ce4eb1c73b64", + "0c9d4ed136ea2b6dfb25e0503d989e4cda396df01a80eb3e7bb4ae49ac234669", )