Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@bicycle-codes/session-cookie
Advanced tools
Create signed cookies with an HMAC key, and verify them.
This will stringify a JSON object in a stable format, then use an HMAC key to create a signature. The final cookie value includes a token that is the signature concatenated with the JSON you passed in, all base64 encoded.
This conveniently includes a command to generate keys as well.
Parsing the cookie will return the cookie as a plain object, plus a "session token" -- the base64 encoded HMAC signature + session data.
Parsing a session token will return the object that you passed in when creating the token, useful for embedding an ID, or any data you want to be certain has not been changed.
Verify the signature with verifySessionString
.
npm i -S @bicycle-codes/session-cookie
These functions should all be run in a server.
Create a string suitable for use as a cookie. Sign the given data with a secret key, and stringify the signature + JSON data as a base64
string.
[!NOTE]
This will add default values for additional cookie attributes.session=123; Max-Age=604800; Path=/; HttpOnly; Secure; SameSite=Lax
These environment variables can be used to set the cookie attributes:
SESSION_COOKIE_HTTPONLY
SESSION_COOKIE_SECURE
SESSION_COOKIE_SAMESITE
SESSION_COOKIE_MAX_AGE_SPAN
SESSION_COOKIE_DOMAIN
SESSION_COOKIE_PATH
import { createCookie } from '@bicycle-codes/session-cookie'
const cookie = createCookie({ hello: 'world' }, SECRET_KEY)
console.log(cookie)
// => session=vTAHUs4nBS65UPy4AdnIMVdh-5MeyJoZWxsbyI6IndvcmxkIn0; Max-Age=604800; Path=/; HttpOnly; Secure; SameSite=Lax
createCookie (sessionData, secretKey, name?, env?)
async function createCookie (
sessionData:Record<string, string>,
secretKey:string,
name?:string,
env?:CookieEnv,
):Promise<string>
Create or patch a Headers
instance.
import { setCookie } from '@bicycle-codes/session-cookie'
const headers = setCookie(cookie)
setCookie(cookie, headers?:Headers)
function setCookie (
cookie:string,
_headers?:Headers,
):Headers
Parse a cookie string into a plain object.
import { parseCookie } from '@bicycle-codes/session-cookie'
const parsed = parseCookie('session=vTAHUs4nBS65UPy4AdnIMVdh-5MeyJoZWxsbyI6IndvcmxkIn0; Max-Age=604800; Path=/; HttpOnly; Secure; SameSite=Lax')
// =>
// {
// session: 'vTAHUs4nBS65UPy4AdnIMVdh-5MeyJoZWxsbyI6IndvcmxkIn0',
// 'Max-Age': '604800',
// Path: '/',
// HttpOnly: true,
// Secure: true,
// SameSite: 'Lax'
// }
Parse a session token. This will return whatever data was used to create the token.
import { parseSession } from '@bicycle-codes/session-cookie'
const session = parseSession(parsed.session as string)
// => { hello: 'world' }
Verify the given session token. This checks that an embedded signature is correct for the associated data.
import {
verifySessionString,
parseCookie
} from '@bicycle-codes/session-cookie'
// ... get headers somehow ...
const cookies = headers.getSetCookie()
const cookie = parseCookie(cookies[0])
const isOk = await verifySessionString(cookie.session, SECRET_KEY)
// => true
verifySessionString(session, key)
async function verifySessionString (
session:string,
key:string
):Promise<boolean>
Do this serverside. Patch the given headers, removing the cookie.
function rmCookie (headers:Headers, name?:string):void
This exposes ESM and common JS via package.json exports
field.
import '@bicycle-codes/session-cookie'
require('@bicycle-codes/session-cookie')
Session cookies are signed using HMAC SHA256, which requires using a secret key of at least 32 bytes of length.
This package conveniently includes a command line tool to generate keys, exposed as cookiekey
. After installing this as a dependency, use it like this:
$ npx cookiekey
BGSzELbpBuESqmKyhtw/9zD7sHIy2hf/kSK0y0U0L60=
Save the secret key as part of your server environment. This depends on always using the same secret key.
This works anywhere that supports the Web Crypto API. Has been verified to work in Cloudflare and Node.
FAQs
Cookies
The npm package @bicycle-codes/session-cookie receives a total of 5 weekly downloads. As such, @bicycle-codes/session-cookie popularity was classified as not popular.
We found that @bicycle-codes/session-cookie demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.