Skip to content

Commit

Permalink
fix(coercePath): escape the port number
Browse files Browse the repository at this point in the history
  • Loading branch information
tdeekens authored and kettanaito committed Dec 14, 2021
1 parent d8bd81a commit 7a22520
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/utils/matching/matchRequestUrl.test.ts
Expand Up @@ -87,13 +87,25 @@ describe('coercePath', () => {
)
})

test('escapes the semicolon in protocol', () => {
test('escapes the colon in protocol', () => {
expect(coercePath('https://example.com')).toEqual('https\\://example.com')
expect(coercePath('https://example.com/:userId')).toEqual(
'https\\://example.com/:userId',
)
expect(coercePath('http://localhost:3000')).toEqual(
'http\\://localhost:3000',
'http\\://localhost\\:3000',
)
})

test('escapes the colon 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',
)
expect(coercePath('https://example.com:1234/:5678')).toEqual(
'https\\://example.com\\:1234/:5678',
)
})
})
5 changes: 5 additions & 0 deletions src/utils/matching/matchRequestUrl.ts
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 7a22520

Please sign in to comment.