Skip to content

Commit 922cf5c

Browse files
authored
fix: avoid decoding URL authority part (#614)
1 parent 24bd7c4 commit 922cf5c

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

lib/__tests__/regression.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ describe('Regression Tests', () => {
3333
)
3434
})
3535

36+
it('should not decode authority when getting cookies', async () => {
37+
const cookieJar = new CookieJar()
38+
await cookieJar.setCookie('a=b; Path=/', 'https://example.com/')
39+
const cookieStr = await cookieJar.getCookieString(
40+
'https://example.com%5C@example.net/',
41+
)
42+
expect(cookieStr).toBe('')
43+
})
44+
45+
it('should not decode authority when setting cookies', async () => {
46+
const cookieJar = new CookieJar()
47+
await cookieJar.setCookie(
48+
'a=b; Path=/',
49+
'https://example.com%5C@example.net/',
50+
)
51+
const cookieStr = await cookieJar.getCookieString('https://example.com/')
52+
expect(cookieStr).toBe('')
53+
})
54+
3655
it('should allow setCookie (without options) callback works even if it is not instanceof Function (GH-158/GH-175)', () => {
3756
expect.assertions(2)
3857
const cookieJar = new CookieJar()

lib/cookie/cookieJar.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,19 @@ function getCookieContext(url: unknown): UrlContext {
225225
protocol: url.protocol,
226226
}
227227
} else if (typeof url === 'string') {
228+
const parsed = new URL(url)
229+
// Decode the path so percent-encoded segments match stored cookie paths
230+
// (RFC 6265 §5.4). Only the pathname gets decoded, not the full URI.
231+
let pathname = parsed.pathname
228232
try {
229-
return new URL(decodeURI(url))
233+
pathname = decodeURI(pathname)
230234
} catch {
231-
return new URL(url)
235+
// Malformed percent-encoding in the path; match it verbatim.
236+
}
237+
return {
238+
hostname: parsed.hostname,
239+
pathname,
240+
protocol: parsed.protocol,
232241
}
233242
} else {
234243
throw new ParameterError('`url` argument is not a string or URL.')

0 commit comments

Comments
 (0)