Skip to content

Commit

Permalink
Merge branch 'checkForEmptyCookie' of https://github.com/vsin12/tough…
Browse files Browse the repository at this point in the history
…-cookie into checkForEmptyCookie
  • Loading branch information
medelibero-sfdc committed Nov 24, 2020
2 parents dc95a90 + 8ab92a5 commit 7e2c51b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ All notable changes to this project will be documented in this file.
### Minor Changes
- Added parameter checking to setCookie so as to error out when no URL was passed in

## X.Y.Z

### Minor Changes
- Added loose mode to the serialized options. Now a serialized cookie jar with loose mode enabled will honor that flag when deserialized.
- Added allowSpecialUseDomain and prefixSecurity to the serialized options. Now any options accepted passed in to the cookie jar will be honored when serialized and deserialized.

## 4.0.0

### Breaking Changes (Major Version)
Expand Down
15 changes: 13 additions & 2 deletions lib/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,9 @@ class CookieJar {

// CookieJar configuration:
rejectPublicSuffixes: !!this.rejectPublicSuffixes,
enableLooseMode: !!this.enableLooseMode,
allowSpecialUseDomain: !!this.allowSpecialUseDomain,
prefixSecurity: getNormalizedPrefixSecurity(this.prefixSecurity),

// this gets filled from getAllCookies:
cookies: []
Expand Down Expand Up @@ -1620,7 +1623,12 @@ class CookieJar {
serialized = strOrObj;
}

const jar = new CookieJar(store, serialized.rejectPublicSuffixes);
const jar = new CookieJar(store, {
rejectPublicSuffixes: serialized.rejectPublicSuffixes,
looseMode: serialized.enableLooseMode,
allowSpecialUseDomain: serialized.allowSpecialUseDomain,
prefixSecurity: serialized.prefixSecurity
});
jar._importCookies(serialized, err => {
if (err) {
return cb(err);
Expand All @@ -1632,7 +1640,10 @@ class CookieJar {
static deserializeSync(strOrObj, store) {
const serialized =
typeof strOrObj === "string" ? JSON.parse(strOrObj) : strOrObj;
const jar = new CookieJar(store, serialized.rejectPublicSuffixes);
const jar = new CookieJar(store, {
rejectPublicSuffixes: serialized.rejectPublicSuffixes,
looseMode: serialized.enableLooseMode
});

// catch this mistake early:
if (!jar.store.synchronous) {
Expand Down
22 changes: 17 additions & 5 deletions lib/permuteDomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,27 @@ const pubsuffix = require("./pubsuffix-psl");

// Gives the permutation of all possible domainMatch()es of a given domain. The
// array is in shortest-to-longest order. Handy for indexing.
const SPECIAL_USE_DOMAINS = ["local"]; // RFC 6761

// RFC 6761
const SPECIAL_USE_DOMAINS = [
"local",
"example",
"invalid",
"localhost",
"test"
];

function permuteDomain(domain, allowSpecialUseDomain) {
let pubSuf = null;
if (allowSpecialUseDomain) {
const domainParts = domain.split(".");
if (SPECIAL_USE_DOMAINS.includes(domainParts[domainParts.length - 1])) {
pubSuf = `${domainParts[domainParts.length - 2]}.${
domainParts[domainParts.length - 1]
}`;
// If the right-most label in the name is a special-use domain (e.g. bananas.apple.localhost),
// then don't use PSL. This is because most special-use domains are not listed on PSL.
const topLevelDomain = domainParts[domainParts.length - 1];
if (SPECIAL_USE_DOMAINS.includes(topLevelDomain)) {
const secondLevelDomain = domainParts[domainParts.length - 2];
// In aforementioned example, the eTLD/pubSuf will be apple.localhost
pubSuf = `${secondLevelDomain}.${topLevelDomain}`;
} else {
pubSuf = pubsuffix.getPublicSuffix(domain);
}
Expand Down
13 changes: 13 additions & 0 deletions test/cookie_jar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,19 @@ vows
assert.strictEqual(cookies[0].key, "");
assert.strictEqual(cookies[0].value, "FooBar");
}
},
"Loose Mode Cloned": {
topic: function() {
const cj = new CookieJar(null, { looseMode: true });
return CookieJar.fromJSON(cj.toJSON());
},
"parses loose cookies from serialized cookie jar": function(cj) {
cj.setCookieSync("FooBar", "http://www.foonet.net", {});
const cookies = cj.getCookiesSync("http://www.foonet.net");
assert.strictEqual(cookies.length, 1);
assert.strictEqual(cookies[0].key, "");
assert.strictEqual(cookies[0].value, "FooBar");
}
}
})
.addBatch({
Expand Down

0 comments on commit 7e2c51b

Please sign in to comment.