Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion http/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ export function getCookies(
): Partial<Record<string, string>> {
const cookie = headers.get("Cookie");
const out: Partial<Record<string, string>> = Object.create(null);
if (cookie !== null) {
if (cookie) {
const c = cookie.split(";");
for (const kv of c) {
if (kv.trim() === "") continue;
const [cookieKey, ...cookieVal] = kv.split("=");
if (cookieKey === "") {
throw new SyntaxError("Cookie cannot start with '='");
Expand Down
25 changes: 25 additions & 0 deletions http/cookie_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ Deno.test({
},
});

Deno.test("getCookies() handles empty cookie headers", () => {
for (const cookie of ["", " \t "]) {
const headers = new Headers({ Cookie: cookie });
const cookies = getCookies(headers);
assertEquals(cookies, {});
assertEquals(Object.getPrototypeOf(cookies), null);
}
});

Deno.test("getCookies() ignores empty cookie pairs", () => {
for (const cookie of ["a=1;", "a=1; \t ;", "; a=1", "a=1;;;"]) {
assertEquals(getCookies(new Headers({ Cookie: cookie })), { a: "1" });
}
assertEquals(getCookies(new Headers({ Cookie: "; ;\t;" })), {});
assertEquals(
getCookies(new Headers({ Cookie: "a=1; ; b=two ;" })),
{ a: "1", b: "two " },
);
assertThrows(
() => getCookies(new Headers({ Cookie: "a=1;=invalid;" })),
SyntaxError,
"Cookie cannot start with '='",
);
});

Deno.test({
name: "getCookies() has correct types",
fn() {
Expand Down
Loading