From a154aad67eedf5554ff019540563b13df6449cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Mon, 21 Sep 2026 08:57:16 +0200 Subject: [PATCH] Restore restricted parent messaging in isolated iframe realms --- .../isolated-parent-postmessage-894-264.md | 45 +++++++++ .../webscene_v8_runtime_cache_and_frames.inc | 65 ++++++++++--- .../native/webscene_v8_runtime_html.inc | 51 ++++++++++ .../native_v8_runtime_resource_tests.inc | 96 +++++++++++++++++++ .../tests/native_v8_runtime_tests.cpp | 4 + 5 files changed, 248 insertions(+), 13 deletions(-) create mode 100644 docs/validation/isolated-parent-postmessage-894-264.md diff --git a/docs/validation/isolated-parent-postmessage-894-264.md b/docs/validation/isolated-parent-postmessage-894-264.md new file mode 100644 index 00000000..8af7b2ee --- /dev/null +++ b/docs/validation/isolated-parent-postmessage-894-264.md @@ -0,0 +1,45 @@ +# Restricted cross-origin parent messaging — 21 September 2026 + +An unchanged Code OSS `645f29cc` Markdown Preview ran in a signed installed-CLI +Release from merged WebScene `06d6a4f5` and AppScene `1142abe`. The isolated +HTTPS prelude and its ServiceWorker script load, but the native preview remains +blank. Its first prelude exception is `Cannot read properties of null (reading +'postMessage')` at inline line 313, the unchanged source's +`window.parent.postMessage({target:ID,channel:'webview-ready'}, parentOrigin, +[this.channel.port2])`. See `vscode-demo/build/appscene-webview-398/merged-1142abe-06d6a4f5/native-preview.log` +and `preview-parent-window-null.png`. WebScene installed `parent=null` in a +cross-origin child because the parent cannot read the child's DOM. The child +still needs a restricted WindowProxy to send a target-origin message. + +The focused fix creates a restricted child-realm parent proxy that only grants +safe WindowProxy properties and the existing bounded `postMessage`/structured- +clone route. The actual parent global, document, storage and arbitrary +properties are denied with `SecurityError`. The owner-realm iframe WindowProxy +remains stable so `MessageEvent.source` retains identity. A direct child +addresses its top parent; a nested cross-origin child addresses its immediate +parent and gets a separate restricted top reference. Its owning frame pointer +and the existing frame task queue refuse detached targets; no polling or frame +scheduling is introduced. + +The focused native cross-origin HTTPS test verifies a parent DOM denial, parent +proxy read/write denial, exact child message origin/source and targetOrigin, +non-delivery to wrong origins, one transferred MessagePort and reply, nested +child-to-immediate-parent delivery and stable nested source. The full direct +and nested handshake took **76.2577 ms** against the 1,000 ms budget. Existing +isolated ServiceWorker registration and direct/inherited opaque sandbox test +passes in **219.654 ms**, the 100-cycle iframe sandbox gate passes, and the +100-cycle ServiceWorker lifecycle passes at **7.84092 ms p95** with flat +1,849,544-byte heap. This native build uses the existing local macOS V8 +monolith with Inspector disabled; installed production SDK/CLI and CI follow. + +`WEBSCENE_NATIVE_ENGINE_TEST_FILTER=iframe-navigation-lifecycle` fails on +`reentrantUnloadBlocked:false` both with this patch and after fully reverting +all its source/test changes and rebuilding the clean merged `06d6a4f5` in the +same configuration. This independent merged-main navigation gap is recorded +under WebScene #267 and must not be attributed to the restricted parent proxy. + +The focused branch must pass relevant runners before merge. An exact merged +installed SDK/CLI Release and native Markdown preview must then prove the +next webview stage, including inner frame, visual, lifecycle and performance +acceptance under WebScene #264 and AppScene #398. No Code OSS source was +modified; no Electron, Chromium, CEF or WebView is introduced. 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 caac14d3..211c9f2d 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 @@ -1238,21 +1238,32 @@ frame_pagehide_dispatched.erase(frame.id); frame_unload_dispatches.erase(frame.id); frame_unload_dispatched.erase(frame.id); - if (same_origin_access) { - // Every browsing context has a WindowProxy even when script has - // not read iframe.contentWindow yet. Materialize it before child - // script can post a message so MessageEvent.source can retain the - // stable proxy across the initial document replacement. + // Every browsing context needs an owner-realm WindowProxy before its + // first script posts a message, even when origins differ. Preserve + // MessageEvent.source identity through the initial navigation. + { auto owner_context = context.Get(isolate); v8::Context::Scope owner_scope(owner_context); - auto proxy = frame_window(frame); - proxy->Set( - owner_context, - js_string(isolate, "document"), - frame_document_value).Check(); + if (same_origin_access) { + auto proxy = frame_window(frame); + proxy->Set( + owner_context, + js_string(isolate, "document"), + frame_document_value).Check(); + } else if (!frame_windows.contains(frame.id)) { + // The normal constructor populates a provisional same-origin + // document. A cross-origin commit must not read that document + // while initializing its stable, masked owner-realm proxy. + auto proxy = frame_window_template.Get(isolate) + ->NewInstance(owner_context).ToLocalChecked(); + proxy->SetAlignedPointerInInternalField( + 0, &frame, v8::kEmbedderDataTypeTagDefault); + frame_windows[frame.id].Reset(isolate, proxy); + } } global->Set(local_context, js_string(isolate, "name"), js_string(isolate, frame.attributes["name"].c_str())).Check(); v8::Local parent_window = v8::Null(isolate); + v8::Local top_window = v8::Null(isolate); if (same_origin_access) { parent_window = context.Get(isolate)->Global(); for (auto* ancestor = frame.parent; @@ -1266,14 +1277,42 @@ } break; } + top_window = context.Get(isolate)->Global(); + } else { + // A real parent global would bypass the child security token. + // Give the child only a restricted proxy whose postMessage uses + // the existing target-origin and structured-clone queue. + auto restricted = v8::ObjectTemplate::New(isolate); + restricted->SetInternalFieldCount(1); + restricted->SetHandler(v8::NamedPropertyHandlerConfiguration( + get_restricted_parent_window_property, + set_restricted_parent_window_property)); + dom_node* owning_parent = nullptr; + for (auto* ancestor = frame.parent; ancestor != nullptr; + ancestor = ancestor->parent) { + if (ancestor->tag == "iframe") { owning_parent = ancestor; break; } + } + auto proxy = restricted->NewInstance(local_context).ToLocalChecked(); + proxy->SetAlignedPointerInInternalField( + 0, owning_parent, v8::kEmbedderDataTypeTagDefault); + parent_window = proxy; + top_window = proxy; + if (owning_parent != nullptr) { + auto top_proxy = restricted->NewInstance(local_context).ToLocalChecked(); + top_proxy->SetAlignedPointerInInternalField( + 0, nullptr, v8::kEmbedderDataTypeTagDefault); + proxy->SetPrivate( + local_context, + v8::Private::ForApi(isolate, js_string(isolate, "webscene.parentTop")), + top_proxy).Check(); + top_window = top_proxy; + } } global->Set(local_context, js_string(isolate, "parent"), parent_window).Check(); global->Set( local_context, js_string(isolate, "top"), - same_origin_access - ? v8::Local(context.Get(isolate)->Global()) - : v8::Local(v8::Null(isolate))).Check(); + top_window).Check(); global->Set( local_context, js_string(isolate, "frameElement"), diff --git a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_html.inc b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_html.inc index 7e58f319..c3f4fe14 100644 --- a/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_html.inc +++ b/experiments/WebScene.NativeEngine.Probe/native/webscene_v8_runtime_html.inc @@ -883,6 +883,57 @@ return count; } + // A cross-origin child still has a parent WindowProxy for messaging. This + // proxy lives in the child's realm and cannot reveal the parent global. + static v8::Intercepted get_restricted_parent_window_property( + v8::Local property, + const v8::PropertyCallbackInfo& info) + { + if (!property->IsString()) return v8::Intercepted::kNo; + auto isolate = info.GetIsolate(); + const auto name = to_utf8(isolate, property.As()); + if (name == "window" || name == "self" || name == "frames" + || name == "parent") { + info.GetReturnValue().Set(info.Holder()); + } else if (name == "top") { + v8::Local top; + if (info.Holder()->GetPrivate( + isolate->GetCurrentContext(), + v8::Private::ForApi(isolate, js_string(isolate, "webscene.parentTop"))) + .ToLocal(&top) && top->IsObject()) { + info.GetReturnValue().Set(top); + } else { + info.GetReturnValue().Set(info.Holder()); + } + } else if (name == "postMessage") { + info.GetReturnValue().Set(v8::Function::New( + isolate->GetCurrentContext(), window_post_message).ToLocalChecked()); + } else if (name == "closed") { + const auto* frame = unwrap_node(info.Holder()); + info.GetReturnValue().Set(v8::Boolean::New( + isolate, frame != nullptr && frame->parent == nullptr)); + } else if (name == "length") { + info.GetReturnValue().Set(v8::Integer::New(isolate, 0)); + } else if (name == "opener") { + info.GetReturnValue().Set(v8::Null(isolate)); + } else { + throw_dom_exception(isolate, + "Blocked a frame with a different origin from accessing this Window", + "SecurityError"); + } + return v8::Intercepted::kYes; + } + + static v8::Intercepted set_restricted_parent_window_property( + v8::Local, v8::Local, + const v8::PropertyCallbackInfo& info) + { + throw_dom_exception(info.GetIsolate(), + "Blocked a frame with a different origin from modifying this Window", + "SecurityError"); + return v8::Intercepted::kYes; + } + static v8::Intercepted get_frame_window_proxy_named_property( v8::Local property, const v8::PropertyCallbackInfo& info) diff --git a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_resource_tests.inc b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_resource_tests.inc index c2418b88..4f224efb 100644 --- a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_resource_tests.inc +++ b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_resource_tests.inc @@ -897,6 +897,102 @@ void test_same_origin_iframe_navigation_document_replacement() webscene_engine_destroy(engine); } +void test_isolated_child_parent_messaging() +{ + constexpr std::string_view address{"https://parent-message.test/index.html"}; + resource_server server{.content = { + {std::string(address), ""}, + {"https://isolated-message.test/pre/index.html", R"HTML( + )HTML"}, + {"https://nested-message.test/frame.html", R"HTML( + )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 parent message engine creation failed"); + require(webscene_engine_load_url(engine, address.data(), address.size()) != 0U, + "isolated parent message outer document failed"); + require(evaluate_until_equals(engine, "location.href", + "isolated-parent-document-ready.js", + R"("https://parent-message.test/index.html")", 2'500) + == R"("https://parent-message.test/index.html")", + "isolated parent message outer document did not become ready"); + const auto started = std::chrono::steady_clock::now(); + execute(engine, R"JS((() => { + const frame = document.createElement('iframe'); + frame.sandbox = 'allow-scripts allow-same-origin'; + frame.src = 'https://isolated-message.test/pre/index.html'; + window.__isolatedMessageFrame = frame; + window.__isolatedMessageResults = []; + window.addEventListener('message', event => { + window.__isolatedMessageResults.push({data:event.data, + origin:event.origin, sourceIsFrame:event.source === frame.contentWindow, + ports:event.ports.length}); + if (event.data?.ready && event.ports.length === 1) { + event.ports[0].postMessage('port-reply'); + } + }); + document.body.appendChild(frame); + })())JS", "isolated-parent-message-setup.js"); + require(evaluate_until_equals(engine, + "__isolatedMessageResults.length", "isolated-parent-message-wait.js", + "3", 2'500) == "3", + "isolated child did not transfer a port and relay nested messaging"); + const auto result = evaluate(engine, R"JS((() => ({ + parentDomDenied:__isolatedMessageFrame.contentDocument === null, + messages:__isolatedMessageResults + }))())JS", "isolated-parent-message-result.js"); + require(result == R"({"parentDomDenied":true,"messages":[{"data":{"ready":true,"denied":true,"mutationDenied":true,"parentIsTop":true,"frameHidden":true},"origin":"https://isolated-message.test","sourceIsFrame":true,"ports":1},{"data":{"reply":"port-reply"},"origin":"https://isolated-message.test","sourceIsFrame":true,"ports":0},{"data":{"nested":true,"origin":"https://nested-message.test","sourceIsNested":true,"parentIsNotTop":true,"parentDenied":true},"origin":"https://isolated-message.test","sourceIsFrame":true,"ports":0}]})", + "isolated child parent messaging leaked DOM, origin, source or port: " + result); + execute(engine, "__isolatedMessageFrame.remove()", + "isolated-parent-message-teardown.js"); + const auto elapsed = std::chrono::duration( + std::chrono::steady_clock::now() - started).count(); + require(elapsed < 1'000.0, "isolated parent messaging exceeded 1s budget"); + std::cout << "isolated child parent messaging " << elapsed << " ms\n"; + webscene_engine_destroy(engine); +} + void test_restricted_cross_origin_nested_window_proxy() { constexpr std::string_view address{ 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 e82a5772..4f323b5a 100644 --- a/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_tests.cpp +++ b/experiments/WebScene.NativeEngine.Probe/tests/native_v8_runtime_tests.cpp @@ -193,6 +193,10 @@ int main() test_restricted_cross_origin_nested_window_proxy(); return 0; } + if (selected == "iframe-cross-origin-parent-message") { + test_isolated_child_parent_messaging(); + return 0; + } if (selected == "query-iframe-worker-bootstrap") { test_query_iframe_worker_extension_host_bootstrap(); return 0;