Skip to content

Commit 607b226

Browse files
committed
Forcibly escape username/pass for basic auth URLs too
1 parent 3ad0fc0 commit 607b226

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/lib/utils.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ describe('sanitizeUrl', () => {
8484
const result = sanitizeUrl('https://example.com?empty&hasvalue=test')
8585
expect(result).toBe('https://example.com/?empty&hasvalue=test')
8686
})
87+
88+
it('should encode basic auth', () => {
89+
const result = sanitizeUrl('http://user$(calc)r:pass$(calc)[email protected]')
90+
expect(result).toBe('http://user%24(calc)r:pass%24(calc)[email protected]/')
91+
})
8792
})
8893

8994
describe('should handle complex URLs', () => {

src/lib/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ export function sanitizeUrl(raw: string) {
722722
if (url.hostname !== encodeURIComponent(url.hostname)) abort()
723723

724724
// Forcibly sanitise all the pieces of the URL
725+
if (url.username) url.username = encodeURIComponent(url.username)
726+
if (url.password) url.password = encodeURIComponent(url.password)
725727
url.pathname = url.pathname.slice(0, 1) + encodeURIComponent(url.pathname.slice(1)).replace(/%2f/ig,'/')
726728
url.search = url.search.slice(0, 1) + Array.from(url.searchParams.entries()).map(sanitizeParam).join('&')
727729
url.hash = url.hash.slice(0, 1) + encodeURIComponent(url.hash.slice(1))

0 commit comments

Comments
 (0)