Skip to content

Commit

Permalink
Merge pull request #173 from miggs125/issue-160
Browse files Browse the repository at this point in the history
Issue 160
  • Loading branch information
medelibero-sfdc authored Nov 13, 2019
2 parents 907d037 + 75acb83 commit 9cb6662
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ The `options` object can be omitted and can have the following properties:

* _rejectPublicSuffixes_ - boolean - default `true` - reject cookies with domains like "com" and "co.uk"
* _looseMode_ - boolean - default `false` - accept malformed cookies like `bar` and `=bar`, which have an implied empty name.
* _allowSpecialUseDomain_ - boolean - default `false` - accepts special-use domain suffixes, such as `local`. Useful for testing purposes.
This is not in the standard, but is used sometimes on the web and is accepted by (most) browsers.

Since eventually this module would like to support database/remote/etc. CookieJars, continuation passing style is used for CookieJar methods.
Expand Down
6 changes: 5 additions & 1 deletion lib/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,9 @@ function CookieJar(store, options) {
if (options.looseMode != null) {
this.enableLooseMode = options.looseMode;
}
if (options.allowSpecialUseDomain != null) {
this.allowSpecialUseDomain = options.allowSpecialUseDomain;
}

if (!store) {
store = new MemoryCookieStore();
Expand All @@ -1034,6 +1037,7 @@ function CookieJar(store, options) {
CookieJar.prototype.store = null;
CookieJar.prototype.rejectPublicSuffixes = true;
CookieJar.prototype.enableLooseMode = false;
CookieJar.prototype.allowSpecialUseDomain = false;
const CAN_BE_SYNC = [];

CAN_BE_SYNC.push("setCookie");
Expand Down Expand Up @@ -1296,7 +1300,7 @@ CookieJar.prototype.getCookies = function(url, options, cb) {
// TODO persist lastAccessed

cb(null, cookies);
});
}, this.allowSpecialUseDomain);
};

CAN_BE_SYNC.push("getCookieString");
Expand Down
4 changes: 2 additions & 2 deletions lib/memstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ MemoryCookieStore.prototype.findCookie = function(domain, path, key, cb) {
return cb(null, this.idx[domain][path][key] || null);
};

MemoryCookieStore.prototype.findCookies = function(domain, path, cb) {
MemoryCookieStore.prototype.findCookies = function(domain, path, cb, allowSpecialUseDomain) {
const results = [];
if (!domain) {
return cb(null, []);
Expand Down Expand Up @@ -100,7 +100,7 @@ MemoryCookieStore.prototype.findCookies = function(domain, path, cb) {
};
}

const domains = permuteDomain(domain) || [domain];
const domains = permuteDomain(domain, allowSpecialUseDomain) || [domain];
const idx = this.idx;
domains.forEach(curDomain => {
const domainIndex = idx[curDomain];
Expand Down
18 changes: 16 additions & 2 deletions lib/permuteDomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,22 @@ 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.
function permuteDomain(domain) {
const pubSuf = pubsuffix.getPublicSuffix(domain);
const SPECIAL_USE_DOMAINS = ["local"]; // RFC 6761
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]
}`;
} else {
pubSuf = pubsuffix.getPublicSuffix(domain);
}
} else {
pubSuf = pubsuffix.getPublicSuffix(domain);
}

if (!pubSuf) {
return null;
}
Expand Down

0 comments on commit 9cb6662

Please sign in to comment.