Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
node-cookie
Advanced tools
Changelog
Readme
Easily parse and write signed & encrypted cookies on Node.js HTTP requests.
node-cookie
makes it simpler to create encrypted and signed cookies for HTTP requests.
You can use it with any framework or library of your choice.
const http = require('http')
const nodeCookie = require('node-cookie')
http.createServer(function (req, res) {
// this will update set-cookie header on res object.
nodeCookie.create(res, 'user', 'virk')
}).listen(3000)
const http = require('http')
const nodeCookie = require('node-cookie')
http.createServer(function (req, res) {
nodeCookie.create(res, 'user', 'virk', '16charlongsecret')
}).listen(3000)
const http = require('http')
const nodeCookie = require('node-cookie')
http.createServer(function (req, res) {
nodeCookie.create(res, 'user', 'virk', '16charlongsecret', true)
}).listen(3000)
Cookie parser is a simple utility module to read and write cookies on Node.js HTTP requests. It supports cookie signing and encryption.
Object
Mixed
String
String
void
void
Object
Parses cookies from HTTP header Cookie
into
a javascript object. Also it will unsign
and decrypt cookies encrypted and signed
by this library using a secret.
Kind: inner method of Cookie
Param | Type | Default |
---|---|---|
req | http.IncomingRequest | |
[secret] | String |
|
[decrypt] | Boolean | false |
Example
nodeCookie.parse(req)
// or if cookies were signed when writing
nodeCookie.parse(req, 'SECRET')
// also if cookies were encrypted
nodeCookie.parse(req, 'SECRET', true)
Mixed
Returns value for a single cookie by its key. It is
recommended to make use of this function when you
want to pull a single cookie. Since the parse
method will eagerly unsign and decrypt all the
cookies.
Kind: inner method of Cookie
Param | Type | Default | Description |
---|---|---|---|
req | http.IncomingRequest | ||
key | String | ||
[secret] | String |
| |
[decrypt] | Boolean | false | |
[cookies] | Object |
| Use existing cookies object over re-parsing them from the header. |
Example
nodeCookie.get(req, 'sessionId')
// if cookie was signed
nodeCookie.get(req, 'sessionId', 'SECRET')
// if cookie was encrypted
nodeCookie.get(req, 'sessionId', 'SECRET', true)
String
Unpack cookie value by unsigning and decrypting
it. Infact you can unpack any value packed via
the packValue
method.
Kind: inner method of Cookie
Param | Type |
---|---|
value | String |
secret | String |
decrypt | Boolean |
String
Pack the value by properly formatting, signing and encrypting it.
Kind: inner method of Cookie
Param | Type | Default |
---|---|---|
value | String | |
[secret] | String |
|
[encrypt] | Boolean | false |
void
Write cookie to the HTTP response object. It will append
duplicate cookies to the Set-Cookie
header, since
browsers discard the duplicate cookies by themselves
Kind: inner method of Cookie
Param | Type | Default |
---|---|---|
res | http.ServerResponse | |
key | String | |
value | * | |
[options] | Object | {} |
[secret] | String |
|
[encrypt] | Boolean | false |
Example
nodeCookie.create(res, 'sessionId', 1)
// sign session id
nodeCookie.create(res, 'sessionId', 1, {}, 'SECRET')
// sign and encrypt session id
nodeCookie.create(res, 'sessionId', 1, {}, 'SECRET', true)
void
Clears the cookie from browser by setting it's expiry in past. This is required since there is no other way to instruct the browser to delete a cookie.
Also this method will override the expires
value on
the options object.
Kind: inner method of Cookie
Param | Type | Default |
---|---|---|
res | http.ServerResponse | |
key | String | |
[options] | Object | {} |
Example
nodeCookie.clear(res, 'sessionId')
FAQs
sign, encrypt and parse http cookies
The npm package node-cookie receives a total of 4,989 weekly downloads. As such, node-cookie popularity was classified as popular.
We found that node-cookie demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.