Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support verifying a sender using messagingToken #28295

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -116,7 +116,8 @@ export class AmpViewerIntegration {
const port = new WindowPortEmulator(
this.win,
origin,
this.win.parent /* target */
this.win.parent /* target */,
messagingToken
);
return this.openChannelAndStart_(
viewer,
Expand Down
22 changes: 18 additions & 4 deletions extensions/amp-viewer-integration/0.1/messaging/messaging.js
Expand Up @@ -63,14 +63,17 @@ export class WindowPortEmulator {
* @param {!Window} win
* @param {string} origin
* @param {!Window} target
* @param {?string=} opt_token
*/
constructor(win, origin, target) {
constructor(win, origin, target, opt_token) {
/** @const @private {!Window} */
this.win_ = win;
/** @const @private {string} */
this.origin_ = origin;
/** @const @private {!Window} */
this.target_ = target;
/** @const @private {?string} */
this.token_ = opt_token || null;
}

/**
Expand All @@ -79,7 +82,11 @@ export class WindowPortEmulator {
*/
addEventListener(eventType, handler) {
this.win_.addEventListener('message', (event) => {
if (event.origin == this.origin_ && event.source == this.target_) {
if (
(event.data.messagingToken === this.token_ ||
event.origin == this.origin_) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small suggestion: if I understand correctly, this means having a messaging token completely disables the origin checking behaviour.

Would it be possible to keep both (i.e. change || to && here)? Then you can just use 'null' as the origin parameter when you create a WindowPortEmulator when there's no origin (this is what we do for the AMP for Email viewer).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually tried passing 'null' as the origin parameter #28225 (comment), but the problem is that when the WindowPortEmulator receives a message from the viewer's parent window, event.origin actually has the origin of the parent window, making this check fail.

Copy link

@molnarg molnarg May 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token_ is null if it was not specified, so a message with messagingToken=null could bypass the origin check, if the token was not expected.

Btw, other token checking is done in the Messaging class, not sure how this interacts with that, would be better to have all this logic in a single class. Also, the Messaging class implemented this check in a better way here without the flaw mentioned above:

if (
this.token_ &&
this.verifyToken_ &&
message.messagingToken !== this.token_
) {

event.source == this.target_
) {
handler(event);
}
});
Expand Down Expand Up @@ -161,9 +168,16 @@ export class Messaging {
* @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 {string=} verifyToken - if true, token above is verified on incoming messages rather than attached in outgoing messages
* @return {!Promise<!Messaging>}
*/
static waitForHandshakeFromDocument(source, target, origin, opt_token) {
static waitForHandshakeFromDocument(
source,
target,
origin,
opt_token,
verifyToken = true
) {
return new Promise((resolve) => {
const listener = (event) => {
const message = parseMessage(event.data);
Expand All @@ -183,7 +197,7 @@ export class Messaging {
port,
/* opt_isWebview */ false,
opt_token,
/* opt_verifyToken */ true
verifyToken
);
messaging.sendResponse_(message.requestid, CHANNEL_OPEN_MSG, null);
resolve(messaging);
Expand Down
@@ -1,6 +1,6 @@
{
"name": "@ampproject/viewer-messaging",
"version": "1.1.0",
"version": "1.1.1",
"description": "Messaging between an AMP Doc and a Viewer",
"author": "The AMP HTML Authors",
"license": "Apache-2.0",
Expand Down