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
45 changes: 45 additions & 0 deletions docs/validation/isolated-parent-postmessage-894-264.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Value> parent_window = v8::Null(isolate);
v8::Local<v8::Value> top_window = v8::Null(isolate);
if (same_origin_access) {
parent_window = context.Get(isolate)->Global();
for (auto* ancestor = frame.parent;
Expand All @@ -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<v8::Value>(context.Get(isolate)->Global())
: v8::Local<v8::Value>(v8::Null(isolate))).Check();
top_window).Check();
global->Set(
local_context,
js_string(isolate, "frameElement"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Value>& info)
{
if (!property->IsString()) return v8::Intercepted::kNo;
auto isolate = info.GetIsolate();
const auto name = to_utf8(isolate, property.As<v8::Value>());
if (name == "window" || name == "self" || name == "frames"
|| name == "parent") {
info.GetReturnValue().Set(info.Holder());
} else if (name == "top") {
v8::Local<v8::Value> 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::Name>, v8::Local<v8::Value>,
const v8::PropertyCallbackInfo<void>& 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<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Value>& info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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), "<!doctype html><body></body>"},
{"https://isolated-message.test/pre/index.html", R"HTML(
<!doctype html><body><script>
let denied = false;
try { void parent.document; }
catch (error) { denied = error.name === 'SecurityError'; }
let mutationDenied = false;
try { parent.document = document; }
catch (error) { mutationDenied = error.name === 'SecurityError'; }
const channel = new MessageChannel();
channel.port1.onmessage = event => {
parent.postMessage({reply:event.data}, 'https://parent-message.test');
const nested = document.createElement('iframe');
nested.sandbox = 'allow-scripts allow-same-origin';
nested.src = 'https://nested-message.test/frame.html';
addEventListener('message', message => {
if (message.data?.nestedReady) {
parent.postMessage({nested:true,
origin:message.origin,
sourceIsNested:message.source === nested.contentWindow,
parentIsNotTop:message.data.parentIsNotTop,
parentDenied:message.data.parentDenied},
'https://parent-message.test');
}
});
document.body.appendChild(nested);
};
parent.postMessage('wrong-target', 'https://other-message.test');
parent.postMessage({ready:true, denied, mutationDenied,
parentIsTop:parent === top, frameHidden:frameElement === null},
'https://parent-message.test', [channel.port2]);
</script></body>)HTML"},
{"https://nested-message.test/frame.html", R"HTML(
<!doctype html><body><script>
let parentDenied = false;
try { void parent.document; }
catch (error) { parentDenied = error.name === 'SecurityError'; }
top.postMessage('wrong-top', 'https://other-message.test');
parent.postMessage({nestedReady:true,
parentIsNotTop:parent !== top, parentDenied},
'https://isolated-message.test');
</script></body>)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<double, std::milli>(
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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading