fix(pattern-router/linear-router): prevent prefix overmatch on wildcard routes (#5252)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(pattern-router): prevent prefix overmatch on wildcard routes * move the test into common tests and fix the linear router * use endsWith * support `/a/*/b/*`
authored by Yusuke Wada · committed by GitHub · parent 63bbcf5 · Browse files · 3 files·+87 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+87 added−11 removed
- tests:+75 added−0 removed
- other:+12 added−11 removed
Provenance
- Commit
a194628- Tree
01d1135- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
63bbcf5
3 files changed+87 added−11 removed
modified:src/router/linear-router/router.ts+9 added-2 removed
38 unmodified lines394041424343444546474849502 unmodified lines535455565457585960616263646586 unmodified lines38 unmodified lines handlers.push([handler, emptyParams]) } } else if (hasStar && !hasLabel) { const endsWithStar = routePath.charCodeAt(routePath.length - 1) === 42 const parts = (endsWithStar ? routePath.slice(0, -2) : routePath).split(splitByStarRe) const endsWithSlashStar = routePath.endsWith('/*') const parts = ( endsWithStar ? routePath.slice(0, endsWithSlashStar ? -2 : -1) : routePath ).split(splitByStarRe)
const lastIndex = parts.length - 1 for (let j = 0, pos = 0, len = parts.length; j < len; j++) { const part = parts[j]2 unmodified lines continue ROUTES_LOOP } pos += part.length if (j === lastIndex) { if ( if (endsWithSlashStar) { if (pos !== path.length && path.charCodeAt(pos) !== 47) { continue ROUTES_LOOP } } else if ( !endsWithStar && pos !== path.length && !(pos === path.length - 1 && path.charCodeAt(pos) === 47) ) {86 unmodified linesmodified:src/router/pattern-router/router.ts+3 added-9 removed
8 unmodified lines9 name: string = 'PatternRouter'10 #routes: Route<T>[] = []11 12 add(method: string, path: string, handler: T) {13- const endsWithWildcard = path.at(-1) === '*'14- if (endsWithWildcard) {15- path = path.slice(0, -2)16- }13+ const suffix = path.endsWith('/*') ? '(?:$|/)' : path.endsWith('*') ? '' : '/?$'14+ path = path.replace(/\*$/, '')15 if (path.at(-1) === '?') {16 path = path.slice(0, -1)17 this.add(method, path.replace(/\/[^/]+$/, ''), handler)18 }9 unmodified lines28 }29 )30 31 try {34- this.#routes.push([35- new RegExp(`^${parts.join('')}${endsWithWildcard ? '' : '/?$'}`),36- method,37- handler,38- ])32+ this.#routes.push([new RegExp(`^${parts.join('')}${suffix}`), method, handler])33 } catch {34 throw new UnsupportedPathError()35 }36 }18 unmodified linesmodified:src/router/common.case.test.ts+75 added
368 unmodified lines369 expect(res[3].handler).toEqual('/x/*')370 })371 })372 373+ describe('Suffix wildcard', () => {374+ beforeEach(() => {375+ router.add('GET', '/assets*', 'assets')376+ })377+378+ it('GET /assets', async () => {379+ const res = match('GET', '/assets')380+ expect(res.length).toBe(1)381+ expect(res[0].handler).toEqual('assets')382+ })383+384+ it('GET /assets-v2', async () => {385+ const res = match('GET', '/assets-v2')386+ expect(res.length).toBe(1)387+ expect(res[0].handler).toEqual('assets')388+ })389+390+ it('GET /assets/app.js', async () => {391+ const res = match('GET', '/assets/app.js')392+ expect(res.length).toBe(1)393+ expect(res[0].handler).toEqual('assets')394+ })395+396+ it('GET /asset', async () => {397+ const res = match('GET', '/asset')398+ expect(res.length).toBe(0)399+ })400+ })401+402+ describe('Trailing wildcard', () => {403+ beforeEach(() => {404+ router.add('GET', '/path/*', 'path')405+ })406+407+ it('GET /path', async () => {408+ const res = match('GET', '/path')409+ expect(res.length).toBe(1)410+ expect(res[0].handler).toEqual('path')411+ })412+413+ it('GET /path/to/file', async () => {414+ const res = match('GET', '/path/to/file')415+ expect(res.length).toBe(1)416+ expect(res[0].handler).toEqual('path')417+ })418+419+ it('GET /pathfoo', async () => {420+ const res = match('GET', '/pathfoo')421+ expect(res.length).toBe(0)422+ })423+ })424+425+ describe('Trailing wildcard after a middle wildcard', () => {426+ beforeEach(() => {427+ router.add('GET', '/a/*/b/*', 'b')428+ })429+430+ it('GET /a/x/b', async () => {431+ const res = match('GET', '/a/x/b')432+ expect(res.length).toBe(1)433+ expect(res[0].handler).toEqual('b')434+ })435+436+ it('GET /a/x/b/c', async () => {437+ const res = match('GET', '/a/x/b/c')438+ expect(res.length).toBe(1)439+ expect(res[0].handler).toEqual('b')440+ })441+442+ it('GET /a/x/bz', async () => {443+ const res = match('GET', '/a/x/bz')444+ expect(res.length).toBe(0)445+ })446+ })447+448 describe('Optional route', () => {449 beforeEach(() => {450 router.add('GET', '/api/animals/:type?', 'animals')451 router.add('GET', '/v1/:version?/:platform?', 'result')547 unmodified lines