Skip to content

Commit 4f7581a

Browse files
committed
test: fix origin validation test to actually verify rejection
Replaced the vacuous 'sandbox logs indicate origin validation is active' test (which had an assertion that always passed: length >= 0) with a proper test that: 1. Injects a message from the wrong source (page context, not parent) 2. Verifies that PostMessageTransport logs 'Ignoring message from unknown source' This actually tests that the source validation in PostMessageTransport is working correctly.
1 parent 1a94ba4 commit 4f7581a

1 file changed

Lines changed: 33 additions & 9 deletions

File tree

tests/e2e/security.spec.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,25 +194,49 @@ test.describe("Host Resilience", () => {
194194
});
195195

196196
test.describe("Origin Validation Infrastructure", () => {
197-
test("sandbox logs indicate origin validation is active", async ({
197+
test("PostMessageTransport rejects messages from wrong source", async ({
198198
page,
199199
}) => {
200-
// Capture all sandbox logs to verify the security infrastructure is working
201-
const allLogs: string[] = [];
200+
// Capture rejection logs from the app's PostMessageTransport
201+
const rejectionLogs: string[] = [];
202202
page.on("console", (msg) => {
203-
allLogs.push(msg.text());
203+
const text = msg.text();
204+
if (text.includes("Ignoring message from unknown source")) {
205+
rejectionLogs.push(text);
206+
}
204207
});
205208

206209
await loadServer(page, "Integration Test Server");
207210

208-
// App should load successfully (proves origin validation passed)
209211
const appFrame = getAppFrame(page);
210212
await expect(appFrame.locator("body")).toBeVisible();
211213

212-
// The sandbox should have logged CSP-related info
213-
const cspLogs = allLogs.filter((log) => log.includes("CSP"));
214-
// CSP logging is expected (either "Received CSP" or "No CSP provided")
215-
expect(cspLogs.length).toBeGreaterThanOrEqual(0); // May or may not have CSP
214+
// Inject a message from the page context (wrong source - not window.parent)
215+
// The app's PostMessageTransport should reject it because event.source
216+
// won't match the expected source (window.parent)
217+
await page.evaluate(() => {
218+
const outerIframe = document.querySelector("iframe");
219+
if (!outerIframe?.contentWindow) return;
220+
221+
const innerIframe = outerIframe.contentDocument?.querySelector("iframe");
222+
if (!innerIframe?.contentWindow) return;
223+
224+
// Send a fake JSON-RPC message from the page (not from parent)
225+
innerIframe.contentWindow.postMessage(
226+
{
227+
jsonrpc: "2.0",
228+
method: "test/injected",
229+
id: 999,
230+
},
231+
"*",
232+
);
233+
});
234+
235+
// Wait for message to be processed
236+
await page.waitForTimeout(500);
237+
238+
// The PostMessageTransport should have logged the rejection
239+
expect(rejectionLogs.length).toBeGreaterThan(0);
216240
});
217241

218242
test("app communication completes round-trip successfully", async ({

0 commit comments

Comments
 (0)