File tree Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Expand file tree Collapse file tree 1 file changed +14
-2
lines changed Original file line number Diff line number Diff line change 1
1
import type { RequestCookie , ResponseCookie } from './types'
2
2
3
+ function maybeDecodeURIComponent ( s : string ) {
4
+ try {
5
+ return decodeURIComponent ( s )
6
+ } catch {
7
+ return s
8
+ }
9
+ }
10
+
3
11
export function stringifyCookie ( c : ResponseCookie | RequestCookie ) : string {
4
12
const attrs = [
5
13
'path' in c && c . path && `Path=${ c . path } ` ,
@@ -19,7 +27,9 @@ export function stringifyCookie(c: ResponseCookie | RequestCookie): string {
19
27
] . filter ( Boolean )
20
28
21
29
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 ( '; ' ) } `
23
33
}
24
34
25
35
/** Parse a `Cookie` header value */
@@ -72,7 +82,9 @@ export function parseSetCookie(setCookie: string): undefined | ResponseCookie {
72
82
)
73
83
const cookie : ResponseCookie = {
74
84
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 ) ,
76
88
domain,
77
89
...( expires && { expires : new Date ( expires ) } ) ,
78
90
...( httponly && { httpOnly : true } ) ,
You can’t perform that action at this time.
0 commit comments