-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Normalize fragment encoding to avoid duplicate route fires (#4132) #4303
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
Open
BigBalli
wants to merge
1
commit into
jashkenas:master
Choose a base branch
from
BigBalli:fix-4132-encoded-double-fire
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+38
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1885,7 +1885,9 @@ | |
| this._wantsPushState = !!this.options.pushState; | ||
| this._hasPushState = !!(this.history && this.history.pushState); | ||
| this._usePushState = this._wantsPushState && this._hasPushState; | ||
| this.fragment = this.getFragment(); | ||
| // Store the initial fragment in decoded form for consistency | ||
| // with what navigate()/loadUrl() write (#4132). | ||
| this.fragment = this.decodeFragment(this.getFragment()); | ||
|
|
||
| // Normalize root to always include a leading and trailing slash. | ||
| this.root = ('/' + this.root + '/').replace(rootStripper, '/'); | ||
|
|
@@ -1979,12 +1981,19 @@ | |
| // Checks the current URL to see if it has changed, and if it has, | ||
| // calls `loadUrl`, normalizing across the hidden iframe. | ||
| checkUrl: function(e) { | ||
| var current = this.getFragment(); | ||
| // Compare in decoded form. Firefox returns `location.href` with | ||
| // percent-encoded non-ASCII characters in the hash, while Chrome | ||
| // returns them decoded. Without normalising here, navigating to a | ||
| // URL like `#search/大阪` from inside the app stores the decoded | ||
| // form in `this.fragment` while the next `hashchange` event reads | ||
| // back the encoded form, so checkUrl thinks the URL changed and | ||
| // fires the route a second time. See #4132, #4085, #3941. | ||
| var current = this.decodeFragment(this.getFragment()); | ||
|
|
||
| // If the user pressed the back button, the iframe's hash will have | ||
| // changed and we should use that for comparison. | ||
| if (current === this.fragment && this.iframe) { | ||
| current = this.getHash(this.iframe.contentWindow); | ||
| current = this.decodeFragment(this.getHash(this.iframe.contentWindow)); | ||
| } | ||
|
|
||
| if (current === this.fragment) { | ||
|
|
@@ -2001,7 +2010,11 @@ | |
| loadUrl: function(fragment) { | ||
| // If the root doesn't match, no routes can match either. | ||
| if (!this.matchRoot()) return this.notfound(); | ||
| fragment = this.fragment = this.getFragment(fragment); | ||
| // Always store the fragment in its decoded form so that the | ||
| // value cached in `this.fragment` is consistent with what | ||
| // `navigate()` writes — see checkUrl for the cross-browser | ||
| // rationale (#4132). | ||
| fragment = this.fragment = this.decodeFragment(this.getFragment(fragment)); | ||
|
Collaborator
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. fragment = this.getFragment(fragment);
this.fragment = this.decodeFragment(fragment);Decode only the cached fragment for comparison; don’t decode the fragment you match routes against, or you change routing semantics. |
||
| return _.some(this.handlers, function(handler) { | ||
| if (handler.route.test(fragment)) { | ||
| handler.callback(fragment); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
decodeFragmentusesdecodeURI, which throwsURIErroron malformed percent-escapes (e.g. a user manually enters a hash containing a stray%or incomplete UTF-8 sequence). By decoding unconditionally instart/checkUrl/loadUrlnow, hashChange navigation that previously tolerated such URLs can start crashing the router. Consider wrapping the decode in a try/catch (returning the original fragment on failure) or adding a safe decode helper used by these call sites so invalid fragments don’t break history handling.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.
so it's suggesting:
I suppose it could be breaking.. although seems unlikely