Skip to content

Commit

Permalink
Merge pull request #54 from eliassorensen/6265bis
Browse files Browse the repository at this point in the history
Change name-value-pair parsing to follow 6265bis

Specifically, if there's no "=", (e.g. `Set-Cookie: foo`) then the name is now "", and the value is the entire string (e.g. "foo")

This matches browser behavior and the newer draft RFC

(Previous behavior was to use the entire string as the name, and treat the value as empty.)
  • Loading branch information
nfriedly authored Jul 25, 2022
2 parents 6687aa0 + 5cae030 commit f87130b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ Returns an array of strings that may be passed to `parse()`.
## References
* [RFC 6265: HTTP State Management Mechanism](https://tools.ietf.org/html/rfc6265)
* [draft-ietf-httpbis-rfc6265bis-10](https://httpwg.org/http-extensions/draft-ietf-httpbis-rfc6265bis.html)
## License
Expand Down
26 changes: 22 additions & 4 deletions lib/set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ function isNonEmptyString(str) {

function parseString(setCookieValue, options) {
var parts = setCookieValue.split(";").filter(isNonEmptyString);
var nameValue = parts.shift().split("=");
var name = nameValue.shift();
var value = nameValue.join("="); // everything after the first =, joined by a "=" if there was more than one part

var nameValuePairStr = parts.shift();
var parsed = parseNameValuePair(nameValuePairStr);
var name = parsed.name;
var value = parsed.value;

options = options
? Object.assign({}, defaultParseOptions, options)
Expand All @@ -32,7 +34,7 @@ function parseString(setCookieValue, options) {
}

var cookie = {
name: name, // grab everything before the first =
name: name,
value: value,
};

Expand All @@ -58,6 +60,22 @@ function parseString(setCookieValue, options) {
return cookie;
}

function parseNameValuePair(nameValuePairStr) {
// Parses name-value-pair according to rfc6265bis draft

var name = "";
var value = "";
var nameValueArr = nameValuePairStr.split("=");
if (nameValueArr.length > 1) {
name = nameValueArr.shift();
value = nameValueArr.join("="); // everything after the first =, joined by a "=" if there was more than one part
} else {
value = nameValuePairStr;
}

return { name: name, value: value };
}

function parse(input, options) {
options = options
? Object.assign({}, defaultParseOptions, options)
Expand Down
11 changes: 11 additions & 0 deletions test/set-cookie-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,15 @@ describe("set-cookie-parser", function () {
expected = {};
assert.deepEqual(actual, expected);
});

it("should have empty name string, and value is the name-value-pair if the name-value-pair string lacks a = character", function () {
var actual = setCookie.parse("foo;");
var expected = [{ name: "", value: "foo" }];

assert.deepEqual(actual, expected);

actual = setCookie.parse("foo;SameSite=None;Secure");
expected = [{ name: "", value: "foo", sameSite: "None", secure: true }];
assert.deepEqual(actual, expected);
});
});

0 comments on commit f87130b

Please sign in to comment.