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,50 @@ 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
+ // 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" , ( ) => {
249
274
expect ( ( ) => {
250
275
mockFrameWindow . trigger ( "message" , {
251
- protocol : "whatev" ,
252
- origin : "origin" ,
276
+ protocol : API_PROTOCOL ,
277
+ origin : HOST_ORIGIN ,
253
278
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" ,
257
283
} ,
258
284
} ) ;
259
285
} ) . not . toThrow ( ) ;
260
286
expect ( subscriptionCalled ) . toBe ( false ) ;
261
287
} ) ;
262
288
263
- it ( "should ignore messages from client applications " , ( ) => {
289
+ it ( "should ignore messages from invalid domains " , ( ) => {
264
290
expect ( ( ) => {
265
291
mockFrameWindow . trigger ( "message" , {
266
292
protocol : API_PROTOCOL ,
267
- origin : "origin" ,
293
+ origin : BAD_ORIGIN ,
268
294
data : {
269
295
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 " ,
273
299
} ,
274
300
} ) ;
275
301
} ) . not . toThrow ( ) ;
@@ -287,7 +313,7 @@ describe("client", () => {
287
313
receivedPayload = data . payload ;
288
314
} ) ;
289
315
mockFrameWindow . trigger ( "message" , {
290
- origin : "origin" ,
316
+ origin : HOST_ORIGIN ,
291
317
data : {
292
318
msgType : "publish" ,
293
319
msg : {
@@ -314,7 +340,7 @@ describe("client", () => {
314
340
receivedPayload = data . payload ;
315
341
} ) ;
316
342
mockFrameWindow . trigger ( "message" , {
317
- origin : "origin" ,
343
+ origin : HOST_ORIGIN ,
318
344
data : {
319
345
msgType : "publish" ,
320
346
msg : {
@@ -345,7 +371,7 @@ describe("client", () => {
345
371
client . start ( ) ;
346
372
347
373
mockFrameWindow . trigger ( "message" , {
348
- origin : "origin" ,
374
+ origin : HOST_ORIGIN ,
349
375
data : {
350
376
msgType : "env_init" ,
351
377
msg : testEnvironmentData ,
0 commit comments