Skip to content

Commit

Permalink
fix: allow set cookies with localhost
Browse files Browse the repository at this point in the history
Adding more tests to cover the breaking use cases noted in #246.

e.g.;.
* `new CookieJar().setCookieSync("settingThisShouldPass=true; Domain=localhost; Path=/;", "http://localhost")`

Also modifies the assertion for a test introduced in #221 that may be incorrect.
  • Loading branch information
colincasey committed Aug 25, 2022
1 parent ec70796 commit 8b06ee7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lib/pubsuffix-psl.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ function getPublicSuffix(domain, options = {}) {
}

if (!ignoreError && SPECIAL_USE_DOMAINS.includes(topLevelDomain)) {
if (allowSpecialUseDomain) {
return "";
}
throw new Error(
`Cookie has domain set to the public suffix "${topLevelDomain}" which is a special use domain. To allow this, configure your CookieJar with {allowSpecialUseDomain:true, rejectPublicSuffixes: false}.`
);
Expand Down
44 changes: 42 additions & 2 deletions test/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,46 @@ function allowSpecialUseOptionVows() {
return specialUseDomains.reduce((vows, specialUseDomain) => {
vows[
`cookie jar with allowSpecialUseDomain set to the default value and domain is "${specialUseDomain}"`
] = {
topic: function() {
const cb = this.callback;
const cj = new CookieJar();
cj.setCookie(
`settingThisShouldPass=true; Domain=${specialUseDomain}; Path=/;`,
`http://${specialUseDomain}`,
at(-1),
(err, cookie) => {
cb(err, { cj: cj, cookie: cookie });
}
);
},
"set the cookie": function(t) {
assert.ok(t.cookie, "didn't set?!");
assert.equal(t.cookie.key, "settingThisShouldPass");
},
"then, retrieving": {
topic: function(t) {
const cb = this.callback;
setTimeout(() => {
t.cj.getCookies(
`http://${specialUseDomain}`,
{ http: true },
(err, cookies) => {
t.cookies = cookies;
cb(err, t);
}
);
}, 2000);
},
"got the cookie": function(t) {
assert.lengthOf(t.cookies, 1);
assert.equal(t.cookies[0].key, "settingThisShouldPass");
}
}
};

vows[
`cookie jar with allowSpecialUseDomain set to the default value and domain is "dev.${specialUseDomain}"`
] = {
topic: function() {
const cb = this.callback;
Expand Down Expand Up @@ -633,7 +673,7 @@ function allowSpecialUseOptionVows() {
};

vows[
`cookie jar with allowSpecialUseDomain enabled and domain is "${specialUseDomain}"`
`cookie jar with allowSpecialUseDomain enabled and domain is "dev.${specialUseDomain}"`
] = {
topic: function() {
const cb = this.callback;
Expand Down Expand Up @@ -676,7 +716,7 @@ function allowSpecialUseOptionVows() {
};

vows[
`cookie jar with allowSpecialUseDomain disabled and domain is "${specialUseDomain}"`
`cookie jar with allowSpecialUseDomain disabled and domain is "dev.${specialUseDomain}"`
] = {
topic: function() {
const cj = new CookieJar(new tough.MemoryCookieStore(), {
Expand Down
8 changes: 3 additions & 5 deletions test/regression_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,18 @@ vows
return cookieJar.setCookieSync(
"a=b; Domain=localhost",
"http://localhost"
); // when domain set to 'localhost', will throw 'Error: Cookie has domain set to a public suffix'
);
},
works: function(err, c) {
// localhost as domain throws an error, cookie should not be defined
assert.instanceOf(err, Error);
assert.isUndefined(c);
assert.instanceOf(c, Cookie);
}
}
},
{
"setCookie with localhost (GH-215) (null domain)": {
topic: function() {
const cookieJar = new CookieJar();
return cookieJar.setCookieSync("a=b; Domain=", "http://localhost"); // when domain set to 'localhost', will throw 'Error: Cookie has domain set to a public suffix'
return cookieJar.setCookieSync("a=b; Domain=", "http://localhost");
},
works: function(c) {
assert.instanceOf(c, Cookie);
Expand Down

0 comments on commit 8b06ee7

Please sign in to comment.