Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
http-cookie-manager
Advanced tools
Highly tested. Parses cookies, then stores them. Provides formatting for response header.
This npm module parses a cookie string from either a request or a plain string
and provides formatting for setting the Set-Cookie
headers.
npm install http-cookie-manager
In order to start using http-cookie, we first need to parse a cookie string,
thus retrieving a CookieManager
, from which we can add more cookies,
modify cookies and format the Set-Cookie
headers:
const parser = require('http-cookie-manager')
let manager
// Parse from an http.IncomingMessage
manager = parser.parseWith(request)
// Parse from a string
manager = parser.parseFrom('number=five;sheathe=dagger')
Both parseWith
and parseFrom
returns a CookieManager
containing all
cookies from the cookie string that was parsed. If the cookie string is empty
(or in the request's case not defined at all), the CookieManager
will contain
no cookies.
Adding a cookie uses the setCookieBy
and setCookie
methods on the
CookieManager
accordingly:
let cookie = manager.setCookieBy('name', 'value')
let cookie = manager.setCookie(new Cookie('name', 'value'))
Both methods return the newly added cookie for chaining.
Cookie
MethodsBy calling methods on a Cookie
, you are changing the resulting Set-Cookie
header. The methods are fully chainable, and will always return the Cookie
back for further calls.
setExpires (date)
The expiration of the cookie. Whatsoever the timezone you might create the
cookie with, it will be converted to GMT+0 to conform with the http cookie
standards. Enter null
to toggle off.
cookie.setExpires(new Date())
setMaxAge (number)
Sets the max age of the cookie. The number
is the number of seconds the cookie
will exist until deleted. Enter null
to toggle off.
cookie.setMaxAge(200)
setDomain (domain)
The domain for the cookie to reside on. Enter null
to toggle off.
cookie.setDomain('example.com')
setPath (path)
The path for the cookie to reside on. Enter null
to toggle off.
cookie.setPath('/')
setSecure (state)
Whether to send as secure or not. Enter false
to toggle off.
cookie.setSecure(true)
setHttpOnly (state)
Whether to send as HttpOnly or not. Enter false
to toggle off.
cookie.setHttpOnly(true)
setSameSite (sameSite)
One of two strings: strict
or lax
. Set to null
to toggle off.
// Not case sensitive (case insensitive)
cookie.setSameSite('laX')
cookie.setSameSite('sTriCt')
You can access a cookie by sending in its name:
let cookie = manager.getCookieBy('name')
If the cookie does not exist, null
will be returned instead.
According to the HTTP specification, cookies cannot be deleted from the server.
There is however a way around this. By utilizing the setMaxAge
, you can make
it reach that max age directly:
manager.getCookieBy('cookieToDelete').setMaxAge(0)
Set-Cookie
HeaderYou can send the modified cookies back to the client by utilizing the
setHeaders (response)
method available on the CookieManager
. This will set
the Set-Cookie
header to include all the modified cookies.
manager.setHeaders(response)
If you would rather retrieve the array that contains all the correctly formatted
Set-Cookie
header strings, you can call createHeaders
.
manager.createHeaders()
const parser = require('http-cookie-manager')
// Let us assume these are actually set
let request
let response
let manager = parser.parseFrom(request)
if (manager.getCookieBy('_sessid') === null)
manager.setCookieBy('_sessid', 'totally-random-string').setHttpOnly(true)
manager.setHeaders(response)
// The new cookie (if added) will now be sent back to the client
response.end()
FAQs
Highly tested. Parses cookies, then stores them. Provides formatting for response header.
The npm package http-cookie-manager receives a total of 2 weekly downloads. As such, http-cookie-manager popularity was classified as not popular.
We found that http-cookie-manager demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.