Skip to content

Commit 0dc9480

Browse files
authored
Merge pull request #80 from spokodev/fix/trim-attribute-whitespace
fix: trim whitespace from cookie attribute names and values (RFC 6265 5.2)
2 parents f79d1cd + 9657b39 commit 0dc9480

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

lib/set-cookie.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ function parseString(setCookieValue, options) {
4848

4949
parts.forEach(function (part) {
5050
var sides = part.split("=");
51-
var key = sides.shift().trimLeft().toLowerCase();
51+
var key = sides.shift().trim().toLowerCase();
5252
if (isForbiddenKey(key)) {
5353
return;
5454
}
55-
var value = sides.join("=");
55+
var value = sides.join("=").trim();
5656
if (key === "expires") {
5757
cookie.expires = new Date(value);
5858
} else if (key === "max-age") {

test/set-cookie-parser.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,36 @@ describe("set-cookie-parser", function () {
247247
assert.deepEqual(actual, expected);
248248
});
249249

250+
it("should trim whitespace around attribute names and values (rfc 6265 5.2 step 3)", function () {
251+
var actual = parseSetCookie(
252+
"foo=bar; Domain= .example.com; Path= /admin; SameSite= Lax"
253+
);
254+
var expected = [
255+
{
256+
name: "foo",
257+
value: "bar",
258+
domain: ".example.com",
259+
path: "/admin",
260+
sameSite: "Lax",
261+
},
262+
];
263+
assert.deepEqual(actual, expected);
264+
265+
actual = parseSetCookie("foo=bar; Domain=.example.com ; Path=/ ");
266+
expected = [
267+
{ name: "foo", value: "bar", domain: ".example.com", path: "/" },
268+
];
269+
assert.deepEqual(actual, expected);
270+
271+
actual = parseSetCookie("foo=bar; Secure ");
272+
expected = [{ name: "foo", value: "bar", secure: true }];
273+
assert.deepEqual(actual, expected);
274+
275+
actual = parseSetCookie("foo=bar; Path =/x");
276+
expected = [{ name: "foo", value: "bar", path: "/x" }];
277+
assert.deepEqual(actual, expected);
278+
});
279+
250280
describe("split option", function () {
251281
const cookieA = "a=b";
252282
const cookieB = "b=c";

0 commit comments

Comments
 (0)