tough-cookie 2.4.3 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +80 -5
- package/lib/cookie.js +1007 -767
- package/lib/memstore.js +140 -126
- package/lib/pathMatch.js +2 -2
- package/lib/permuteDomain.js +22 -8
- package/lib/pubsuffix-psl.js +2 -2
- package/lib/store.js +36 -31
- package/lib/version.js +2 -0
- package/package.json +50 -17
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://nodei.co/npm/tough-cookie/)
|
|
4
4
|
|
|
5
|
-
[](https://travis-ci.org/salesforce/tough-cookie)
|
|
6
6
|
|
|
7
7
|
# Synopsis
|
|
8
8
|
|
|
@@ -149,6 +149,7 @@ Cookie object properties:
|
|
|
149
149
|
* _path_ - string - the `Path=` of the cookie
|
|
150
150
|
* _secure_ - boolean - the `Secure` cookie flag
|
|
151
151
|
* _httpOnly_ - boolean - the `HttpOnly` cookie flag
|
|
152
|
+
* _sameSite_ - string - the `SameSite` cookie attribute (from [RFC6265bis]); must be one of `none`, `lax`, or `strict`
|
|
152
153
|
* _extensions_ - `Array` - any unrecognized cookie attributes as strings (even if equal-signs inside)
|
|
153
154
|
* _creation_ - `Date` - when this cookie was constructed
|
|
154
155
|
* _creationIndex_ - number - set at construction, used to provide greater sort precision (please see `cookieCompare(a,b)` for a full explanation)
|
|
@@ -198,7 +199,7 @@ compute the TTL relative to `now` (milliseconds). The same precedence rules as
|
|
|
198
199
|
|
|
199
200
|
The "number" `Infinity` is returned for cookies without an explicit expiry and `0` is returned if the cookie is expired. Otherwise a time-to-live in milliseconds is returned.
|
|
200
201
|
|
|
201
|
-
### `.
|
|
202
|
+
### `.canonicalizedDomain()`
|
|
202
203
|
|
|
203
204
|
### `.cdomain()`
|
|
204
205
|
|
|
@@ -251,6 +252,8 @@ The `options` object can be omitted and can have the following properties:
|
|
|
251
252
|
|
|
252
253
|
* _rejectPublicSuffixes_ - boolean - default `true` - reject cookies with domains like "com" and "co.uk"
|
|
253
254
|
* _looseMode_ - boolean - default `false` - accept malformed cookies like `bar` and `=bar`, which have an implied empty name.
|
|
255
|
+
* _prefixSecurity_ - string - default `silent` - set to `'unsafe-disabled'`, `'silent'`, or `'strict'`. See [Cookie Prefixes] below.
|
|
256
|
+
* _allowSpecialUseDomain_ - boolean - default `false` - accepts special-use domain suffixes, such as `local`. Useful for testing purposes.
|
|
254
257
|
This is not in the standard, but is used sometimes on the web and is accepted by (most) browsers.
|
|
255
258
|
|
|
256
259
|
Since eventually this module would like to support database/remote/etc. CookieJars, continuation passing style is used for CookieJar methods.
|
|
@@ -265,6 +268,7 @@ The `options` object can be omitted and can have the following properties:
|
|
|
265
268
|
* _secure_ - boolean - autodetect from url - indicates if this is a "Secure" API. If the currentUrl starts with `https:` or `wss:` then this is defaulted to `true`, otherwise `false`.
|
|
266
269
|
* _now_ - Date - default `new Date()` - what to use for the creation/access time of cookies
|
|
267
270
|
* _ignoreError_ - boolean - default `false` - silently ignore things like parse errors and invalid domains. `Store` errors aren't ignored by this option.
|
|
271
|
+
* _sameSiteContext_ - string - default unset - set to `'none'`, `'lax'`, or `'strict'` See [SameSite Cookies] below.
|
|
268
272
|
|
|
269
273
|
As per the RFC, the `.hostOnly` property is set if there was no "Domain=" parameter in the cookie string (or `.domain` was null on the Cookie object). The `.domain` property is set to the fully-qualified hostname of `currentUrl` in this case. Matching this cookie requires an exact hostname match (not a `domainMatch` as per usual).
|
|
270
274
|
|
|
@@ -285,6 +289,7 @@ The `options` object can be omitted and can have the following properties:
|
|
|
285
289
|
* _now_ - Date - default `new Date()` - what to use for the creation/access time of cookies
|
|
286
290
|
* _expire_ - boolean - default `true` - perform expiry-time checking of cookies and asynchronously remove expired cookies from the store. Using `false` will return expired cookies and **not** remove them from the store (which is useful for replaying Set-Cookie headers, potentially).
|
|
287
291
|
* _allPaths_ - boolean - default `false` - if `true`, do not scope cookies by path. The default uses RFC-compliant path scoping. **Note**: may not be supported by the underlying store (the default `MemoryCookieStore` supports it).
|
|
292
|
+
* _sameSiteContext_ - string - default unset - Set this to `'none'`, `'lax'` or `'strict'` to enforce SameSite cookies upon retrival. See [SameSite Cookies] below.
|
|
288
293
|
|
|
289
294
|
The `.lastAccessed` property of the returned cookies will have been updated.
|
|
290
295
|
|
|
@@ -354,6 +359,16 @@ The `store` argument is optional, but must be a _synchronous_ `Store` instance i
|
|
|
354
359
|
|
|
355
360
|
The _source_ and _destination_ must both be synchronous `Store`s. If one or both stores are asynchronous, use `.clone` instead. Recall that `MemoryCookieStore` supports both synchronous and asynchronous API calls.
|
|
356
361
|
|
|
362
|
+
### `.removeAllCookies(cb(err))`
|
|
363
|
+
|
|
364
|
+
Removes all cookies from the jar.
|
|
365
|
+
|
|
366
|
+
This is a new backwards-compatible feature of `tough-cookie` version 2.5, so not all Stores will implement it efficiently. For Stores that do not implement `removeAllCookies`, the fallback is to call `removeCookie` after `getAllCookies`. If `getAllCookies` fails or isn't implemented in the Store, that error is returned. If one or more of the `removeCookie` calls fail, only the first error is returned.
|
|
367
|
+
|
|
368
|
+
### `.removeAllCookiesSync()`
|
|
369
|
+
|
|
370
|
+
Sync version of `.removeAllCookies()`
|
|
371
|
+
|
|
357
372
|
## Store
|
|
358
373
|
|
|
359
374
|
Base class for CookieJar stores. Available as `tough.Store`.
|
|
@@ -418,19 +433,29 @@ Removes matching cookies from the store. The `path` parameter is optional, and
|
|
|
418
433
|
|
|
419
434
|
Pass an error ONLY if removing any existing cookies failed.
|
|
420
435
|
|
|
436
|
+
### `store.removeAllCookies(cb(err))`
|
|
437
|
+
|
|
438
|
+
_Optional_. Removes all cookies from the store.
|
|
439
|
+
|
|
440
|
+
Pass an error if one or more cookies can't be removed.
|
|
441
|
+
|
|
442
|
+
**Note**: New method as of `tough-cookie` version 2.5, so not all Stores will implement this, plus some stores may choose not to implement this.
|
|
443
|
+
|
|
421
444
|
### `store.getAllCookies(cb(err, cookies))`
|
|
422
445
|
|
|
423
|
-
Produces an `Array` of all cookies during `jar.serialize()`. The items in the array can be true `Cookie` objects or generic `Object`s with the [Serialization Format] data structure.
|
|
446
|
+
_Optional_. Produces an `Array` of all cookies during `jar.serialize()`. The items in the array can be true `Cookie` objects or generic `Object`s with the [Serialization Format] data structure.
|
|
424
447
|
|
|
425
448
|
Cookies SHOULD be returned in creation order to preserve sorting via `compareCookies()`. For reference, `MemoryCookieStore` will sort by `.creationIndex` since it uses true `Cookie` objects internally. If you don't return the cookies in creation order, they'll still be sorted by creation time, but this only has a precision of 1ms. See `compareCookies` for more detail.
|
|
426
449
|
|
|
427
450
|
Pass an error if retrieval fails.
|
|
428
451
|
|
|
452
|
+
**Note**: not all Stores can implement this due to technical limitations, so it is optional.
|
|
453
|
+
|
|
429
454
|
## MemoryCookieStore
|
|
430
455
|
|
|
431
456
|
Inherits from `Store`.
|
|
432
457
|
|
|
433
|
-
A just-in-memory CookieJar synchronous store implementation, used by default. Despite being a synchronous implementation, it's usable with both the synchronous and asynchronous forms of the `CookieJar` API.
|
|
458
|
+
A just-in-memory CookieJar synchronous store implementation, used by default. Despite being a synchronous implementation, it's usable with both the synchronous and asynchronous forms of the `CookieJar` API. Supports serialization, `getAllCookies`, and `removeAllCookies`.
|
|
434
459
|
|
|
435
460
|
## Community Cookie Stores
|
|
436
461
|
|
|
@@ -471,9 +496,59 @@ These are some Store implementations authored and maintained by the community. T
|
|
|
471
496
|
}
|
|
472
497
|
```
|
|
473
498
|
|
|
499
|
+
# RFC6265bis
|
|
500
|
+
|
|
501
|
+
Support for RFC6265bis revision 02 is being developed. Since this is a bit of an omnibus revision to the RFC6252, support is broken up into the functional areas.
|
|
502
|
+
|
|
503
|
+
## Leave Secure Cookies Alone
|
|
504
|
+
|
|
505
|
+
Not yet supported.
|
|
506
|
+
|
|
507
|
+
This change makes it so that if a cookie is sent from the server to the client with a `Secure` attribute, the channel must also be secure or the cookie is ignored.
|
|
508
|
+
|
|
509
|
+
## SameSite Cookies
|
|
510
|
+
|
|
511
|
+
Supported.
|
|
512
|
+
|
|
513
|
+
This change makes it possible for servers, and supporting clients, to mitigate certain types of CSRF attacks by disallowing `SameSite` cookies from being sent cross-origin.
|
|
514
|
+
|
|
515
|
+
On the Cookie object itself, you can get/set the `.sameSite` attribute, which will be serialized into the `SameSite=` cookie attribute. When unset or `undefined`, no `SameSite=` attribute will be serialized. The valid values of this attribute are `'none'`, `'lax'`, or `'strict'`. Other values will be serialized as-is.
|
|
516
|
+
|
|
517
|
+
When parsing cookies with a `SameSite` cookie attribute, values other than `'lax'` or `'strict'` are parsed as `'none'`. For example, `SomeCookie=SomeValue; SameSite=garbage` will parse so that `cookie.sameSite === 'none'`.
|
|
518
|
+
|
|
519
|
+
In order to support SameSite cookies, you must provide a `sameSiteContext` option to _both_ `setCookie` and `getCookies`. Valid values for this option are just like for the Cookie object, but have particular meanings:
|
|
520
|
+
1. `'strict'` mode - If the request is on the same "site for cookies" (see the RFC draft for what this means), pass this option to add a layer of defense against CSRF.
|
|
521
|
+
2. `'lax'` mode - If the request is from another site, _but_ is directly because of navigation by the user, e.g., `<link type=prefetch>` or `<a href="...">`, pass `sameSiteContext: 'lax'`.
|
|
522
|
+
3. `'none'` - Otherwise, pass `sameSiteContext: 'none'` (this indicates a cross-origin request).
|
|
523
|
+
4. unset/`undefined` - SameSite **will not** be enforced! This can be a valid use-case for when CSRF isn't in the threat model of the system being built.
|
|
524
|
+
|
|
525
|
+
It is highly recommended that you read RFC 6265bis for fine details on SameSite cookies. In particular [Section 8.8](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-02#section-8.8) discusses security considerations and defense in depth.
|
|
526
|
+
|
|
527
|
+
## Cookie Prefixes
|
|
528
|
+
|
|
529
|
+
Supported.
|
|
530
|
+
|
|
531
|
+
Cookie prefixes are a way to indicate that a given cookie was set with a set of attributes simply by inspecting the first few characters of the cookie's name.
|
|
532
|
+
|
|
533
|
+
Cookie prefixes are defined in [Section 4.1.3 of 6265bis](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-03#section-4.1.3). Two prefixes are defined:
|
|
534
|
+
|
|
535
|
+
1. `"__Secure-" Prefix`: If a cookie's name begins with a case-sensitive match for the string "__Secure-", then the cookie will have been set with a "Secure" attribute.
|
|
536
|
+
2. `"__Host-" Prefix`: If a cookie's name begins with a case-sensitive match for the string "__Host-", then the cookie will have been set with a "Secure" attribute, a "Path" attribute with a value of "/", and no "Domain" attribute.
|
|
537
|
+
|
|
538
|
+
If `prefixSecurity` is enabled for `CookieJar`, then cookies that match the prefixes defined above but do not obey the attribute restrictions will not be added.
|
|
539
|
+
|
|
540
|
+
You can define this functionality by passing in `prefixSecurity` option to `CookieJar`. It can be one of 3 values:
|
|
541
|
+
|
|
542
|
+
1. `silent`: Enable cookie prefix checking but silently fail to add the cookie if conditions not met. Default.
|
|
543
|
+
2. `strict`: Enable cookie prefix checking and error out if conditions not met.
|
|
544
|
+
3. `unsafe-disabled`: Disable cookie prefix checking.
|
|
545
|
+
|
|
546
|
+
Note that if `ignoreError` is passed in as `true` then the error will be silent regardless of `prefixSecurity` option (assuming it's enabled).
|
|
547
|
+
|
|
548
|
+
|
|
474
549
|
# Copyright and License
|
|
475
550
|
|
|
476
|
-
|
|
551
|
+
BSD-3-Clause:
|
|
477
552
|
|
|
478
553
|
```text
|
|
479
554
|
Copyright (c) 2015, Salesforce.com, Inc.
|