Skip to content

Preserve percent-encoded trailing whitespace in fragments (#4198)#4301

Open
BigBalli wants to merge 1 commit intojashkenas:masterfrom
BigBalli:fix-4198-trailing-space
Open

Preserve percent-encoded trailing whitespace in fragments (#4198)#4301
BigBalli wants to merge 1 commit intojashkenas:masterfrom
BigBalli:fix-4198-trailing-space

Conversation

@BigBalli
Copy link
Copy Markdown

@BigBalli BigBalli commented Apr 6, 2026

Fixes #4198.

Problem

routeStripper (/^[#\/]|\s+$/g) was applied at the very end of
getFragment(), after getPath() has already decoded the URL via
decodeFragment(). As a result, an encoded trailing space (%20) was
decoded to a literal space and then stripped, silently truncating
legitimate parameter values:

/outbound/22130600/po/powithspacetest%20
                                       ^^ stripped, lookup fails

Why the strip exists

The trailing-space strip was introduced in #1794 to work around old
browsers where location.hash itself returned trailing whitespace. That
fix only ever needed to apply to the raw hash value — not to the
already-decoded path.

Fix

  • Split routeStripper into two regexes:
  • getFragment() no longer touches trailing whitespace, so explicit
    fragments and decoded paths preserve their trailing characters.

Tests

  • Updated the existing #1794 test to assert against getHash() (the
    correct level), and added an assertion that getFragment('fragment ')
    preserves whitespace.
  • Updated the existing #1820 test (leading slash + trailing space) to
    reflect that only the leading slash is stripped now.
  • Added a new regression test for #4198 covering a percent-encoded
    trailing space in the path.

npm run lint passes.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trailing Fragment Space(s)

1 participant