Skip to content

Commit

Permalink
Merge branch 'master' into feat/special-use-domains
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivanKaul committed Oct 10, 2020
2 parents da488f0 + 0ccbbec commit 30c1033
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 7 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to this project will be documented in this file.

## 4.X.X

### 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.

## 4.0.0

### Breaking Changes (Major Version)
Expand Down
105 changes: 105 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Salesforce Open Source Community Code of Conduct

## About the Code of Conduct

Equality is a core value at Salesforce. We believe a diverse and inclusive
community fosters innovation and creativity, and are committed to building a
culture where everyone feels included.

Salesforce open-source projects are committed to providing a friendly, safe, and
welcoming environment for all, regardless of gender identity and expression,
sexual orientation, disability, physical appearance, body size, ethnicity, nationality,
race, age, religion, level of experience, education, socioeconomic status, or
other similar personal characteristics.

The goal of this code of conduct is to specify a baseline standard of behavior so
that people with different social values and communication styles can work
together effectively, productively, and respectfully in our open source community.
It also establishes a mechanism for reporting issues and resolving conflicts.

All questions and reports of abusive, harassing, or otherwise unacceptable behavior
in a Salesforce open-source project may be reported by contacting the Salesforce
Open Source Conduct Committee at ossconduct@salesforce.com.

## Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of gender
identity and expression, sexual orientation, disability, physical appearance,
body size, ethnicity, nationality, race, age, religion, level of experience, education,
socioeconomic status, or other similar personal characteristics.

## Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy toward other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Personal attacks, insulting/derogatory comments, or trolling
* Public or private harassment
* Publishing, or threatening to publish, others' private information—such as
a physical or electronic address—without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
* Advocating for or encouraging any of the above behaviors

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned with this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project email
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the Salesforce Open Source Conduct Committee
at ossconduct@salesforce.com. All complaints will be reviewed and investigated
and will result in a response that is deemed necessary and appropriate to the
circumstances. The committee is obligated to maintain confidentiality with
regard to the reporter of an incident. Further details of specific enforcement
policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership and the Salesforce Open Source Conduct
Committee.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][contributor-covenant-home],
version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html.
It includes adaptions and additions from [Go Community Code of Conduct][golang-coc],
[CNCF Code of Conduct][cncf-coc], and [Microsoft Open Source Code of Conduct][microsoft-coc].

This Code of Conduct is licensed under the [Creative Commons Attribution 3.0 License][cc-by-3-us].

[contributor-covenant-home]: https://www.contributor-covenant.org (https://www.contributor-covenant.org/)
[golang-coc]: https://golang.org/conduct
[cncf-coc]: https://github.com/cncf/foundation/blob/master/code-of-conduct.md
[microsoft-coc]: https://opensource.microsoft.com/codeofconduct/
[cc-by-3-us]: https://creativecommons.org/licenses/by/3.0/us/
7 changes: 7 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Security

Please report any security issue to [security@salesforce.com](mailto:security@salesforce.com)
as soon as it is discovered. This library limits its runtime dependencies in
order to reduce the total cost of ownership as much as can be, but all consumers
should remain vigilant and have their security stakeholders review all third-party
products (3PP) like this one and their dependencies.
49 changes: 42 additions & 7 deletions lib/cookie.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright (c) 2015, Salesforce.com, Inc.
* Copyright (c) 2015-2020, Salesforce.com, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -36,6 +36,7 @@ const pubsuffix = require("./pubsuffix-psl");
const Store = require("./store").Store;
const MemoryCookieStore = require("./memstore").MemoryCookieStore;
const pathMatch = require("./pathMatch").pathMatch;
const validators = require("./validators.js");
const VERSION = require("./version");
const { fromCallback } = require("universalify");

Expand Down Expand Up @@ -79,6 +80,7 @@ const SAME_SITE_CONTEXT_VAL_ERR =
'Invalid sameSiteContext option for getCookies(); expected one of "strict", "lax", or "none"';

function checkSameSiteContext(value) {
validators.validate(validators.isNonEmptyString(value), value);
const context = String(value).toLowerCase();
if (context === "none" || context === "lax" || context === "strict") {
return context;
Expand All @@ -97,7 +99,7 @@ const PrefixSecurityEnum = Object.freeze({
// * all capturing groups converted to non-capturing -- "(?:)"
// * support for IPv6 Scoped Literal ("%eth1") removed
// * lowercase hexadecimal only
var IP_REGEX_LOWERCASE =/(?:^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$)|(?:^(?:(?:[a-f\d]{1,4}:){7}(?:[a-f\d]{1,4}|:)|(?:[a-f\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d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* Parses a Natural number (i.e., non-negative integer) with either the
Expand Down Expand Up @@ -301,6 +303,7 @@ function parseDate(str) {
}

function formatDate(date) {
validators.validate(validators.isDate(date), date);
return date.toUTCString();
}

Expand Down Expand Up @@ -403,6 +406,7 @@ function defaultPath(path) {
}

function trimTerminator(str) {
if (validators.isEmptyString(str)) return str;
for (let t = 0; t < TERMINATORS.length; t++) {
const terminatorIdx = str.indexOf(TERMINATORS[t]);
if (terminatorIdx !== -1) {
Expand All @@ -415,6 +419,7 @@ function trimTerminator(str) {

function parseCookiePair(cookiePair, looseMode) {
cookiePair = trimTerminator(cookiePair);
validators.validate(validators.isString(cookiePair), cookiePair);

let firstEq = cookiePair.indexOf("=");
if (looseMode) {
Expand Down Expand Up @@ -616,6 +621,7 @@ function parse(str, options) {
* @returns boolean
*/
function isSecurePrefixConditionMet(cookie) {
validators.validate(validators.isObject(cookie), cookie);
return !cookie.key.startsWith("__Secure-") || cookie.secure;
}

Expand All @@ -631,6 +637,7 @@ function isSecurePrefixConditionMet(cookie) {
* @returns boolean
*/
function isHostPrefixConditionMet(cookie) {
validators.validate(validators.isObject(cookie));
return (
!cookie.key.startsWith("__Host-") ||
(cookie.secure &&
Expand Down Expand Up @@ -698,6 +705,8 @@ function fromJSON(str) {
*/

function cookieCompare(a, b) {
validators.validate(validators.isObject(a), a);
validators.validate(validators.isObject(b), b);
let cmp = 0;

// descending for length: b CMP a
Expand Down Expand Up @@ -725,6 +734,7 @@ function cookieCompare(a, b) {
// Gives the permutation of all possible pathMatch()es of a given path. The
// array is in longest-to-shortest order. Handy for indexing.
function permutePath(path) {
validators.validate(validators.isString(path));
if (path === "/") {
return ["/"];
}
Expand Down Expand Up @@ -1060,6 +1070,7 @@ class CookieJar {
if (typeof options === "boolean") {
options = { rejectPublicSuffixes: options };
}
validators.validate(validators.isObject(options), options);
this.rejectPublicSuffixes = options.rejectPublicSuffixes;
this.enableLooseMode = !!options.looseMode;
this.allowSpecialUseDomain = !!options.allowSpecialUseDomain;
Expand All @@ -1076,12 +1087,20 @@ class CookieJar {
}

setCookie(cookie, url, options, cb) {
validators.validate(validators.isNonEmptyString(url), cb, options);
let err;

if (validators.isFunction(url)) {
cb = url;
return cb(new Error("No URL was specified"));
}

const context = getCookieContext(url);
if (typeof options === "function") {
if (validators.isFunction(options)) {
cb = options;
options = {};
}
validators.validate(validators.isFunction(cb), cb);

const host = canonicalDomain(context.hostname);
const loose = options.loose || this.enableLooseMode;
Expand Down Expand Up @@ -1249,11 +1268,14 @@ class CookieJar {

// RFC6365 S5.4
getCookies(url, options, cb) {
validators.validate(validators.isNonEmptyString(url), cb, url);
const context = getCookieContext(url);
if (typeof options === "function") {
if (validators.isFunction(options)) {
cb = options;
options = {};
}
validators.validate(validators.isObject(options), cb, options);
validators.validate(validators.isFunction(cb), cb);

const host = canonicalDomain(context.hostname);
const path = context.pathname || "/";
Expand Down Expand Up @@ -1369,6 +1391,7 @@ class CookieJar {

getCookieString(...args) {
const cb = args.pop();
validators.validate(validators.isFunction(cb), cb);
const next = function(err, cookies) {
if (err) {
cb(err);
Expand All @@ -1388,6 +1411,7 @@ class CookieJar {

getSetCookieStrings(...args) {
const cb = args.pop();
validators.validate(validators.isFunction(cb), cb);
const next = function(err, cookies) {
if (err) {
cb(err);
Expand All @@ -1405,8 +1429,9 @@ class CookieJar {
}

serialize(cb) {
validators.validate(validators.isFunction(cb), cb);
let type = this.store.constructor.name;
if (type === "Object") {
if (validators.isObject(type)) {
type = null;
}

Expand All @@ -1422,6 +1447,7 @@ class CookieJar {

// CookieJar configuration:
rejectPublicSuffixes: !!this.rejectPublicSuffixes,
enableLooseMode: !!this.enableLooseMode,

// this gets filled from getAllCookies:
cookies: []
Expand Down Expand Up @@ -1524,6 +1550,7 @@ class CookieJar {
}

removeAllCookies(cb) {
validators.validate(validators.isFunction(cb), cb);
const store = this.store;

// Check that the store implements its own removeAllCookies(). The default
Expand Down Expand Up @@ -1577,6 +1604,7 @@ class CookieJar {
cb = store;
store = null;
}
validators.validate(validators.isFunction(cb), cb);

let serialized;
if (typeof strOrObj === "string") {
Expand All @@ -1588,7 +1616,10 @@ class CookieJar {
serialized = strOrObj;
}

const jar = new CookieJar(store, serialized.rejectPublicSuffixes);
const jar = new CookieJar(store, {
rejectPublicSuffixes: serialized.rejectPublicSuffixes,
looseMode: serialized.enableLooseMode
});
jar._importCookies(serialized, err => {
if (err) {
return cb(err);
Expand All @@ -1600,7 +1631,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 Expand Up @@ -1669,3 +1703,4 @@ exports.permuteDomain = require("./permuteDomain").permuteDomain;
exports.permutePath = permutePath;
exports.canonicalDomain = canonicalDomain;
exports.PrefixSecurityEnum = PrefixSecurityEnum;
exports.ParameterError = validators.ParameterError;

0 comments on commit 30c1033

Please sign in to comment.