-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import {registerServiceBuilderForDoc} from '../service'; | |
import {dev, duplicateErrorIfNecessary} from '../log'; | ||
import {isIframed} from '../dom'; | ||
import { | ||
getSourceOrigin, | ||
parseQueryString, | ||
parseUrl, | ||
removeFragment, | ||
|
@@ -362,6 +363,27 @@ export class Viewer { | |
} | ||
}); | ||
|
||
// Replace URL if requested. | ||
const replaceUrlParam = this.params_['replaceUrl']; | ||
if (ampdoc.isSingleDoc() && | ||
replaceUrlParam && | ||
this.win.history.replaceState) { | ||
try { | ||
// The origin and source origin must match. | ||
const url = parseUrl(this.win.location.href); | ||
const replaceUrl = parseUrl( | ||
removeFragment(replaceUrlParam) + this.win.location.hash); | ||
if (url.origin == replaceUrl.origin && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. compare case-insensitive ( I believe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe by spec, HTMLAnchorElement must lowercase origins. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. huh didn't know our |
||
getSourceOrigin(url) == getSourceOrigin(replaceUrl)) { | ||
this.win.history.replaceState({}, '', replaceUrl.href); | ||
this.win.location.originalHref = url.href; | ||
dev().fine(TAG_, 'replace url:' + replaceUrl.href); | ||
} | ||
} catch (e) { | ||
dev().error(TAG_, 'replaceUrl failed', e); | ||
} | ||
} | ||
|
||
// Remove hash when we have an incoming click tracking string | ||
// (see impression.js). | ||
if (this.params_['click']) { | ||
|
@@ -373,7 +395,7 @@ export class Viewer { | |
this.win.location.originalHash = this.win.location.hash; | ||
} | ||
this.win.history.replaceState({}, '', newUrl); | ||
dev().fine(TAG_, 'replace url:' + this.win.location.href); | ||
dev().fine(TAG_, 'replace fragment:' + this.win.location.href); | ||
} | ||
} | ||
|
||
|
@@ -941,6 +963,7 @@ function getChannelError(opt_reason) { | |
return new Error('No messaging channel: ' + opt_reason); | ||
} | ||
|
||
|
||
/** | ||
* Sets the viewer visibility state. This calls is restricted to runtime only. | ||
* @param {!VisibilityState} state | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.