Skip to content

Commit 70d148b

Browse files
committed
fix(client): fix message validation bug
This resolves an issue with message validation in the client COMUI-4097
1 parent e1127ca commit 70d148b

File tree

2 files changed

+57
-25
lines changed

2 files changed

+57
-25
lines changed

packages/iframe-coordinator/src/client.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ export class Client {
161161
private _onWindowMessage = (event: MessageEvent) => {
162162
let validated = null;
163163

164+
// Ignore messages from the wrong origin
165+
if (event.origin !== this._hostOrigin) {
166+
return;
167+
}
168+
169+
// Ignore messages from clients to hosts
164170
if (event.data && event.data.direction === "ClientToHost") {
165171
return;
166172
}

packages/iframe-coordinator/src/specs/client.spec.ts

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
import { EnvData, SetupData } from "../messages/Lifecycle";
99
import { Publication } from "../messages/Publication";
1010

11+
const HOST_ORIGIN = "https://example.com";
12+
const BAD_ORIGIN = "https://evil.com";
13+
1114
describe("client", () => {
1215
let client: any;
1316
let mockFrameWindow: any;
@@ -33,7 +36,7 @@ describe("client", () => {
3336
};
3437

3538
client = new Client({
36-
hostOrigin: "https://example.com",
39+
hostOrigin: HOST_ORIGIN,
3740
});
3841
client._clientWindow = mockFrameWindow;
3942
});
@@ -71,7 +74,7 @@ describe("client", () => {
7174
client.start();
7275

7376
mockFrameWindow.trigger("message", {
74-
origin: "origin",
77+
origin: HOST_ORIGIN,
7578
data: {
7679
msgType: "env_init",
7780
msg: testEnvironmentData,
@@ -205,17 +208,20 @@ describe("client", () => {
205208
beforeEach(() => {
206209
subscriptionCalled = false;
207210
client.start();
208-
client.messaging.addListener("origin", () => (subscriptionCalled = true));
211+
client.messaging.addListener(
212+
"myTopic",
213+
() => (subscriptionCalled = true),
214+
);
209215
});
210216

211-
it("should throw an exception on invalid iframe-coordinator message", () => {
217+
it("should throw an exception on invalid message type", () => {
212218
expect(() => {
213219
mockFrameWindow.trigger("message", {
214-
origin: "origin",
220+
origin: HOST_ORIGIN,
215221
data: {
216222
protocol: API_PROTOCOL,
217-
msgType: "test data",
218-
msg: "msg",
223+
msgType: "not valid type",
224+
msg: { topic: "myTopic", payload: "data" },
219225
direction: "HostToClient",
220226
},
221227
});
@@ -227,14 +233,15 @@ describe("client", () => {
227233
expect(subscriptionCalled).toBe(false);
228234
});
229235

230-
it("should throw an exception on invalid iframe-coordinator message with no direction", () => {
236+
it("should throw an exception on invalid message content", () => {
231237
expect(() => {
232238
mockFrameWindow.trigger("message", {
233-
origin: "origin",
239+
origin: HOST_ORIGIN,
234240
data: {
235241
protocol: API_PROTOCOL,
236-
msgType: "test data",
237-
msg: "msg",
242+
msgType: "publish",
243+
msg: { invalid: "yes" },
244+
direction: "HostToClient",
238245
},
239246
});
240247
}).toThrowMatching((err) => {
@@ -245,31 +252,50 @@ describe("client", () => {
245252
expect(subscriptionCalled).toBe(false);
246253
});
247254

248-
it("should not throw an exception if not from iframe-coordinator", () => {
255+
// it("should throw an exception on invalid iframe-coordinator message with no direction", () => {
256+
// expect(() => {
257+
// mockFrameWindow.trigger("message", {
258+
// origin: HOST_ORIGIN,
259+
// data: {
260+
// protocol: API_PROTOCOL,
261+
// msgType: "publish",
262+
// msg: { topic: "myTopic", payload: "data" },
263+
// },
264+
// });
265+
// }).toThrowMatching((err) => {
266+
// return err.message.startsWith(
267+
// "I received an invalid message from the host application",
268+
// );
269+
// });
270+
// expect(subscriptionCalled).toBe(false);
271+
// });
272+
273+
it("should ignore messages from other client applications", () => {
249274
expect(() => {
250275
mockFrameWindow.trigger("message", {
251-
protocol: "whatev",
252-
origin: "origin",
276+
protocol: API_PROTOCOL,
277+
origin: HOST_ORIGIN,
253278
data: {
254-
protocol: "whatev",
255-
msgType: "test data",
256-
msg: "msg",
279+
protocol: API_PROTOCOL,
280+
msgType: "publish",
281+
msg: { topic: "myTopic", payload: "data" },
282+
direction: "ClientToHost",
257283
},
258284
});
259285
}).not.toThrow();
260286
expect(subscriptionCalled).toBe(false);
261287
});
262288

263-
it("should ignore messages from client applications", () => {
289+
it("should ignore messages from invalid domains", () => {
264290
expect(() => {
265291
mockFrameWindow.trigger("message", {
266292
protocol: API_PROTOCOL,
267-
origin: "origin",
293+
origin: BAD_ORIGIN,
268294
data: {
269295
protocol: API_PROTOCOL,
270-
msgType: "invalid message type",
271-
msg: "msg",
272-
direction: "ClientToHost",
296+
msgType: "publish",
297+
msg: { topic: "myTopic", payload: "data" },
298+
direction: "HostToClient",
273299
},
274300
});
275301
}).not.toThrow();
@@ -287,7 +313,7 @@ describe("client", () => {
287313
receivedPayload = data.payload;
288314
});
289315
mockFrameWindow.trigger("message", {
290-
origin: "origin",
316+
origin: HOST_ORIGIN,
291317
data: {
292318
msgType: "publish",
293319
msg: {
@@ -314,7 +340,7 @@ describe("client", () => {
314340
receivedPayload = data.payload;
315341
});
316342
mockFrameWindow.trigger("message", {
317-
origin: "origin",
343+
origin: HOST_ORIGIN,
318344
data: {
319345
msgType: "publish",
320346
msg: {
@@ -345,7 +371,7 @@ describe("client", () => {
345371
client.start();
346372

347373
mockFrameWindow.trigger("message", {
348-
origin: "origin",
374+
origin: HOST_ORIGIN,
349375
data: {
350376
msgType: "env_init",
351377
msg: testEnvironmentData,

0 commit comments

Comments
 (0)