8
8
import { EnvData , SetupData } from "../messages/Lifecycle" ;
9
9
import { Publication } from "../messages/Publication" ;
10
10
11
+ const HOST_ORIGIN = "https://example.com" ;
12
+ const BAD_ORIGIN = "https://evil.com" ;
13
+
11
14
describe ( "client" , ( ) => {
12
15
let client : any ;
13
16
let mockFrameWindow : any ;
@@ -33,7 +36,7 @@ describe("client", () => {
33
36
} ;
34
37
35
38
client = new Client ( {
36
- hostOrigin : "https://example.com" ,
39
+ hostOrigin : HOST_ORIGIN ,
37
40
} ) ;
38
41
client . _clientWindow = mockFrameWindow ;
39
42
} ) ;
@@ -71,7 +74,7 @@ describe("client", () => {
71
74
client . start ( ) ;
72
75
73
76
mockFrameWindow . trigger ( "message" , {
74
- origin : "origin" ,
77
+ origin : HOST_ORIGIN ,
75
78
data : {
76
79
msgType : "env_init" ,
77
80
msg : testEnvironmentData ,
@@ -205,17 +208,20 @@ describe("client", () => {
205
208
beforeEach ( ( ) => {
206
209
subscriptionCalled = false ;
207
210
client . start ( ) ;
208
- client . messaging . addListener ( "origin" , ( ) => ( subscriptionCalled = true ) ) ;
211
+ client . messaging . addListener (
212
+ "myTopic" ,
213
+ ( ) => ( subscriptionCalled = true ) ,
214
+ ) ;
209
215
} ) ;
210
216
211
- it ( "should throw an exception on invalid iframe-coordinator message" , ( ) => {
217
+ it ( "should throw an exception on invalid message type " , ( ) => {
212
218
expect ( ( ) => {
213
219
mockFrameWindow . trigger ( "message" , {
214
- origin : "origin" ,
220
+ origin : HOST_ORIGIN ,
215
221
data : {
216
222
protocol : API_PROTOCOL ,
217
- msgType : "test data " ,
218
- msg : "msg" ,
223
+ msgType : "not valid type " ,
224
+ msg : { topic : "myTopic" , payload : "data" } ,
219
225
direction : "HostToClient" ,
220
226
} ,
221
227
} ) ;
@@ -227,14 +233,15 @@ describe("client", () => {
227
233
expect ( subscriptionCalled ) . toBe ( false ) ;
228
234
} ) ;
229
235
230
- it ( "should throw an exception on invalid iframe-coordinator message with no direction " , ( ) => {
236
+ it ( "should throw an exception on invalid message content " , ( ) => {
231
237
expect ( ( ) => {
232
238
mockFrameWindow . trigger ( "message" , {
233
- origin : "origin" ,
239
+ origin : HOST_ORIGIN ,
234
240
data : {
235
241
protocol : API_PROTOCOL ,
236
- msgType : "test data" ,
237
- msg : "msg" ,
242
+ msgType : "publish" ,
243
+ msg : { invalid : "yes" } ,
244
+ direction : "HostToClient" ,
238
245
} ,
239
246
} ) ;
240
247
} ) . toThrowMatching ( ( err ) => {
@@ -245,31 +252,51 @@ describe("client", () => {
245
252
expect ( subscriptionCalled ) . toBe ( false ) ;
246
253
} ) ;
247
254
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" , ( ) => {
249
275
expect ( ( ) => {
250
276
mockFrameWindow . trigger ( "message" , {
251
- protocol : "whatev" ,
252
- origin : "origin" ,
277
+ protocol : API_PROTOCOL ,
278
+ origin : HOST_ORIGIN ,
253
279
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" ,
257
284
} ,
258
285
} ) ;
259
286
} ) . not . toThrow ( ) ;
260
287
expect ( subscriptionCalled ) . toBe ( false ) ;
261
288
} ) ;
262
289
263
- it ( "should ignore messages from client applications " , ( ) => {
290
+ it ( "should ignore messages from invalid domains " , ( ) => {
264
291
expect ( ( ) => {
265
292
mockFrameWindow . trigger ( "message" , {
266
293
protocol : API_PROTOCOL ,
267
- origin : "origin" ,
294
+ origin : BAD_ORIGIN ,
268
295
data : {
269
296
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 " ,
273
300
} ,
274
301
} ) ;
275
302
} ) . not . toThrow ( ) ;
@@ -287,7 +314,7 @@ describe("client", () => {
287
314
receivedPayload = data . payload ;
288
315
} ) ;
289
316
mockFrameWindow . trigger ( "message" , {
290
- origin : "origin" ,
317
+ origin : HOST_ORIGIN ,
291
318
data : {
292
319
msgType : "publish" ,
293
320
msg : {
@@ -314,7 +341,7 @@ describe("client", () => {
314
341
receivedPayload = data . payload ;
315
342
} ) ;
316
343
mockFrameWindow . trigger ( "message" , {
317
- origin : "origin" ,
344
+ origin : HOST_ORIGIN ,
318
345
data : {
319
346
msgType : "publish" ,
320
347
msg : {
@@ -345,7 +372,7 @@ describe("client", () => {
345
372
client . start ( ) ;
346
373
347
374
mockFrameWindow . trigger ( "message" , {
348
- origin : "origin" ,
375
+ origin : HOST_ORIGIN ,
349
376
data : {
350
377
msgType : "env_init" ,
351
378
msg : testEnvironmentData ,
0 commit comments