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

Implement replaceUrl protocol for safe URL replacements #9175

Merged
merged 3 commits into from
May 9, 2017

Conversation

dvoytenko
Copy link
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
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
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
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
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
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
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
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
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
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
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.

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.

None yet

5 participants