Skip to content

Commit b8328f9

Browse files
authored
fix(core): authenticate runtime control message senders (#3813)
1 parent 4f77c28 commit b8328f9

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎packages/core/src/runtime/bridge.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,54 @@ function createMockDeps() {
2727

2828
function makeControlMessage(action: string, extra?: Record<string, unknown>) {
2929
return new MessageEvent("message", {
30+
source: window.parent,
3031
data: { source: "hf-parent", type: "control", action, ...extra },
3132
});
3233
}
3334

3435
describe("installRuntimeControlBridge", () => {
36+
it("rejects foreign and null senders before dispatching controls", () => {
37+
const foreign = document.createElement("iframe");
38+
document.body.append(foreign);
39+
const deps = createMockDeps();
40+
const handler = installRuntimeControlBridge(deps);
41+
try {
42+
for (const source of [foreign.contentWindow, null]) {
43+
handler(
44+
new MessageEvent("message", {
45+
source,
46+
data: { source: "hf-parent", type: "control", action: "play" },
47+
}),
48+
);
49+
}
50+
expect(deps.onPlay).not.toHaveBeenCalled();
51+
handler(
52+
new MessageEvent("message", {
53+
source: window,
54+
data: { source: "hf-parent", type: "control", action: "play" },
55+
}),
56+
);
57+
expect(deps.onPlay).toHaveBeenCalledOnce();
58+
} finally {
59+
window.removeEventListener("message", handler);
60+
foreign.remove();
61+
}
62+
});
63+
it("accepts an embedding parent distinct from the runtime window", () => {
64+
const frame = document.createElement("iframe");
65+
document.body.append(frame);
66+
vi.stubGlobal("parent", frame.contentWindow);
67+
const deps = createMockDeps();
68+
const handler = installRuntimeControlBridge(deps);
69+
try {
70+
handler(makeControlMessage("play"));
71+
expect(deps.onPlay).toHaveBeenCalledOnce();
72+
} finally {
73+
window.removeEventListener("message", handler);
74+
vi.unstubAllGlobals();
75+
frame.remove();
76+
}
77+
});
3578
it("dispatches play command", () => {
3679
const deps = createMockDeps();
3780
const handler = installRuntimeControlBridge(deps);

‎packages/core/src/runtime/bridge.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ function handleFlashElements(data: BridgeControlData): void {
123123

124124
export function installRuntimeControlBridge(deps: BridgeDeps): (event: MessageEvent) => void {
125125
const handler = (event: MessageEvent) => {
126+
if (event.source !== window.parent && event.source !== window) return;
126127
const data = event.data as BridgeControlData | null;
127128
if (!data || data.source !== "hf-parent" || data.type !== "control") return;
128129
if (rejectUnsupportedProtocol(data)) return;

‎packages/core/src/runtime/init.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ describe("initSandboxRuntimeModular", () => {
169169

170170
window.dispatchEvent(
171171
new MessageEvent("message", {
172+
source: window.parent,
172173
data: { source: "hf-parent", type: "control", action: "set-volume", volume: 1 },
173174
}),
174175
);
@@ -239,6 +240,7 @@ describe("initSandboxRuntimeModular", () => {
239240
window.addEventListener("error", onError);
240241
window.dispatchEvent(
241242
new MessageEvent("message", {
243+
source: window.parent,
242244
data: { source: "hf-parent", type: "control", action: "set-volume", volume: 1 },
243245
}),
244246
);
@@ -2846,6 +2848,7 @@ describe("initSandboxRuntimeModular", () => {
28462848
if (payload.source !== "hf-preview" || payload.type !== "ready") return;
28472849
window.dispatchEvent(
28482850
new MessageEvent("message", {
2851+
source: window.parent,
28492852
data: {
28502853
source: "hf-parent",
28512854
type: "control",
@@ -2856,6 +2859,7 @@ describe("initSandboxRuntimeModular", () => {
28562859
);
28572860
window.dispatchEvent(
28582861
new MessageEvent("message", {
2862+
source: window.parent,
28592863
data: {
28602864
source: "hf-parent",
28612865
type: "control",
@@ -3049,6 +3053,7 @@ describe("initSandboxRuntimeModular", () => {
30493053

30503054
window.dispatchEvent(
30513055
new MessageEvent("message", {
3056+
source: window.parent,
30523057
data: { source: "hf-parent", type: "control", action: "set-muted", muted: false },
30533058
}),
30543059
);
@@ -3058,6 +3063,7 @@ describe("initSandboxRuntimeModular", () => {
30583063

30593064
window.dispatchEvent(
30603065
new MessageEvent("message", {
3066+
source: window.parent,
30613067
data: { source: "hf-parent", type: "control", action: "set-muted", muted: true },
30623068
}),
30633069
);
@@ -3067,6 +3073,7 @@ describe("initSandboxRuntimeModular", () => {
30673073

30683074
window.dispatchEvent(
30693075
new MessageEvent("message", {
3076+
source: window.parent,
30703077
data: { source: "hf-parent", type: "control", action: "set-muted", muted: false },
30713078
}),
30723079
);
@@ -3100,6 +3107,7 @@ describe("initSandboxRuntimeModular", () => {
31003107

31013108
window.dispatchEvent(
31023109
new MessageEvent("message", {
3110+
source: window.parent,
31033111
data: {
31043112
source: "hf-parent",
31053113
type: "control",
@@ -3148,6 +3156,7 @@ describe("initSandboxRuntimeModular", () => {
31483156

31493157
window.dispatchEvent(
31503158
new MessageEvent("message", {
3159+
source: window.parent,
31513160
data: {
31523161
source: "hf-parent",
31533162
type: "control",

0 commit comments

Comments
 (0)