Skip to content

Force cloudflare amp-ad to use absolute src. #7556

Merged
lannka merged 1 commit intoampproject:masterfrom
cloudflare:oli/absolute-src
Feb 16, 2017
Merged

Force cloudflare amp-ad to use absolute src. #7556
lannka merged 1 commit intoampproject:masterfrom
cloudflare:oli/absolute-src

Conversation

@oliy
Copy link
Copy Markdown
Contributor

@oliy oliy commented Feb 14, 2017

The cloudflare extension wasn't properly ensuring that the src attribute was an absolute URL. This changes the implementation to still check for vendor specific URLs, but ensures full URLs.

Fixes #7543

@oliy oliy changed the title #7543 Force cloudflare amp-ad to use absolute src. Force cloudflare amp-ad to use absolute src. Implements #7543 Feb 14, 2017
@oliy oliy changed the title Force cloudflare amp-ad to use absolute src. Implements #7543 Force cloudflare amp-ad to use absolute src. Fixes #7543 Feb 14, 2017
@jridgewell jridgewell changed the title Force cloudflare amp-ad to use absolute src. Fixes #7543 Force cloudflare amp-ad to use absolute src. Feb 14, 2017
@jridgewell jridgewell requested review from lannka and tdrl February 14, 2017 20:43
@jridgewell
Copy link
Copy Markdown
Contributor

/to @lannka, @tdrl

}

const src = el.getAttribute('src');
if (src.indexOf(network.base) != 0) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

startsWith, please.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replaced

}

const src = el.getAttribute('src');
if (src.indexOf(network.base) != 0) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Keeping with the title, we should also assertAbsoluteHttpOrHttpsUrl .

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

let url = el.getAttribute('src');

// optionally convert to a4a endpoint
if (a4a && url.indexOf('/_a4a/') != base.length) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

url.substr(base.length, 6) != '/_a4a'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks.

}

const src = el.getAttribute('src');
if (!src.startsWith(network.base)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

String#startsWith is not available in all environments. You'll need to import the helper.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Switched to the string.js version. Incidentally, the original implementation was pretty much the same, but I guess the string.js version is a bit clearer in intent.

}

const src = el.getAttribute('src');
if (!src.startsWith(network.base)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please use startsWith from string.js. i guess the native one is not widely supported

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Switched.

@jridgewell jridgewell dismissed their stale review February 15, 2017 00:31

Issues addressed

Copy link
Copy Markdown
Contributor

@lannka lannka left a comment

Choose a reason for hiding this comment

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

LGTM with one request

&& el.hasAttribute('src')
&& el.hasAttribute('data-cf-network')
&& NETWORKS[el.getAttribute('data-cf-network')] != null;
if (!(this.isAmpAdElement() && el.hasAttribute('src')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you add brief js doc what are we checking ?

@dknecht
Copy link
Copy Markdown
Contributor

dknecht commented Feb 15, 2017

I am not sure longterm if this is best idea because then we are having publisher put specific url on page vs just attributes. I guess can always add other option in the future.


it('should handle both data parameters and templated parameters', () => {
el.setAttribute('src', '/ad?width=SLOT_WIDTH&height=SLOT_HEIGHT');
el.setAttribute('src', 'https://firebolt.cloudflaredemo.com' +
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Are there some tests that it correctly fails if the src does not start with the Cloudflare base URL? (Or, at least, is not a fully qualified URL?)

Also, there should be tests for the 'optionally convert to a4a endpoint' logic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added additional test.

@dknecht
Copy link
Copy Markdown
Contributor

dknecht commented Feb 15, 2017

@oliy can we switch adzerk to be engine.betazerk.com ?

@oliy
Copy link
Copy Markdown
Contributor Author

oliy commented Feb 15, 2017

I also addressed @dknecht concerns with supporting default src in vendors and changing adzerk to betazerk.

@oliy
Copy link
Copy Markdown
Contributor Author

oliy commented Feb 15, 2017

@lannka Can we merge this? Would like to get them in before Thursday canary.

@irtefa
Copy link
Copy Markdown
Contributor

irtefa commented Feb 16, 2017

@jridgewell just a heads up. Can we please merge this for the canary release tomorrow? Thanks!

@dknecht
Copy link
Copy Markdown
Contributor

dknecht commented Feb 16, 2017

@jridgewell Would it be possible for this to get patched into current canary and then pushed with next production release. We have publishers ready to use and collect a4a metrics that we were hoping to share for ampconf.

@lannka lannka merged commit bd30509 into ampproject:master Feb 16, 2017
petekip pushed a commit to petekip/amphtml that referenced this pull request Jul 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants