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
23 changes: 23 additions & 0 deletions js/packages/truapi/src/sandbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ describe("sandbox iframe MessagePort handshake", () => {
expect(currentWindow.listeners.size).toBe(0);
});

it('treats a masked "null" ancestor origin as hidden and pings with the wildcard', async () => {
// Firefox implements location.ancestorOrigins but serializes cross-origin
// ancestors as "null", which is not a valid postMessage targetOrigin.
currentWindow = installFakeIframeWindow({ ancestorOrigins: ["null"] });
const sandbox = await importSandbox();

expect(sandbox.getClientSync()).not.toBeNull();
expect(currentWindow.parentPostMessage.mock.calls).toEqual([
[{ type: "truapi-ready" }, "*"],
]);

const accepted = trackChannel();
currentWindow.dispatch({
source: currentWindow.parent,
origin: "https://host.example",
data: { type: "truapi-init" },
ports: [accepted.port1],
});
await Promise.resolve();
expect(currentWindow.win.__HOST_API_PORT__).toBe(accepted.port1);
expect(currentWindow.listeners.size).toBe(0);
});

it("uses a data-free wildcard ready ping only when the host origin is hidden", async () => {
currentWindow = installFakeIframeWindow({});
const sandbox = await importSandbox();
Expand Down
6 changes: 4 additions & 2 deletions js/packages/truapi/src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ function resolveHostOrigin(): string | null {
// Fall through to ancestorOrigins.
}
}
const ancestors = window.location?.ancestorOrigins;
if (ancestors && ancestors.length > 0) return ancestors[0] ?? null;
// Firefox serializes cross-origin ancestors as "null", which is not a
// valid postMessage targetOrigin; treat it as an unknown host origin.
const ancestor = window.location?.ancestorOrigins?.[0];
if (ancestor && ancestor !== "null") return ancestor;
return null;
}

Expand Down