Skip to content
Myr

fix(url): ignore fragment identifiers in getPath() (#4627)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(url): ignore fragment identifiers in getPath() In Service Worker contexts, URLs may contain fragment identifiers (#) which should not be included in route matching. This change modifies getPath() to strip fragment identifiers from the path, consistent with how query strings are already handled. Fixes #4440 * fix: handle fragment before query in percent-encoded paths When a URL contains a fragment with query-like characters (e.g., #section?foo=bar), the previous logic would incorrectly include the fragment in the path because it checked for '?' first and only looked for '#' if '?' was not found. This fix uses Math.min() to find whichever delimiter ('?' or '#') comes first, correctly handling edge cases per RFC 3986 where '#' terminates the path regardless of subsequent '?' characters in the fragment. Added test case for this edge case. * test(url): add test for encoded hash (%23) in path with fragment Verify that percent-encoded hash (%23) is preserved while real fragment identifiers are correctly stripped. decodeURI preserves reserved characters per Web Standards. * perf(url): reduce === -1 evaluations in getPath Restructure ternary to avoid redundant separator === -1 check. For /path?x=1, reduces evaluations from 3 to 2. Co-authored-by: usualoma <usualoma@users.noreply.github.com> * test(url): add coverage for percent encoding without query or fragment Cover the undefined branch when both queryIndex and hashIndex are -1. ---------

authored by Sano Suguru · committed by GitHub · parent 5ca5c3e · Browse files · 2 files+59 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: sanosuguru <sa.50.00.no.riku@gmail.com>
  • Co-authored-by: usualoma <usualoma@users.noreply.github.com>

Diff stats

2 files+59 added−4 removed

  • tests:+46 added−0 removed
  • other:+13 added−4 removed

2 files+59 added−4 removed

modified:src/utils/url.test.ts+46 added
123 unmodified lines
124 'x://a/:/hello', // unknown schema125 ])('getPath - %s', (url) => {126 expect(getPath(new Request(url))).toBe(new URL(url).pathname)127 })128129 it('getPath - with fragment', () => {130 let path = getPath(new Request('https://example.com/users/#user-list'))131 expect(path).toBe('/users/')132 path = getPath(new Request('https://example.com/users/1#profile-section'))133 expect(path).toBe('/users/1')134 path = getPath(new Request('https://example.com/hello#section'))135 expect(path).toBe('/hello')136 path = getPath(new Request('https://example.com/#top'))137 expect(path).toBe('/')138 })139140 it('getPath - with query and fragment', () => {141 let path = getPath(new Request('https://example.com/hello?name=foo#section'))142 expect(path).toBe('/hello')143 path = getPath(new Request('https://example.com/search?q=test#results'))144 expect(path).toBe('/search')145 })146147 it('getPath - with percent encoding only (no query or fragment)', () => {148 const path = getPath(new Request('https://example.com/hello%20world'))149 expect(path).toBe('/hello world')150 })151152 it('getPath - with percent encoding and fragment', () => {153 let path = getPath(new Request('https://example.com/hello%20world#section'))154 expect(path).toBe('/hello world')155 path = getPath(new Request('https://example.com/%E7%82%8E#top'))156 expect(path).toBe('/炎')157 })158159 it('getPath - with percent encoding and fragment containing query-like chars', () => {160 const path = getPath(new Request('https://example.com/hello%20world#section?foo=bar'))161 expect(path).toBe('/hello world')162 })163164 it('getPath - with encoded hash (%23) in path and real fragment', () => {165 // %23 is encoded '#' - decodeURI preserves reserved characters, so %23 stays as %23166 let path = getPath(new Request('https://example.com/path%23test#real-fragment'))167 expect(path).toBe('/path%23test')168 path = getPath(new Request('https://example.com/foo%23bar%23baz#section'))169 expect(path).toBe('/foo%23bar%23baz')170 // Only encoded hash, no real fragment171 path = getPath(new Request('https://example.com/issue%23123'))172 expect(path).toBe('/issue%23123')173 })174 })175176 describe('getQueryStrings', () => {177 it('getQueryStrings', () => {
186 unmodified lines
modified:src/utils/url.ts+13 added-4 removed
109 unmodified lines
110 for (; i < url.length; i++) {111 const charCode = url.charCodeAt(i)112 if (charCode === 37) {113 // '%'114 // If the path contains percent encoding, use `indexOf()` to find '?' and return the result immediately.114 // If the path contains percent encoding, use `indexOf()` to find '?' or '#' and return the result immediately.115 // Although this is a performance disadvantage, it is acceptable since we prefer cases that do not include percent encoding.116 const queryIndex = url.indexOf('?', i)117 const path = url.slice(start, queryIndex === -1 ? undefined : queryIndex)117 const hashIndex = url.indexOf('#', i)118 const end =119 queryIndex === -1120 ? hashIndex === -1121 ? undefined122 : hashIndex123 : hashIndex === -1124 ? queryIndex125 : Math.min(queryIndex, hashIndex)126 const path = url.slice(start, end)127 return tryDecodeURI(path.includes('%25') ? path.replace(/%25/g, '%2525') : path)119 } else if (charCode === 63) {120 // '?'128 } else if (charCode === 63 || charCode === 35) {129 // '?' or '#'130 break131 }132 }133 return url.slice(start, i)
186 unmodified lines