Skip to content

Commit baf9cc9

Browse files
committed
Fix skip over of boolean attributes
1 parent 6fea506 commit baf9cc9

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

src/index.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export function stringifySetCookie(
301301
throw new TypeError(`argument name is invalid: ${cookie.name}`);
302302
}
303303

304-
const value = enc(cookie.value || "");
304+
const value = cookie.value ? enc(cookie.value) : "";
305305

306306
if (!cookieValueRegExp.test(value)) {
307307
throw new TypeError(`argument val is invalid: ${cookie.value}`);
@@ -424,32 +424,34 @@ export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
424424
eqIdx === -1
425425
? valueSlice(str, index, endIdx)
426426
: valueSlice(str, index, eqIdx);
427-
const name = attr.toLowerCase();
427+
const val = eqIdx === -1 ? undefined : valueSlice(str, eqIdx + 1, endIdx);
428428

429-
// Handle boolean attributes.
430-
if (eqIdx === -1) {
431-
if (name === "httponly") {
429+
switch (attr.toLowerCase()) {
430+
case "httponly":
432431
setCookie.httpOnly = true;
433-
} else if (name === "secure") {
432+
break;
433+
case "secure":
434434
setCookie.secure = true;
435-
} else if (name === "partitioned") {
435+
break;
436+
case "partitioned":
436437
setCookie.partitioned = true;
437-
}
438-
} else {
439-
const val = valueSlice(str, eqIdx + 1, endIdx);
440-
441-
if (name === "max-age") {
442-
if (maxAgeRegExp.test(val)) setCookie.maxAge = Number(val);
443-
} else if (name === "domain") {
438+
break;
439+
case "domain":
444440
setCookie.domain = val;
445-
} else if (name === "path") {
441+
break;
442+
case "path":
446443
setCookie.path = val;
447-
} else if (name === "expires") {
444+
break;
445+
case "max-age":
446+
if (val && maxAgeRegExp.test(val)) setCookie.maxAge = Number(val);
447+
break;
448+
case "expires":
449+
if (!val) break;
448450
const date = new Date(val);
449-
if (Number.isFinite(date.valueOf())) {
450-
setCookie.expires = date;
451-
}
452-
} else if (name === "priority") {
451+
if (Number.isFinite(date.valueOf())) setCookie.expires = date;
452+
break;
453+
case "priority":
454+
if (!val) break;
453455
const priority = val.toLowerCase();
454456
if (
455457
priority === "low" ||
@@ -458,7 +460,9 @@ export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
458460
) {
459461
setCookie.priority = priority;
460462
}
461-
} else if (name === "samesite") {
463+
break;
464+
case "samesite":
465+
if (!val) break;
462466
const sameSite = val.toLowerCase();
463467
if (
464468
sameSite === "lax" ||
@@ -467,7 +471,7 @@ export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
467471
) {
468472
setCookie.sameSite = sameSite;
469473
}
470-
}
474+
break;
471475
}
472476

473477
index = endIdx + 1;

src/parse-set-cookie.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ describe("cookie.parseSetCookie", () => {
6565
});
6666
});
6767

68+
it("should ignore value of boolean attributes", () => {
69+
expect(parseSetCookie("key=value; HttpOnly=true; Secure=false")).toEqual({
70+
name: "key",
71+
value: "value",
72+
httpOnly: true,
73+
secure: true,
74+
});
75+
});
76+
6877
it("should handle attributes with extra spaces", () => {
6978
expect(parseSetCookie("key=value; HttpOnly ; Secure ")).toEqual({
7079
name: "key",
@@ -135,6 +144,13 @@ describe("cookie.parseSetCookie", () => {
135144
});
136145
});
137146

147+
it("should ignore max-age with decimals", () => {
148+
expect(parseSetCookie("key=value; Max-Age=1.5")).toEqual({
149+
name: "key",
150+
value: "value",
151+
});
152+
});
153+
138154
it("should parse negative max-age", () => {
139155
expect(parseSetCookie("key=value; Max-Age=-1")).toEqual({
140156
name: "key",

0 commit comments

Comments
 (0)