Skip to content

Implement replaceUrl protocol for safe URL replacements#9175

Merged
dvoytenko merged 3 commits intoampproject:masterfrom
dvoytenko:fixes170
May 9, 2017
Merged

Implement replaceUrl protocol for safe URL replacements#9175
dvoytenko merged 3 commits intoampproject:masterfrom
dvoytenko:fixes170

Conversation

@dvoytenko
Copy link
Copy Markdown
Contributor

Closes #9044.

/cc @ericfs @alf7

@dvoytenko dvoytenko requested a review from lannka May 5, 2017 22:14
const replaceUrlParam = this.params_['replaceUrl'];
if (ampdoc.isSingleDoc() &&
replaceUrlParam &&
this.win.history.replaceState) {
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.

#replaceState check should be unnecessary, supported in everything but IE 9.

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.

I believe this is still an issue. Last time we tried to remove this check, we saw lots of errors. Feel free to file a bug for a deeper cleanup. But it will have to first start in the history-impl.js with error sampling for when it's not available.

@dvoytenko dvoytenko requested a review from aghassemi May 8, 2017 18:26
const url = parseUrl(this.win.location.href);
const replaceUrl = parseUrl(
removeFragment(replaceUrlParam) + this.win.location.hash);
if (url.origin == replaceUrl.origin &&
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.

compare case-insensitive ( I believe origin when coming from navigator will be lowercase but not when coming from this.params_['replaceUrl'] (don't see parseUrl or getSourceOrigin lowercase anything)

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.

I believe by spec, HTMLAnchorElement must lowercase origins.

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.

huh didn't know our parseUrl uses <a>. very cool!

expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'http://www.example.com/two' + fragment);
expect(ampdoc.getUrl())
.to.equal('http://www.example.com/two' + fragment);
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.

I expected this to be 'http://www.example.com/two&b=1' + fragment. Looking at the code, I don't see how it is not, sort of confused. We remove fragment but not query string params and url.href should include query string params.

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.

For that to happen, & should be encoded. Basically, the whole fragment is parsed first using query parameter parse, which will end up with two params: #repalceUrl=http://www.example.com/two AND b=1. Only replaceUrl is used for this, so this test demonstrates that b=1 is ignored.

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.

got it. Might be worth adding a test for fragment = '#replaceUrl=http://www.example.com/two%25b=2&b=1'; to expect http://www.example.com/two&b=2 then.

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.

Good idea. Done.

expect(windowApi.history.replaceState).to.be.calledWith({}, '',
'http://www.example.com/two' + fragment);
expect(ampdoc.getUrl())
.to.equal('http://www.example.com/two' + fragment);
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.

got it. Might be worth adding a test for fragment = '#replaceUrl=http://www.example.com/two%25b=2&b=1'; to expect http://www.example.com/two&b=2 then.

@dvoytenko dvoytenko merged commit fa020cb into ampproject:master May 9, 2017
@dvoytenko dvoytenko deleted the fixes170 branch May 9, 2017 01:10
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.

5 participants