Installations
npm install cookie
Score
99.4
Supply Chain
99.5
Quality
89.5
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Developer
jshttp
Developer Guide
Module System
CommonJS
Min. Node Version
>=18
Typescript Support
Yes
Node Version
22.9.0
NPM Version
10.8.3
Statistics
1,363 Stars
360 Commits
159 Forks
31 Watching
5 Branches
57 Contributors
Updated on 21 Nov 2024
Bundle Size
2.59 kB
Minified
1.10 kB
Minified + Gzipped
Languages
TypeScript (91.7%)
JavaScript (8.3%)
Total Downloads
Cumulative downloads
Total Downloads
9,846,529,506
Last day
-5.5%
10,724,095
Compared to previous day
Last week
3%
62,312,176
Compared to previous week
Last month
9.1%
258,340,396
Compared to previous month
Last year
11%
2,566,299,772
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
5
cookie
Basic HTTP cookie parser and serializer for HTTP servers.
Installation
1$ npm install cookie
API
1const cookie = require("cookie"); 2// import * as cookie from 'cookie';
cookie.parse(str, options)
Parse a HTTP Cookie
header string and returning an object of all cookie name-value pairs.
The str
argument is the string representing a Cookie
header value and options
is an
optional object containing additional parsing options.
1const cookies = cookie.parse("foo=bar; equation=E%3Dmc%5E2"); 2// { foo: 'bar', equation: 'E=mc^2' }
Options
cookie.parse
accepts these properties in the options object.
decode
Specifies a function that will be used to decode a cookie-value. Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to decode a previously-encoded cookie value into a JavaScript string.
The default function is the global decodeURIComponent
, wrapped in a try..catch
. If an error
is thrown it will return the cookie's original value. If you provide your own encode/decode
scheme you must ensure errors are appropriately handled.
cookie.serialize(name, value, options)
Serialize a cookie name-value pair into a Set-Cookie
header string. The name
argument is the
name for the cookie, the value
argument is the value to set the cookie to, and the options
argument is an optional object containing additional serialization options.
1const setCookie = cookie.serialize("foo", "bar"); 2// foo=bar
Options
cookie.serialize
accepts these properties in the options object.
encode
Specifies a function that will be used to encode a cookie-value.
Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode
a value into a string suited for a cookie's value, and should mirror decode
when parsing.
The default function is the global encodeURIComponent
.
maxAge
Specifies the number
(in seconds) to be the value for the Max-Age
Set-Cookie
attribute.
The cookie storage model specification states that if both expires
and
maxAge
are set, then maxAge
takes precedence, but it is possible not all clients by obey this,
so if both are set, they should point to the same date and time.
expires
Specifies the Date
object to be the value for the Expires
Set-Cookie
attribute.
When no expiration is set clients consider this a "non-persistent cookie" and delete it the current session is over.
The cookie storage model specification states that if both expires
and
maxAge
are set, then maxAge
takes precedence, but it is possible not all clients by obey this,
so if both are set, they should point to the same date and time.
domain
Specifies the value for the Domain
Set-Cookie
attribute.
When no domain is set clients consider the cookie to apply to the current domain only.
path
Specifies the value for the Path
Set-Cookie
attribute.
When no path is set, the path is considered the "default path".
httpOnly
Enables the HttpOnly
Set-Cookie
attribute.
When enabled, clients will not allow client-side JavaScript to see the cookie in document.cookie
.
secure
Enables the Secure
Set-Cookie
attribute.
When enabled, clients will only send the cookie back if the browser has a HTTPS connection.
partitioned
Enables the Partitioned
Set-Cookie
attribute.
When enabled, clients will only send the cookie back when the current domain and top-level domain matches.
This is an attribute that has not yet been fully standardized, and may change in the future. This also means clients may ignore this attribute until they understand it. More information about can be found in the proposal.
priority
Specifies the value for the Priority
Set-Cookie
attribute.
'low'
will set thePriority
attribute toLow
.'medium'
will set thePriority
attribute toMedium
, the default priority when not set.'high'
will set thePriority
attribute toHigh
.
More information about priority levels can be found in the specification.
sameSite
Specifies the value for the SameSite
Set-Cookie
attribute.
true
will set theSameSite
attribute toStrict
for strict same site enforcement.'lax'
will set theSameSite
attribute toLax
for lax same site enforcement.'none'
will set theSameSite
attribute toNone
for an explicit cross-site cookie.'strict'
will set theSameSite
attribute toStrict
for strict same site enforcement.
More information about enforcement levels can be found in the specification.
Example
The following example uses this module in conjunction with the Node.js core HTTP server to prompt a user for their name and display it back on future visits.
1var cookie = require("cookie"); 2var escapeHtml = require("escape-html"); 3var http = require("http"); 4var url = require("url"); 5 6function onRequest(req, res) { 7 // Parse the query string 8 var query = url.parse(req.url, true, true).query; 9 10 if (query && query.name) { 11 // Set a new cookie with the name 12 res.setHeader( 13 "Set-Cookie", 14 cookie.serialize("name", String(query.name), { 15 httpOnly: true, 16 maxAge: 60 * 60 * 24 * 7, // 1 week 17 }), 18 ); 19 20 // Redirect back after setting cookie 21 res.statusCode = 302; 22 res.setHeader("Location", req.headers.referer || "/"); 23 res.end(); 24 return; 25 } 26 27 // Parse the cookies on the request 28 var cookies = cookie.parse(req.headers.cookie || ""); 29 30 // Get the visitor name set in the cookie 31 var name = cookies.name; 32 33 res.setHeader("Content-Type", "text/html; charset=UTF-8"); 34 35 if (name) { 36 res.write("<p>Welcome back, <b>" + escapeHtml(name) + "</b>!</p>"); 37 } else { 38 res.write("<p>Hello, new visitor!</p>"); 39 } 40 41 res.write('<form method="GET">'); 42 res.write( 43 '<input placeholder="enter your name" name="name"> <input type="submit" value="Set Name">', 44 ); 45 res.end("</form>"); 46} 47 48http.createServer(onRequest).listen(3000);
Testing
1npm test
Benchmark
1npm run bench
name hz min max mean p75 p99 p995 p999 rme samples
· simple 8,566,313.09 0.0000 0.3694 0.0001 0.0001 0.0002 0.0002 0.0003 ±0.64% 4283157 fastest
· decode 3,834,348.85 0.0001 0.2465 0.0003 0.0003 0.0003 0.0004 0.0006 ±0.38% 1917175
· unquote 8,315,355.96 0.0000 0.3824 0.0001 0.0001 0.0002 0.0002 0.0003 ±0.72% 4157880
· duplicates 1,944,765.97 0.0004 0.2959 0.0005 0.0005 0.0006 0.0006 0.0008 ±0.24% 972384
· 10 cookies 675,345.67 0.0012 0.4328 0.0015 0.0015 0.0019 0.0020 0.0058 ±0.75% 337673
· 100 cookies 61,040.71 0.0152 0.4092 0.0164 0.0160 0.0196 0.0228 0.2260 ±0.71% 30521 slowest
✓ parse top-sites (15) 22945ms
name hz min max mean p75 p99 p995 p999 rme samples
· parse accounts.google.com 7,164,349.17 0.0000 0.0929 0.0001 0.0002 0.0002 0.0002 0.0003 ±0.09% 3582184
· parse apple.com 7,817,686.84 0.0000 0.6048 0.0001 0.0001 0.0002 0.0002 0.0003 ±1.05% 3908844
· parse cloudflare.com 7,189,841.70 0.0000 0.0390 0.0001 0.0002 0.0002 0.0002 0.0003 ±0.06% 3594921
· parse docs.google.com 7,051,765.61 0.0000 0.0296 0.0001 0.0002 0.0002 0.0002 0.0003 ±0.06% 3525883
· parse drive.google.com 7,349,104.77 0.0000 0.0368 0.0001 0.0001 0.0002 0.0002 0.0003 ±0.05% 3674553
· parse en.wikipedia.org 1,929,909.49 0.0004 0.3598 0.0005 0.0005 0.0007 0.0007 0.0012 ±0.16% 964955
· parse linkedin.com 2,225,658.01 0.0003 0.0595 0.0004 0.0005 0.0005 0.0005 0.0006 ±0.06% 1112830
· parse maps.google.com 4,423,511.68 0.0001 0.0942 0.0002 0.0003 0.0003 0.0003 0.0005 ±0.08% 2211756
· parse microsoft.com 3,387,601.88 0.0002 0.0725 0.0003 0.0003 0.0004 0.0004 0.0005 ±0.09% 1693801
· parse play.google.com 7,375,980.86 0.0000 0.1994 0.0001 0.0001 0.0002 0.0002 0.0003 ±0.12% 3687991
· parse support.google.com 4,912,267.94 0.0001 2.8958 0.0002 0.0002 0.0003 0.0003 0.0005 ±1.28% 2456134
· parse www.google.com 3,443,035.87 0.0002 0.2783 0.0003 0.0003 0.0004 0.0004 0.0007 ±0.51% 1721518
· parse youtu.be 1,910,492.87 0.0004 0.3490 0.0005 0.0005 0.0007 0.0007 0.0011 ±0.46% 955247
· parse youtube.com 1,895,082.62 0.0004 0.7454 0.0005 0.0005 0.0006 0.0007 0.0013 ±0.64% 947542 slowest
· parse example.com 21,582,835.27 0.0000 0.1095 0.0000 0.0000 0.0001 0.0001 0.0001 ±0.13% 10791418
References
License
Stable Version
The latest stable version of the package.
Stable Version
1.0.2
LOW
1
0/10
Summary
cookie accepts cookie name, path, and domain with out of bounds characters
Affected Versions
< 0.7.0
Patched Versions
0.7.0
Reason
no binaries found in the repo
Reason
30 commit(s) and 21 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/ci.yml:12
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/jshttp/cookie/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/jshttp/cookie/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/jshttp/cookie/ci.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
Found 9/30 approved changesets -- score normalized to 3
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 19 are checked with a SAST tool
Score
6.9
/10
Last Scanned on 2024-11-18
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 More