Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add a guard against maliciously-sized cookies #39

Merged
merged 2 commits into from
Dec 13, 2022
Merged
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
5 changes: 5 additions & 0 deletions cookiejar.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@

var cookie_str_splitter = /[:](?=\s*[a-zA-Z0-9_\-]+\s*[=])/g;
Cookie.prototype.parse = function parse(str, request_domain, request_path) {
if ( str.length > 32768 ) {
console.warn("Cookie too long for parsing (>32768 characters)");
return;
}

if (this instanceof Cookie) {
var parts = str.split(";").filter(function (value) {
return !!value;
Expand Down
4 changes: 4 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ assert.equal(cookie.domain, ".test.com");
assert.equal(cookie.path, "/");
assert.deepEqual(cookie, new Cookie("a=1;domain=.test.com;path=/"));

// ensure cookies that are too long are not parsed to avoid any issues with DoS inputs
var too_long_cookie = new Cookie( "foo=" + "blah".repeat( 10000 ) );
assert.equal(too_long_cookie, undefined);

// Test request_path and request_domain
test_jar2.setCookie(new Cookie("sub=4;path=/", "test.com"));
var cookie = test_jar2.getCookie("sub", CookieAccessInfo("sub.test.com", "/"));
Expand Down