salesforce/tough-cookie
salesforce/tough-cookie has 1,067 GitHub stars and 307 forks. GitHot measured 0 stars in 24 hours. Last checked 2026-11-09 11:17 UTC. Last push 2026-11-01.
RFC6265 Cookies and CookieJar for Node.js
Measured star growth
- in 24h
- 0
- in 7 days
- Update delayed
- in 15 days
- Update delayed
- in 1 month
- Update delayed
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
OriginalTough Cookie ·

A Node.js implementation of RFC6265 for cookie parsing, storage, and retrieval.
Getting Started
Install Tough Cookie using npm:
npm install tough-cookie
or yarn:
yarn add tough-cookie
Usage
import { Cookie, CookieJar } from 'tough-cookie'
// parse a `Cookie` request header
const reqCookies = 'ID=298zf09hf012fh2; csrf=u32t4o3tb3gg43; _gat=1'
.split(';')
.map((str) => Cookie.parse(str))
.filter((cookie) => cookie !== undefined)
// generate a `Cookie` request header
const cookieHeader = reqCookies
.map((cookie) => cookie.cookieString())
.join('; ')
// parse a Set-Cookie response header
const resCookie = Cookie.parse(
'foo=bar; Domain=example.com; Path=/; Max-Age=3600',
)
if (!resCookie) throw new Error('invalid Set-Cookie header')
// generate a Set-Cookie response header
const setCookieHeader = resCookie.toString()
// store and retrieve cookies
const cookieJar = new CookieJar() // uses the in-memory store by default
await cookieJar.setCookie(resCookie, 'https://example.com/')
const matchingCookies = await cookieJar.getCookies('https://example.com/')
[!IMPORTANT] For more detailed usage information, refer to the API docs.
RFC6265bis
Support for RFC6265bis is being developed. As these revisions to RFC6265 are
still in Active Internet-Draft state, the areas of support that follow are subject to change.
SameSite Cookies
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.
Example
import { CookieJar } from 'tough-cookie'
const cookieJar = new CookieJar() // uses the in-memory store by default
// storing cookies with various SameSite attributes
await cookieJar.setCookie(
'strict=authorized; SameSite=strict',
'http://example.com/index.html',
)
await cookieJar.setCookie(
'lax=okay; SameSite=lax',
'http://example.com/index.html',
)
await cookieJar.setCookie('normal=whatever', 'http://example.com/index.html')
// retrieving cookies using a SameSite context
const laxCookies = await cookieJar.getCookies('http://example.com/index.html', {
// the first cookie (strict=authorized) will not be returned if the context is 'lax'
// but the other two cookies will be returned
sameSiteContext: 'lax',
})
[!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
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
ignoreErroris passed in astruewhen setting a cookie then the error is silent regardless of theprefixSecurityoption (assuming it's enabled).
Example
import { CookieJar, MemoryCookieStore } from 'tough-cookie'
const cookieJar = new CookieJar(new MemoryCookieStore(), {
prefixSecurity: 'silent',
})
// ignoreError silences the general Secure-cookie origin rejection
await cookieJar.setCookie(
'__Secure-SID=12345; Domain=example.com; Secure;',
'http://example.com',
{ ignoreError: true },
)
// this cookie will be stored since the url is secure (https)
await cookieJar.setCookie(
'__Secure-SID=12345; Domain=example.com; Secure;',
'https://example.com',
)
[!NOTE] It is highly recommended that you read RFC6265bis - Section 4.1.3 for more details on Cookie Prefixes.
Potentially Trustworthy Origins are considered "Secure"
The definition of a "Secure" connection is not explicitly defined by RFC6265bis but the following text is provided in RFC6265bis - Section 5.8.3:
[!NOTE] Typically, user agents consider a connection secure if the connection makes use of transport-layer security, such as SSL or TLS, or if the host is trusted. For example, most user agents consider "https" to be a scheme that denotes a secure protocol and "localhost" to be trusted host.
As well as a note to Appendix A. Changes from RFC6265 which refers to "potentially trustworthy origins" which are defined in the Secure Contexts - W3C Candidate Recommendation Draft:
[!Note] Considers potentially trustworthy origins as "secure".
Since most web browsers treat localhost as a trustworthy origin, by default, so does tough-cookie. To disable this
behavior, the CookieJar must be configured with:
import { CookieJar, MemoryCookieStore } from 'tough-cookie'
const cookieJar = new CookieJar(new MemoryCookieStore(), {
// add configuration so localhost will not be considered trustworthy
// (fyi - this doesn't apply to https cookies on localhost as those use a secure protocol)
allowSecureOnLocal: false,
})
// this cookie will be persisted to storage
await cookieJar.setCookie(
'SID=12345; Domain=localhost; Secure;',
'https://localhost',
)
// but, on retrieval, it will not be returned
const cookies = await cookieJar.getCookies('http://localhost')
Secure cookies set on an untrustworthy origin are rejected, including
unprefixed cookies. ignoreError: true silently discards them; it does not
store them. This origin check applies independently of prefixSecurity.
To restore the previous behavior of storing Secure cookies regardless of the
origin they were set from, the CookieJar must be configured with the following.
This only affects storage; Secure cookies are still only returned for secure
origins on retrieval.
import { CookieJar, MemoryCookieStore } from 'tough-cookie'
const cookieJar = new CookieJar(new MemoryCookieStore(), {
enforceSecureOriginOnStorage: false,
})
// this cookie will be persisted to storage
await cookieJar.setCookie('SID=12345; Secure;', 'http://example.com')
// but, on retrieval, it will only be returned for secure origins
const cookies = await cookieJar.getCookies('https://example.com')
Node.js Version Support
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.
Explore related projects
codecrafters-io/build-your-own-x
Master programming by recreating your favorite technologies from scratch.
561,398 starspublic-apis/public-apis
A collective list of free APIs
497,679 starsmorluto/rea
Reverse engineer anything with agents, from app behavior down to native binaries.
447,495 starsopenclaw/openclaw
The AI that really does things. Any OS. Any Platform. The lobster way. 🦞
394,261 stars