Skip to content

Commit bf02d04

Browse files
authored
Don't validate cookie strings passed in the CookieMap constructor (#19945)
Co-authored-by: pfgithub <6010774+pfgithub@users.noreply.github.com>
1 parent 5910504 commit bf02d04

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

src/bun.js/bindings/CookieMap.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,6 @@ ExceptionOr<Ref<CookieMap>> CookieMap::create(std::variant<Vector<Vector<String>
6060
Vector<KeyValuePair<String, String>> cookies;
6161
for (const auto& pair : pairs) {
6262
if (pair.size() == 2) {
63-
if (!pair[1].isEmpty() && !isValidHTTPHeaderValue(pair[1])) {
64-
if (throwOnInvalidCookieString) {
65-
return Exception { TypeError, "Invalid cookie string: cookie value is not valid"_s };
66-
} else {
67-
continue;
68-
}
69-
}
70-
7163
cookies.append(KeyValuePair<String, String>(pair[0], pair[1]));
7264
} else if (throwOnInvalidCookieString) {
7365
return Exception { TypeError, "Invalid cookie string: expected name=value pair"_s };
@@ -78,13 +70,6 @@ ExceptionOr<Ref<CookieMap>> CookieMap::create(std::variant<Vector<Vector<String>
7870
[&](const HashMap<String, String>& pairs) -> ExceptionOr<Ref<CookieMap>> {
7971
Vector<KeyValuePair<String, String>> cookies;
8072
for (const auto& entry : pairs) {
81-
if (!entry.value.isEmpty() && !isValidHTTPHeaderValue(entry.value)) {
82-
if (throwOnInvalidCookieString) {
83-
return Exception { TypeError, "Invalid cookie string: cookie value is not valid"_s };
84-
} else {
85-
continue;
86-
}
87-
}
8873
cookies.append(KeyValuePair<String, String>(entry.key, entry.value));
8974
}
9075

test/js/bun/cookie/cookie.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ describe("cookie path option", () => {
323323
"/x/y": {
324324
GET(r) {
325325
r.cookies.set("user", "a", { maxAge: 3600, path: "/" });
326-
const cookie = r.cookies.toSetCookieHeaders().at(0);
326+
const cookie = r.cookies.toSetCookieHeaders().at(0)!;
327327
return new Response("ok", {
328328
headers: { "set-cookie": cookie },
329329
});
@@ -371,3 +371,45 @@ test("delete cookie invalid path option", () => {
371371
`"Invalid cookie name: contains invalid characters"`,
372372
);
373373
});
374+
375+
describe("Bun.CookieMap constructor", () => {
376+
test("throws for invalid array", () => {
377+
expect(() => new Bun.CookieMap([["abc defg =fhaingj809读写汉字学中文"]])).toThrowErrorMatchingInlineSnapshot(
378+
`"Expected arrays of exactly two strings"`,
379+
);
380+
});
381+
test("accepts unicode cookie value in object", () => {
382+
const map = new Bun.CookieMap({
383+
"cookie key": "读写汉字学中文",
384+
});
385+
expect(map.get("cookie key")).toBe("读写汉字学中文");
386+
});
387+
test("accepts unicode cookie value in array", () => {
388+
const map = new Bun.CookieMap([["cookie key", "读写汉字学中文"]]);
389+
expect(map.get("cookie key")).toBe("读写汉字学中文");
390+
});
391+
test("accepts unicode cookie value in string", () => {
392+
const map = new Bun.CookieMap("cookie key=读写汉字学中文");
393+
expect(map.get("cookie key")).toBe("读写汉字学中文");
394+
});
395+
test("serializes unicode cookie value", () => {
396+
const map = new Bun.CookieMap();
397+
map.set("cookiekey", "读写汉字学中文");
398+
expect(map.toSetCookieHeaders()).toMatchInlineSnapshot(`
399+
[
400+
"cookiekey=%E8%AF%BB%E5%86%99%E6%B1%89%E5%AD%97%E5%AD%A6%E4%B8%AD%E6%96%87; Path=/; SameSite=Lax",
401+
]
402+
`);
403+
// re-parse
404+
const reparsed = new Bun.CookieMap(map.toSetCookieHeaders()[0].split(";")[0]!);
405+
expect(reparsed.get("cookiekey")).toBe("读写汉字学中文");
406+
});
407+
test("doesn't parse percent encoded value in object or array", () => {
408+
const map = new Bun.CookieMap({
409+
"cookiekey": "%E8%AF%BB%E5%86%99%E6%B1%89%E5%AD%97%E5%AD%A6%E4%B8%AD%E6%96%87",
410+
});
411+
const map2 = new Bun.CookieMap([["cookiekey", "%E8%AF%BB%E5%86%99%E6%B1%89%E5%AD%97%E5%AD%A6%E4%B8%AD%E6%96%87"]]);
412+
expect(map.get("cookiekey")).toBe("%E8%AF%BB%E5%86%99%E6%B1%89%E5%AD%97%E5%AD%A6%E4%B8%AD%E6%96%87");
413+
expect(map2.get("cookiekey")).toBe("%E8%AF%BB%E5%86%99%E6%B1%89%E5%AD%97%E5%AD%A6%E4%B8%AD%E6%96%87");
414+
});
415+
});

0 commit comments

Comments
 (0)