-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathmessaging.js
483 lines (452 loc) · 14.3 KB
/
messaging.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
const TAG = 'amp-viewer-messaging';
const CHANNEL_OPEN_MSG = 'channelOpen';
const HANDSHAKE_POLL_MSG = 'handshake-poll';
const APP = '__AMPHTML__';
/**
* @enum {string}
*/
const MessageType_Enum = {
REQUEST: 'q',
RESPONSE: 's',
};
/**
* @typedef {function(string, *, boolean):(!Promise<*>|undefined)}
*/
let RequestHandler; // eslint-disable-line @typescript-eslint/no-unused-vars
/**
* @param {*} message
* @return {?AmpViewerMessage}
*/
export function parseMessage(message) {
if (typeof message != 'string') {
return /** @type {AmpViewerMessage} */ (message);
}
if (message.charAt(0) != '{') {
return null;
}
try {
return /** @type {?AmpViewerMessage} */ (
JSON.parse(/** @type {string} */ (message))
);
} catch (e) {
return null;
}
}
/**
* @fileoverview This class is a de-facto implementation of MessagePort
* from Channel Messaging API:
* https://developer.mozilla.org/en-US/docs/Web/API/Channel_Messaging_API
*/
export class WindowPortEmulator {
/**
* @param {!Window} win
* @param {string} origin
* @param {!Window} target
*/
constructor(win, origin, target) {
/** @const @private {!Window} */
this.win_ = win;
/** @const @private {string} */
this.origin_ = origin;
/** @const @private {!Window} */
this.target_ = target;
}
/**
* @param {string} eventType
* @param {function(!Event):*} handler
*/
addEventListener(eventType, handler) {
this.win_.addEventListener('message', (event) => {
if (event.origin == this.origin_ && event.source == this.target_) {
handler(event);
}
});
}
/**
* @param {JsonObject} data
*/
postMessage(data) {
// Opaque (null) origin can only receive messages sent to "*"
const targetOrigin = this.origin_ === 'null' ? '*' : this.origin_;
this.target_./*OK*/ postMessage(data, targetOrigin);
}
/**
* Starts the sending of messages queued on the port.
*/
start() {}
}
/**
* @fileoverview This is used in amp-viewer-integration.js for the
* communication protocol between AMP and the viewer. In the comments, I will
* refer to the communication as a conversation between me and Bob. The
* messaging protocol should support both sides, but at this point I'm the
* ampdoc and Bob is the viewer.
*/
export class Messaging {
/**
* Performs a handshake and initializes messaging.
*
* Requires the `handshakepoll` viewer capability and the `origin` viewer parameter to be specified.
* @param {!Window} target - window containing AMP document to perform handshake with
* @param {?string=} opt_token - message token to verify on incoming messages (must be provided as viewer parameter)
* @return {!Promise<!Messaging>}
*/
static initiateHandshakeWithDocument(target, opt_token) {
return new Promise((resolve) => {
const intervalRef = setInterval(() => {
const channel = new MessageChannel();
const pollMessage = /** @type {JsonObject} */ ({
app: APP,
name: HANDSHAKE_POLL_MSG,
});
target./*OK*/ postMessage(pollMessage, '*', [channel.port2]);
const port = channel.port1;
const listener = (event) => {
const message = parseMessage(event.data);
if (!message) {
return;
}
if (message.app === APP && message.name === CHANNEL_OPEN_MSG) {
clearInterval(intervalRef);
port.removeEventListener('message', listener);
const messaging = new Messaging(
null,
port,
/* opt_isWebview */ false,
opt_token,
/* opt_verifyToken */ true
);
messaging.sendResponse_(message.requestid, CHANNEL_OPEN_MSG, null);
resolve(messaging);
}
};
port.addEventListener('message', listener);
port.start();
}, 1000);
});
}
/**
* Waits for handshake from iframe and initializes messaging.
*
* Requires the `origin` viewer parameter to be specified.
* @param {!Window} source - the source window containing the viewer
* @param {!Window} target - window containing AMP document to perform handshake with (usually contentWindow of iframe)
* @param {string} origin - origin of target window (use "null" if opaque)
* @param {?string=} opt_token - message token to verify on incoming messages (must be provided as viewer parameter)
* @param {?RegExp=} opt_cdnProxyRegex
* @return {!Promise<!Messaging>}
*/
static waitForHandshakeFromDocument(
source,
target,
origin,
opt_token,
opt_cdnProxyRegex
) {
return new Promise((resolve) => {
const listener = (event) => {
const message = parseMessage(event.data);
if (!message) {
return;
}
if (
(event.origin == origin ||
(opt_cdnProxyRegex && opt_cdnProxyRegex.test(event.origin))) &&
(!event.source || event.source == target) &&
message.app === APP &&
message.name === CHANNEL_OPEN_MSG
) {
source.removeEventListener('message', listener);
const port = new WindowPortEmulator(source, event.origin, target);
const messaging = new Messaging(
null,
port,
/* opt_isWebview */ false,
opt_token,
/* opt_verifyToken */ true
);
messaging.sendResponse_(message.requestid, CHANNEL_OPEN_MSG, null);
resolve(messaging);
}
};
source.addEventListener('message', listener);
});
}
/**
* Conversation (messaging protocol) between me and Bob.
* @param {?Window} win
* @param {!MessagePort|!WindowPortEmulator} port
* @param {boolean=} opt_isWebview
* @param {?string=} opt_token
* @param {boolean=} opt_verifyToken
*/
constructor(win, port, opt_isWebview, opt_token, opt_verifyToken) {
/** @const @private {?Window} */
this.win_ = win;
/** @const @private {!MessagePort|!WindowPortEmulator} */
this.port_ = port;
/** @const @private {boolean} */
this.isWebview_ = !!opt_isWebview;
/**
* A token that the viewer may include as an init parameter to enhance
* security for communication to opaque origin (a.k.a. null origin) AMP
* documents.
*
* For an AMP document embedded inside a sandbox iframe, the origin of the
* document would be "null", which defeats the purpose of an origin check.
* An attacker could simply create a sandboxed, malicious iframe (therefore
* having null origin), walk on the DOM frame tree to find a reference to
* the viewer iframe (this is not constrained by the same origin policy),
* and then send postMessage() calls to the viewer frame and pass the
* viewer's origin checks, if any.
*
* The viewer could also check the source of the message to be a legitimate
* AMP iframe window, but the attacker could bypass that by navigating the
* legitimate AMP iframe window away to a malicious document. Recent
* browsers have banned this kind of attack, but it's tricky to rely on it.
*
* To prevent the above attack in a null origin AMP document, the viewer
* should include this token in an init parameter, either in the `src` or
* `name` attribute of the iframe, and then verify that this token is
* included in all the messages sent from AMP to the viewer. The attacker
* would not be able to steal this token under the same origin policy,
* because the token is inside the viewer document at a different origin
* and the attacker can't access it.
* @const @private {?string}
*/
this.token_ = opt_token || null;
/**
* If true, the token above is verified on incoming messages instead of
* being attached to outgoing messages.
* @const @private {boolean}
*/
this.verifyToken_ = !!opt_verifyToken;
/** @private {number} */
this.requestIdCounter_ = 0;
/** @private {!Object<number, {resolve: function(*), reject: function(!Error)}>} */
this.waitingForResponse_ = {};
/**
* A map from message names to request handlers.
* @private {!Object<string, !RequestHandler>}
*/
this.messageHandlers_ = {};
/** @private {?RequestHandler} */
this.defaultHandler_ = null;
this.port_.addEventListener('message', this.handleMessage_.bind(this));
this.port_.start();
}
/**
* Registers a method that will handle requests sent to the specified
* message name.
* @param {string} messageName The name of the message to handle.
* @param {!RequestHandler} requestHandler
*/
registerHandler(messageName, requestHandler) {
this.messageHandlers_[messageName] = requestHandler;
}
/**
* Unregisters the handler for the specified message name.
* @param {string} messageName The name of the message to unregister.
*/
unregisterHandler(messageName) {
delete this.messageHandlers_[messageName];
}
/**
* @param {?RequestHandler} requestHandler
*/
setDefaultHandler(requestHandler) {
this.defaultHandler_ = requestHandler;
}
/**
* Bob sent me a message. I need to decide if it's a new request or
* a response to a previous 'conversation' we were having.
* @param {!Event} event
* @private
*/
handleMessage_(event) {
const message = parseMessage(event.data);
if (!message || message.app !== APP) {
return;
}
if (
this.token_ &&
this.verifyToken_ &&
message.messagingToken !== this.token_
) {
// We received a message with an invalid token - dismiss it.
this.logError_(TAG + ': handleMessage_ error: ', 'invalid token');
return;
}
if (message.type === MessageType_Enum.REQUEST) {
this.handleRequest_(message);
} else if (message.type === MessageType_Enum.RESPONSE) {
this.handleResponse_(message);
}
}
/**
* I'm sending Bob a new outgoing request.
* @param {string} messageName
* @param {?JsonObject|string|undefined} messageData
* @param {boolean} awaitResponse
* @return {!Promise<*>|undefined}
*/
sendRequest(messageName, messageData, awaitResponse) {
const requestId = ++this.requestIdCounter_;
let promise = undefined;
if (awaitResponse) {
promise = new Promise((resolve, reject) => {
this.waitingForResponse_[requestId] = {resolve, reject};
});
}
this.sendMessage_(
/** @type {!AmpViewerMessage} */ ({
app: APP,
requestid: requestId,
type: MessageType_Enum.REQUEST,
name: messageName,
data: messageData,
rsvp: awaitResponse,
})
);
return promise;
}
/**
* I'm responding to a request that Bob made earlier.
* @param {number} requestId
* @param {string} messageName
* @param {*} messageData
* @private
*/
sendResponse_(requestId, messageName, messageData) {
this.sendMessage_(
/** @type {!AmpViewerMessage} */ ({
app: APP,
requestid: requestId,
type: MessageType_Enum.RESPONSE,
name: messageName,
data: messageData,
})
);
}
/**
* @param {number} requestId
* @param {string} messageName
* @param {*} reason !Error most of time, string sometimes, * rarely.
* @private
*/
sendResponseError_(requestId, messageName, reason) {
const errString = this.errorToString_(reason);
this.logError_(
TAG + ': sendResponseError_, message name: ' + messageName,
errString
);
this.sendMessage_(
/** @type {!AmpViewerMessage} */ ({
app: APP,
requestid: requestId,
type: MessageType_Enum.RESPONSE,
name: messageName,
data: null,
error: errString,
})
);
}
/**
* @param {!AmpViewerMessage} message
* @private
*/
sendMessage_(message) {
const /** Object<string, *> */ finalMessage = Object.assign(message, {});
if (this.token_ && !this.verifyToken_) {
finalMessage.messagingToken = this.token_;
}
this.port_./*OK*/ postMessage(
this.isWebview_
? JSON.stringify(/** @type {!JsonObject} */ (finalMessage))
: finalMessage
);
}
/**
* I'm handling an incoming request from Bob. I'll either respond normally
* (ex: "got it Bob!") or with an error (ex: "I didn't get a word of what
* you said!").
* @param {!AmpViewerMessage} message
* @private
*/
handleRequest_(message) {
let handler = this.messageHandlers_[message.name];
if (!handler) {
handler = this.defaultHandler_;
}
if (!handler) {
const error = new Error(
'Cannot handle request because no default handler is set!'
);
error.args = message.name;
throw error;
}
const promise = handler(message.name, message.data, !!message.rsvp);
if (message.rsvp) {
const requestId = message.requestid;
if (!promise) {
this.sendResponseError_(
requestId,
message.name,
new Error('no response')
);
throw new Error('expected response but none given: ' + message.name);
}
promise.then(
(data) => {
this.sendResponse_(requestId, message.name, data);
},
(reason) => {
this.sendResponseError_(requestId, message.name, reason);
}
);
}
}
/**
* I sent out a request to Bob. He responded. And now I'm handling that
* response.
* @param {!AmpViewerMessage} message
* @private
*/
handleResponse_(message) {
const requestId = message.requestid;
const pending = this.waitingForResponse_[requestId];
if (pending) {
delete this.waitingForResponse_[requestId];
if (message.error) {
this.logError_(TAG + ': handleResponse_ error: ', message.error);
pending.reject(
new Error(`Request ${message.name} failed: ${message.error}`)
);
} else {
pending.resolve(message.data);
}
}
}
/**
* @param {string} state
* @param {!Error|string=} opt_data
* @private
*/
logError_(state, opt_data) {
if (!this.win_) {
return;
}
let stateStr = 'amp-messaging-error-logger: ' + state;
const dataStr = ' data: ' + this.errorToString_(opt_data);
stateStr += dataStr;
this.win_['viewerState'] = stateStr;
}
/**
* @param {*} err !Error most of time, string sometimes, * rarely.
* @return {string}
* @private
*/
errorToString_(err) {
return err ? (err.message ? err.message : String(err)) : 'unknown error';
}
}