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

Issue 160 #173

Merged
merged 3 commits into from
Nov 13, 2019
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,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 @@ -981,6 +981,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 @@ -990,6 +993,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 @@ -1213,7 +1217,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