Preserve percent-encoded trailing whitespace in fragments (#4198)#4301
Open
BigBalli wants to merge 1 commit intojashkenas:masterfrom
Open
Preserve percent-encoded trailing whitespace in fragments (#4198)#4301BigBalli wants to merge 1 commit intojashkenas:masterfrom
BigBalli wants to merge 1 commit intojashkenas:masterfrom
Conversation
…4198) The route stripper used to remove trailing whitespace from any fragment, including the value returned by getPath(), which decodes percent-encoded characters first. As a result, a fragment ending in %20 (e.g. an item id with a literal trailing space) was silently chopped, breaking router lookups for legitimate parameters. Move the trailing whitespace strip onto the raw hash value in getHash(), where the original fix for jashkenas#1794 belongs (location.hash returning trailing whitespace in old browsers). The path branch through getPath() and any explicit fragment passed to getFragment() now preserve trailing whitespace, fixing jashkenas#4198 while keeping jashkenas#1794 covered. Updated existing jashkenas#1794/jashkenas#1820 tests to assert at the right level and added a regression test for jashkenas#4198.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #4198.
Problem
routeStripper(/^[#\/]|\s+$/g) was applied at the very end ofgetFragment(), aftergetPath()has already decoded the URL viadecodeFragment(). As a result, an encoded trailing space (%20) wasdecoded to a literal space and then stripped, silently truncating
legitimate parameter values:
Why the strip exists
The trailing-space strip was introduced in #1794 to work around old
browsers where
location.hashitself returned trailing whitespace. Thatfix only ever needed to apply to the raw hash value — not to the
already-decoded path.
Fix
routeStripperinto two regexes:routeStripper = /^[#\/]/— only the leading hash/slash, appliedin
getFragment()as before.trailingSpaceStripper = /\s+$/— applied insidegetHash()onthe raw hash value, preserving the original Trailing spaces in search query cause route to be fired twice #1794 fix at the level
where it actually matters.
getFragment()no longer touches trailing whitespace, so explicitfragments and decoded paths preserve their trailing characters.
Tests
#1794test to assert againstgetHash()(thecorrect level), and added an assertion that
getFragment('fragment ')preserves whitespace.
#1820test (leading slash + trailing space) toreflect that only the leading slash is stripped now.
#4198covering a percent-encodedtrailing space in the path.
npm run lintpasses.