Gathering detailed insights and metrics for tough-cookie
Gathering detailed insights and metrics for tough-cookie
Gathering detailed insights and metrics for tough-cookie
Gathering detailed insights and metrics for tough-cookie
RFC6265 Cookies and CookieJar for Node.js
npm install tough-cookie
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.5
Supply Chain
100
Quality
85.5
Maintenance
100
Vulnerability
100
License
TypeScript (99.47%)
JavaScript (0.38%)
Shell (0.12%)
EJS (0.03%)
Total Downloads
12,056,734,789
Last Day
3,034,895
Last Week
48,943,622
Last Month
213,265,979
Last Year
2,166,623,612
BSD-3-Clause License
1,007 Stars
675 Commits
282 Forks
49 Watchers
56 Branches
66 Contributors
Updated on Jun 26, 2025
Minified
Minified + Gzipped
Latest Version
5.1.2
Package Id
tough-cookie@5.1.2
Unpacked Size
222.39 kB
Size
48.76 kB
File Count
41
NPM Version
10.9.2
Node Version
22.14.0
Published on
Feb 28, 2025
Cumulative downloads
Total Downloads
A Node.js implementation of RFC6265 for cookie parsing, storage, and retrieval.
Install Tough Cookie using npm
:
1npm install tough-cookie
or yarn
:
1yarn add tough-cookie
1import { Cookie, CookieJar } from 'tough-cookie' 2 3// parse a `Cookie` request header 4const reqCookies = 'ID=298zf09hf012fh2; csrf=u32t4o3tb3gg43; _gat=1' 5 .split(';') 6 .map(Cookie.parse) 7// generate a `Cookie` request header 8const cookieHeader = reqCookies.map((cookie) => cookie.cookieString()).join(';') 9 10// parse a Set-Cookie response header 11const resCookie = Cookie.parse( 12 'foo=bar; Domain=example.com; Path=/; Expires=Tue, 21 Oct 2025 00:00:00 GMT', 13) 14// generate a Set-Cookie response header 15const setCookieHeader = cookie.toString() 16 17// store and retrieve cookies 18const cookieJar = new CookieJar() // uses the in-memory store by default 19await cookieJar.setCookie(resCookie, 'https://example.com/') 20const matchingCookies = await cookieJar.getCookies('https://example.com/')
[!IMPORTANT] For more detailed usage information, refer to the API docs.
Support for RFC6265bis is being developed. As these revisions to RFC6252 are
still in Active Internet-Draft
state, the areas of support that follow are subject to change.
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.
1import { CookieJar } from 'tough-cookie' 2 3const cookieJar = new CookieJar() // uses the in-memory store by default 4 5// storing cookies with various SameSite attributes 6await cookieJar.setCookie( 7 'strict=authorized; SameSite=strict', 8 'http://example.com/index.html', 9) 10await cookieJar.setCookie( 11 'lax=okay; SameSite=lax', 12 'http://example.com/index.html', 13) 14await cookieJar.setCookie('normal=whatever', 'http://example.com/index.html') 15 16// retrieving cookies using a SameSite context 17const laxCookies = await cookieJar.getCookies('http://example.com/index.html', { 18 // the first cookie (strict=authorized) will not be returned if the context is 'lax' 19 // but the other two cookies will be returned 20 sameSiteContext: 'lax', 21})
[!NOTE] It is highly recommended that you read RFC6265bis - Section 8.8 for more details on SameSite cookies, security considerations, and defense in depth.
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.
Two prefixes are defined:
"__Secure-"
If a cookie's name begins with a case-sensitive match for the string __Secure-
, then the cookie was set with a "Secure" attribute.
"__Host-"
If a cookie's name begins with a case-sensitive match for the string __Host-
, then the cookie was set with a "Secure" attribute, a "Path" attribute with a value of "/", and no "Domain" attribute.
If prefixSecurity
is enabled for CookieJar
, then cookies that match the prefixes defined above but do
not obey the attribute restrictions are not added.
You can define this functionality by passing in the prefixSecurity
option to CookieJar
. It can be one of 3 values:
silent
: (default) Enable cookie prefix checking but silently fail to add the cookie if conditions are not met.strict
: Enable cookie prefix checking and error out if conditions are not met.unsafe-disabled
: Disable cookie prefix checking.If
ignoreError
is passed in astrue
when setting a cookie then the error is silent regardless of theprefixSecurity
option (assuming it's enabled).
1import { CookieJar, MemoryCookieStore } from 'tough-cookie' 2 3const cookieJar = new CookieJar(new MemoryCookieStore(), { 4 prefixSecurity: 'silent', 5}) 6 7// this cookie will be silently ignored since the url is insecure (http) 8await cookieJar.setCookie( 9 '__Secure-SID=12345; Domain=example.com; Secure;', 10 'http://example.com', 11) 12 13// this cookie will be stored since the url is secure (https) 14await cookieJar.setCookie( 15 '__Secure-SID=12345; Domain=example.com; Secure;', 16 'https://example.com', 17)
[!NOTE] It is highly recommended that you read RFC6265bis - Section 4.1.3 for more details on Cookie Prefixes.
We follow the Node.js release schedule and support all versions that are in Active LTS or Maintenance. We will always do a major release when dropping support for older versions of node, and we will do so in consultation with our community.
7.5/10
Summary
Regular Expression Denial of Service in tough-cookie
Affected Versions
< 2.3.3
Patched Versions
2.3.3
6.5/10
Summary
tough-cookie Prototype Pollution vulnerability
Affected Versions
< 4.1.3
Patched Versions
4.1.3
5.3/10
Summary
ReDoS via long string of semicolons in tough-cookie
Affected Versions
< 2.3.0
Patched Versions
2.3.0
Reason
all changesets reviewed
Reason
no dangerous workflow patterns detected
Reason
13 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreLast Day
-5.5%
3,034,895
Compared to previous day
Last Week
-7.7%
48,943,622
Compared to previous week
Last Month
3.2%
213,265,979
Compared to previous month
Last Year
10%
2,166,623,612
Compared to previous year