Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/service/standard-actions-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ export class StandardActions {
/** @const @private {!./viewport/viewport-impl.Viewport} */
this.viewport_ = Services.viewportForDoc(ampdoc);

// A meta[name="amp-action-whitelist"] tag, if present, contains,
// in its content attribute, a whitelist of actions on the special AMP target.
const meta =
this.ampdoc.getRootNode().head.querySelector('meta[name="amp-action-whitelist"]');
Copy link

Choose a reason for hiding this comment

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

80 column limit. gulp presubmit should catch this.

Copy link
Owner Author

Choose a reason for hiding this comment

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

done this for the actual pull request ampproject#13192

// Cache the whitelist of allowed AMP actions (if provided).
if (meta) {
/** @const @private {!Array<string>} */
this.ampActionWhitelist_ = meta.getAttribute('content').split(',')
.map(action => action.trim());
}

this.installActions_(this.actions_);
}

Expand Down Expand Up @@ -101,10 +112,14 @@ export class StandardActions {
* @param {number=} opt_actionIndex
* @param {!Array<!./action-impl.ActionInfoDef>=} opt_actionInfos
* @return {?Promise}
* @throws {Error} If action is not recognized.
* @throws {Error} If action is not recognized or is not whitelisted.
*/
handleAmpTarget(invocation, opt_actionIndex, opt_actionInfos) {
const method = invocation.method;
if (this.ampActionWhitelist_ &&
!this.ampActionWhitelist_.includes(method)) {
throw user().createError('AMP action', method, 'is not whitelisted');
}
switch (method) {
case 'pushState':
case 'setState':
Expand Down
59 changes: 59 additions & 0 deletions test/functional/test-standard-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {StandardActions} from '../../src/service/standard-actions-impl';
import {Services} from '../../src/services';
import {installHistoryServiceForDoc} from '../../src/service/history-impl';
import {setParentWindow} from '../../src/service';
import {createElementWithAttributes} from '../../src/dom';


describes.sandboxed('StandardActions', {}, () => {
Expand Down Expand Up @@ -352,6 +353,64 @@ describes.sandboxed('StandardActions', {}, () => {
standardActions.handleAmpTarget(invocation);
expect(printStub).to.be.calledOnce;
});

it('should not implement print when not whitelisted', () => {
window.document.head.appendChild(
createElementWithAttributes(window.document, 'meta', {
name: 'amp-action-whitelist',
content: 'pushState,setState',
}));

standardActions = new StandardActions(ampdoc);
Copy link

Choose a reason for hiding this comment

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

This is also done in beforeEach, no? It's the same ampdoc being passed around...

Copy link
Owner Author

Choose a reason for hiding this comment

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

It is done in beforeEach as well, but the problem is back in beforeEach there is no meta tag present in the document

Copy link
Owner Author

Choose a reason for hiding this comment

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

To your comment below: The whitelist is created in the constructor of the StandardAction. By the time the constructor is called we have appended the meta tag child. I did what you are suggesting and the test fails for the reason I explained.

Copy link

Choose a reason for hiding this comment

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

Uhh sorry, I forgot the constructor is caching the meta tag content.


const windowApi = {
print: () => {},
};
const printStub = sandbox.stub(windowApi, 'print');
const invocation = {
method: 'print',
satisfiesTrust: () => true,
target: {
ownerDocument: {
defaultView: windowApi,
},
},
};
expect(() => standardActions.handleAmpTarget(invocation)).to.throw();
expect(printStub).to.not.be.called;
});

it('should implement pushState when whitelisted', () => {
window.document.head.appendChild(
createElementWithAttributes(window.document, 'meta', {
name: 'amp-action-whitelist',
content: 'setState, pushState',
}));

standardActions = new StandardActions(ampdoc);
Copy link

Choose a reason for hiding this comment

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

ditto


const pushStateWithExpression = sandbox.stub();
// Bind.pushStateWithExpression() doesn't resolve with a value,
// but add one here to check that the promise is chained.
pushStateWithExpression.returns(Promise.resolve('push-state-complete'));

window.services.bind = {
obj: {pushStateWithExpression},
};

const args = {
[OBJECT_STRING_ARGS_KEY]: '{foo: 123}',
};
const target = ampdoc;
const satisfiesTrust = () => true;
const pushState = {method: 'pushState', args, target, satisfiesTrust};

return standardActions.handleAmpTarget(pushState, 0, []).then(result => {
expect(result).to.equal('push-state-complete');
expect(pushStateWithExpression).to.be.calledOnce;
expect(pushStateWithExpression).to.be.calledWith('{foo: 123}');
});
});
});

describes.fakeWin('adoptEmbedWindow', {}, env => {
Expand Down