Skip to content

Commit

Permalink
Fixes escaping port number in absolute urls
Browse files Browse the repository at this point in the history
  • Loading branch information
tdeekens committed Dec 13, 2021
1 parent d8bd81a commit 1c8d1c4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/utils/matching/matchRequestUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,16 @@ describe('coercePath', () => {
'https\\://example.com/:userId',
)
expect(coercePath('http://localhost:3000')).toEqual(
'http\\://localhost:3000',
'http\\://localhost\\:3000',
)
})

test('escapes the semicolon in port', () => {
expect(coercePath('https://example.com:1234')).toEqual(
'https\\://example.com\\:1234',
)
expect(coercePath('https://example.com:1234/:userId')).toEqual(
'https\\://example.com\\:1234/:userId',
)
})
})
5 changes: 5 additions & 0 deletions src/utils/matching/matchRequestUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export function coercePath(path: string): string {
* @see https://github.com/pillarjs/path-to-regexp/issues/259
*/
.replace(/^([^\/]+)(:)(?=\/\/)/g, '$1\\$2')
/**
* Escape the port so that "path-to-regexp" can match
* absolute URLs including port numbers.
*/
.replace(/((?::)(?:[0-9]+))/g, '\\$1')
)
}

Expand Down

0 comments on commit 1c8d1c4

Please sign in to comment.