@@ -63,6 +63,11 @@ const domainValueRegExp =
63
63
*/
64
64
const pathValueRegExp = / ^ [ \u0020 - \u003A \u003D - \u007E ] * $ / ;
65
65
66
+ /**
67
+ * RegExp to match max-age-value in RFC 6265 sec 5.6.2
68
+ */
69
+ const maxAgeRegExp = / ^ - ? \d + $ / ;
70
+
66
71
const __toString = Object . prototype . toString ;
67
72
68
73
const NullObject = /* @__PURE__ */ ( ( ) => {
@@ -405,7 +410,10 @@ export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
405
410
const len = str . length ;
406
411
const endIdx = colonIdx === - 1 ? len : colonIdx ;
407
412
const [ name , value ] = keyPairSlice ( str , 0 , endIdx ) ;
408
- const setCookie : SetCookie = { name, value : dec ( value ) } ;
413
+ const setCookie : SetCookie =
414
+ value === undefined
415
+ ? { name : "" , value : dec ( name ) }
416
+ : { name, value : dec ( value ) } ;
409
417
410
418
let index = endIdx + 1 ;
411
419
while ( index < len ) {
@@ -414,20 +422,13 @@ export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
414
422
const [ attr , val ] = keyPairSlice ( str , index , endIdx ) ;
415
423
const name = attr . toLowerCase ( ) ;
416
424
417
- if ( name === "max-age" ) {
418
- const num = Number ( val ) ;
419
- if ( Number . isInteger ( num ) ) {
420
- setCookie . maxAge = num ;
421
- }
425
+ if ( name === "max-age" && val && maxAgeRegExp . test ( val ) ) {
426
+ setCookie . maxAge = Number ( val ) ;
422
427
} else if ( name === "domain" ) {
423
- if ( domainValueRegExp . test ( val ) ) {
424
- setCookie . domain = val ;
425
- }
428
+ setCookie . domain = val ;
426
429
} else if ( name === "path" ) {
427
- if ( pathValueRegExp . test ( val ) ) {
428
- setCookie . path = val ;
429
- }
430
- } else if ( name === "expires" ) {
430
+ setCookie . path = val ;
431
+ } else if ( name === "expires" && val ) {
431
432
const date = new Date ( val ) ;
432
433
if ( Number . isFinite ( date . valueOf ( ) ) ) {
433
434
setCookie . expires = date ;
@@ -438,12 +439,12 @@ export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
438
439
setCookie . secure = true ;
439
440
} else if ( name === "partitioned" ) {
440
441
setCookie . partitioned = true ;
441
- } else if ( name === "priority" ) {
442
+ } else if ( name === "priority" && val ) {
442
443
const priority = val . toLowerCase ( ) ;
443
444
if ( priority === "low" || priority === "medium" || priority === "high" ) {
444
445
setCookie . priority = priority ;
445
446
}
446
- } else if ( name === "samesite" ) {
447
+ } else if ( name === "samesite" && val ) {
447
448
const sameSite = val . toLowerCase ( ) ;
448
449
if ( sameSite === "lax" || sameSite === "strict" || sameSite === "none" ) {
449
450
setCookie . sameSite = sameSite ;
@@ -481,10 +482,14 @@ function endIndex(str: string, index: number, min: number) {
481
482
/**
482
483
* Slice out a key=value pair between min to max.
483
484
*/
484
- function keyPairSlice ( str : string , min : number , max : number ) : [ string , string ] {
485
+ function keyPairSlice (
486
+ str : string ,
487
+ min : number ,
488
+ max : number ,
489
+ ) : [ string , string | undefined ] {
485
490
const eqIdx = str . indexOf ( "=" , min ) ;
486
491
if ( eqIdx === - 1 || eqIdx > max ) {
487
- return [ valueSlice ( str , min , max ) , "" ] ;
492
+ return [ valueSlice ( str , min , max ) , undefined ] ;
488
493
}
489
494
490
495
const key = valueSlice ( str , min , eqIdx ) ;
0 commit comments