Skip to content

Commit 41d078d

Browse files
committed
fix double decoding of cookie value
1 parent 8312ccd commit 41d078d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

packages/cookies/src/serialize.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import type { RequestCookie, ResponseCookie } from './types'
22

3+
function maybeDecodeURIComponent(s: string) {
4+
try {
5+
return decodeURIComponent(s)
6+
} catch {
7+
return s
8+
}
9+
}
10+
311
export function stringifyCookie(c: ResponseCookie | RequestCookie): string {
412
const attrs = [
513
'path' in c && c.path && `Path=${c.path}`,
@@ -19,7 +27,9 @@ export function stringifyCookie(c: ResponseCookie | RequestCookie): string {
1927
].filter(Boolean)
2028

2129
const stringified = `${c.name}=${encodeURIComponent(c.value ?? '')}`
22-
return attrs.length === 0 ? stringified : `${stringified}; ${attrs.join('; ')}`
30+
return attrs.length === 0
31+
? stringified
32+
: `${stringified}; ${attrs.join('; ')}`
2333
}
2434

2535
/** Parse a `Cookie` header value */
@@ -72,7 +82,9 @@ export function parseSetCookie(setCookie: string): undefined | ResponseCookie {
7282
)
7383
const cookie: ResponseCookie = {
7484
name,
75-
value: decodeURIComponent(value),
85+
// parseCookie already decoded the value, so if the value contains special chars
86+
// decoding it again will cause problems
87+
value: maybeDecodeURIComponent(value),
7688
domain,
7789
...(expires && { expires: new Date(expires) }),
7890
...(httponly && { httpOnly: true }),

0 commit comments

Comments
 (0)