Skip to content

Commit f99592f

Browse files
committed
validate messages from app come from same origin as sandbox proxy
1 parent 507fbbf commit f99592f

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

examples/basic-host/src/sandbox.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ if (!document.referrer.match(ALLOWED_REFERRER_PATTERN)) {
2020
// This is the origin we expect all parent messages to come from.
2121
const EXPECTED_HOST_ORIGIN = new URL(document.referrer).origin;
2222

23+
const OWN_ORIGIN = new URL(window.location.href).origin;
24+
2325
// Security self-test: verify iframe isolation is working correctly.
2426
// This MUST throw a SecurityError -- if `window.top` is accessible, the sandbox
2527
// configuration is dangerously broken and untrusted content could escape.
@@ -126,6 +128,15 @@ window.addEventListener("message", async (event) => {
126128
}
127129
}
128130
} else if (event.source === inner.contentWindow) {
131+
if (event.origin !== OWN_ORIGIN) {
132+
console.error(
133+
"[Sandbox] Rejecting message from inner iframe with unexpected origin:",
134+
event.origin,
135+
"expected:",
136+
OWN_ORIGIN
137+
);
138+
return;
139+
}
129140
// Relay messages from inner frame to parent window.
130141
// Use specific origin instead of "*" to prevent message interception.
131142
window.parent.postMessage(event.data, EXPECTED_HOST_ORIGIN);

0 commit comments

Comments
 (0)