Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/validation/isolated-service-worker-child-892-264.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2516,19 +2516,19 @@

void install_service_worker_control(
v8::Local<v8::Context> local_context,
v8::Local<v8::Object> global)
v8::Local<v8::Object> global,
dom_node* frame_owner)
{
if (worker_owner != nullptr || service_worker_realm) return;
if (in_frame_context()) {
auto* root = static_cast<dom_node*>(
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(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,109 @@
void test_service_worker_cross_origin_iframe_visibility_gate()
{
resource_server server{.content = {
{"https://workspace.test/index.html",
"<!doctype html><title>isolated worker owner</title><body></body>"},
{"https://preview.test/pre/index.html", R"HTML(
<!doctype html><body><script>
console.log('isolated-sw-visible:' + Boolean(navigator.serviceWorker));
if (navigator.serviceWorker) {
navigator.serviceWorker.register('service-worker.js', {
type: 'module', updateViaCache: 'none'
}).then(registration => console.log('isolated-sw-registered:' +
Boolean(registration.active))).catch(error =>
console.error('isolated-sw-error:' + error.message));
}
</script></body>)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",
"<!doctype html><body><script>console.log('opaque-sw-visible:' +"
" Boolean(navigator.serviceWorker));"
"const nested=document.createElement('iframe');"
"nested.setAttribute('sandbox','allow-same-origin allow-scripts');"
"nested.src='https://preview.test/pre/nested.html';"
"document.body.appendChild(nested);</script></body>"},
{"https://preview.test/pre/nested.html",
"<!doctype html><body><script>console.log('nested-opaque-sw-visible:' +"
" Boolean(navigator.serviceWorker));</script></body>"}
}};
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<char> 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<double, std::milli>(
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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion scripts/tests/test_v8_bootstrap_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)


Expand Down
Loading