Skip to content

Commit 9e09de4

Browse files
authored
Merge pull request #294 from purecloudlabs/COMUI-4097
fix(client): fix message validation bug
2 parents e1127ca + bef05ca commit 9e09de4

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-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: 52 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,51 @@ describe("client", () => {
245252
expect(subscriptionCalled).toBe(false);
246253
});
247254

248-
it("should not throw an exception if not from iframe-coordinator", () => {
255+
// Fix this in next major release, holding off for now in case of compat issues
256+
// it("should throw an exception on invalid iframe-coordinator message with no direction", () => {
257+
// expect(() => {
258+
// mockFrameWindow.trigger("message", {
259+
// origin: HOST_ORIGIN,
260+
// data: {
261+
// protocol: API_PROTOCOL,
262+
// msgType: "publish",
263+
// msg: { topic: "myTopic", payload: "data" },
264+
// },
265+
// });
266+
// }).toThrowMatching((err) => {
267+
// return err.message.startsWith(
268+
// "I received an invalid message from the host application",
269+
// );
270+
// });
271+
// expect(subscriptionCalled).toBe(false);
272+
// });
273+
274+
it("should ignore messages from other client applications", () => {
249275
expect(() => {
250276
mockFrameWindow.trigger("message", {
251-
protocol: "whatev",
252-
origin: "origin",
277+
protocol: API_PROTOCOL,
278+
origin: HOST_ORIGIN,
253279
data: {
254-
protocol: "whatev",
255-
msgType: "test data",
256-
msg: "msg",
280+
protocol: API_PROTOCOL,
281+
msgType: "publish",
282+
msg: { topic: "myTopic", payload: "data" },
283+
direction: "ClientToHost",
257284
},
258285
});
259286
}).not.toThrow();
260287
expect(subscriptionCalled).toBe(false);
261288
});
262289

263-
it("should ignore messages from client applications", () => {
290+
it("should ignore messages from invalid domains", () => {
264291
expect(() => {
265292
mockFrameWindow.trigger("message", {
266293
protocol: API_PROTOCOL,
267-
origin: "origin",
294+
origin: BAD_ORIGIN,
268295
data: {
269296
protocol: API_PROTOCOL,
270-
msgType: "invalid message type",
271-
msg: "msg",
272-
direction: "ClientToHost",
297+
msgType: "publish",
298+
msg: { topic: "myTopic", payload: "data" },
299+
direction: "HostToClient",
273300
},
274301
});
275302
}).not.toThrow();
@@ -287,7 +314,7 @@ describe("client", () => {
287314
receivedPayload = data.payload;
288315
});
289316
mockFrameWindow.trigger("message", {
290-
origin: "origin",
317+
origin: HOST_ORIGIN,
291318
data: {
292319
msgType: "publish",
293320
msg: {
@@ -314,7 +341,7 @@ describe("client", () => {
314341
receivedPayload = data.payload;
315342
});
316343
mockFrameWindow.trigger("message", {
317-
origin: "origin",
344+
origin: HOST_ORIGIN,
318345
data: {
319346
msgType: "publish",
320347
msg: {
@@ -345,7 +372,7 @@ describe("client", () => {
345372
client.start();
346373

347374
mockFrameWindow.trigger("message", {
348-
origin: "origin",
375+
origin: HOST_ORIGIN,
349376
data: {
350377
msgType: "env_init",
351378
msg: testEnvironmentData,

0 commit comments

Comments
 (0)