Skip to content

Commit

Permalink
Remove "amp-allowed-url-macros" meta
Browse files Browse the repository at this point in the history
  • Loading branch information
caroqliu committed Apr 13, 2020
1 parent 0e243a6 commit 56cfae3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 34 deletions.
29 changes: 8 additions & 21 deletions src/service/variable-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ export class VariableSource {
}

/**
* Searches for the "amp-allowed-url-macros" meta tag, parses and returns its contents.
* For email documents, the allowlist defaults to empty and supercedes the meta tag.
* For email documents, all URL macros are disallowed by default.
* @return {?Array<string>} The allowlist of substitutable AMP variables
* @private
*/
Expand All @@ -341,8 +340,7 @@ export class VariableSource {
return this.variableWhitelist_;
}

// Allow no URL macros for AMP4Email format documents.
// This cannot be overwritten even if the meta tag is provided.
// Disallow all URL macros for AMP4Email format documents.
if (this.ampdoc.isSingleDoc()) {
const doc = /** @type {Document} */ (this.ampdoc.getRootNode());
if (
Expand All @@ -351,24 +349,13 @@ export class VariableSource {
doc.documentElement.hasAttribute &&
isAmp4Email(doc)
) {
return [];
/**
* The whitelist of variables allowed for variable substitution.
* @private {?Array<string>}
*/
this.variableWhitelist_ = [''];
return this.variableWhitelist_;
}
}

// A meta[name="amp-allowed-url-macros"] tag, if present,
// contains, in its content attribute, a whitelist of variable substitution.
const meta = this.ampdoc.getMetaByName('amp-allowed-url-macros');
if (meta === null) {
return null;
}

/**
* The whitelist of variables allowed for variable substitution.
* @private {?Array<string>}
*/
this.variableWhitelist_ = meta
.split(',')
.map((variable) => variable.trim());
return this.variableWhitelist_;
}
}
8 changes: 2 additions & 6 deletions test/unit/test-variable-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,8 @@ describes.fakeWin(
(env) => {
let variableSource;
beforeEach(() => {
env.sandbox.stub(env.ampdoc, 'getMeta').returns({
'amp-allowed-url-macros': 'ABC,ABCD,CANONICAL',
});
variableSource = new VariableSource(env.ampdoc);
variableSource.variableWhitelist_ = ['ABC', 'ABCD', 'CANONICAL'];
});

it('Works with whitelisted variables', () => {
Expand Down Expand Up @@ -173,10 +171,8 @@ describes.fakeWin(
);

it('Should not work with empty variable whitelist', () => {
env.sandbox.stub(env.ampdoc, 'getMeta').returns({
'amp-allowed-url-macros': '',
});
const variableSource = new VariableSource(env.ampdoc);
variableSource.variableWhitelist_ = [''];

variableSource.setAsync('RANDOM', () => Promise.resolve('0.1234'));
expect(variableSource.getExpr()).to.be.ok;
Expand Down
11 changes: 4 additions & 7 deletions test/unit/url-expander/test-expander.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,14 @@ describes.realWin(
};

function createExpanderWithWhitelist(whitelist, mockBindings) {
env.sandbox.stub(env.ampdoc, 'getMeta').returns({
'amp-allowed-url-macros': whitelist,
});

variableSource = new GlobalVariableSource(env.ampdoc);
variableSource.variableWhitelist_ = whitelist;
return new Expander(variableSource, mockBindings);
}

it('should not replace unwhitelisted RANDOM', () => {
const expander = createExpanderWithWhitelist(
'ABC,ABCD,CANONICAL',
['ABC', 'ABCD', 'CANONICAL'],
mockBindings
);
const url = 'http://www.google.com/?test=RANDOM';
Expand All @@ -119,7 +116,7 @@ describes.realWin(

it('should replace whitelisted ABCD', () => {
const expander = createExpanderWithWhitelist(
'ABC,ABCD,CANONICAL',
['ABC', 'ABCD', 'CANONICAL'],
mockBindings
);
const url = 'http://www.google.com/?test=ABCD';
Expand All @@ -128,7 +125,7 @@ describes.realWin(
});

it('should not replace anything with empty whitelist', () => {
const expander = createExpanderWithWhitelist('', mockBindings);
const expander = createExpanderWithWhitelist([''], mockBindings);
const url = 'http://www.google.com/?test=ABCD';
const expected = 'http://www.google.com/?test=ABCD';
return expect(expander.expand(url)).to.eventually.equal(expected);
Expand Down

0 comments on commit 56cfae3

Please sign in to comment.