Installations
npm install @mgcrea/fastify-session
Developer Guide
Typescript
Yes
Module System
ESM
Min. Node Version
>=14
Node Version
20.18.0
NPM Version
10.8.2
Score
77.9
Supply Chain
99.5
Quality
78.5
Maintenance
50
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (98.41%)
JavaScript (1.59%)
Developer
mgcrea
Download Statistics
Total Downloads
263,357
Last Day
466
Last Week
1,909
Last Month
9,328
Last Year
86,408
GitHub Statistics
34 Stars
136 Commits
9 Forks
3 Watching
2 Branches
5 Contributors
Bundle Size
8.18 kB
Minified
3.28 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.4.1
Package Id
@mgcrea/fastify-session@2.4.1
Unpacked Size
112.67 kB
Size
17.92 kB
File Count
9
NPM Version
10.8.2
Node Version
20.18.0
Publised On
21 Oct 2024
Total Downloads
Cumulative downloads
Total Downloads
263,357
Last day
18.6%
466
Compared to previous day
Last week
-31.8%
1,909
Compared to previous week
Last month
20.3%
9,328
Compared to previous month
Last year
0.2%
86,408
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Dev Dependencies
22
FastifySession
Features
Session plugin for fastify that supports both stateless and sateful sessions.
-
Requires @fastify/cookie to handle cookies.
-
Works with fastify
>=4.0.0
. -
Can leverage crypto addons like @mgcrea/fastify-session-sodium-crypto to perform crypto.
-
Can leverage store addons like:
-
Built with TypeScript for static type checking with exported types along the library.
Install
1npm install @mgcrea/fastify-session @fastify/cookie 2# or 3pnpm add @mgcrea/fastify-session @fastify/cookie
Quickstart
Basic example (signed session with hmac stored in a volatile in-memory store)
Defaults to a volatile in-memory store for sessions (great for tests), with hmac for signature.
1import createFastify, { FastifyInstance, FastifyServerOptions } from "fastify"; 2import fastifyCookie from "@fastify/cookie"; 3import fastifySession from "@mgcrea/fastify-session"; 4 5const SESSION_SECRET = "a secret with minimum length of 32 characters"; 6const SESSION_TTL = 86400; // 1 day in seconds 7 8export const buildFastify = (options?: FastifyServerOptions): FastifyInstance => { 9 const fastify = createFastify(options); 10 11 fastify.register(fastifyCookie); 12 fastify.register(fastifySession, { 13 secret: SESSION_SECRET, 14 cookie: { maxAge: SESSION_TTL }, 15 }); 16 17 return fastify; 18};
Production example (signed session with sodium stored in redis)
For better performance/security, you can use the @mgcrea/fastify-session-sodium-crypto addon:
Leveraging an external redis store, the session id (generated with nanoid) is signed using a secret-key with libsodium's crytpo_auth
1import createFastify, { FastifyInstance, FastifyServerOptions } from 'fastify'; 2import fastifyCookie from 'fastify-cookie'; 3import fastifySession from '@mgcrea/fastify-session'; 4import { SODIUM_AUTH } from '@mgcrea/fastify-session-sodium-crypto'; 5 6const SESSION_KEY = 'Egb/g4RUumlD2YhWYfeDlm5MddajSjGEBhm0OW+yo9s=''; 7const SESSION_TTL = 86400; // 1 day in seconds 8const REDIS_URI = process.env.REDIS_URI || 'redis://localhost:6379/1'; 9 10export const buildFastify = (options?: FastifyServerOptions): FastifyInstance => { 11 const fastify = createFastify(options); 12 13 fastify.register(fastifyCookie); 14 fastify.register(fastifySession, { 15 key: Buffer.from(SESSION_KEY, 'base64'), 16 crypto: SODIUM_AUTH, 17 store: new RedisStore({ client: new Redis(REDIS_URI), ttl: SESSION_TTL }), 18 cookie: { maxAge: SESSION_TTL }, 19 }); 20 21 return fastify; 22};
Stateless example (encrypted session with sodium not using a store)
No external store required, the entire session data is encrypted using a secret-key with libsodium's crypto_secretbox_easy
Here we used a secret
instead of providing a key
, key derivation will happen automatically on startup.
1import createFastify, { FastifyInstance, FastifyServerOptions } from "fastify"; 2import fastifyCookie from "fastify-cookie"; 3import fastifySession from "@mgcrea/fastify-session"; 4import { SODIUM_SECRETBOX } from "@mgcrea/fastify-session-sodium-crypto"; 5 6const SESSION_TTL = 86400; // 1 day in seconds 7 8export const buildFastify = (options?: FastifyServerOptions): FastifyInstance => { 9 const fastify = createFastify(options); 10 11 fastify.register(fastifyCookie); 12 fastify.register(fastifySession, { 13 secret: "a secret with minimum length of 32 characters", 14 crypto: SODIUM_SECRETBOX, 15 cookie: { maxAge: SESSION_TTL }, 16 }); 17 18 return fastify; 19};
Benchmarks
Session crypto sealing
1node --experimental-specifier-resolution=node --loader=ts-paths-esm-loader/transpile-only --no-warnings test/benchmark/cryptoSeal.ts
1SODIUM_SECRETBOX#sealJson x 333,747 ops/sec ±0.62% (91 runs sampled) 2SODIUM_AUTH#sealJson x 376,300 ops/sec ±0.50% (89 runs sampled) 3HMAC#sealJson x 264,292 ops/sec ±3.13% (85 runs sampled) 4Fastest is SODIUM_AUTH#sealJson
Session crypto unsealing
1node --experimental-specifier-resolution=node --loader=ts-paths-esm-loader/transpile-only --no-warnings test/benchmark/cryptoUnseal.ts
1SODIUM_SECRETBOX#unsealJson x 424,297 ops/sec ±0.69% (86 runs sampled) 2SODIUM_AUTH#unsealJson x 314,736 ops/sec ±0.96% (89 runs sampled) 3HMAC#unsealJson x 145,037 ops/sec ±5.67% (78 runs sampled) 4Fastest is SODIUM_SECRETBOX#unsealJson
Authors
Credits
Heavily inspired from
- fastify-secure-session by Matteo Collina
- fastify-session by Denis Fäcke
- express-session by Douglas Wilson
- cookie-signature by TJ Holowaychuk
License
1The MIT License 2 3Copyright (c) 2020 Olivier Louvignes <olivier@mgcrea.io> 4 5Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 6documentation files (the "Software"), to deal in the Software without restriction, including without limitation the 7rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit 8persons to whom the Software is furnished to do so, subject to the following conditions: 9 10The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 11Software. 12 13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 14WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 15COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 16OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: MIT License: LICENSE.md:0
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:46: update your workflow using https://app.stepsecurity.io/secureworkflow/mgcrea/fastify-session/main.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:48: update your workflow using https://app.stepsecurity.io/secureworkflow/mgcrea/fastify-session/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:52: update your workflow using https://app.stepsecurity.io/secureworkflow/mgcrea/fastify-session/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/mgcrea/fastify-session/main.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/mgcrea/fastify-session/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/mgcrea/fastify-session/main.yml/master?enable=pin
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 1 are checked with a SAST tool
Reason
10 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-7q7g-4xm8-89cq
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-8jhw-289h-jh2g
- Warn: Project is vulnerable to: GHSA-64vr-g452-qvp3
- Warn: Project is vulnerable to: GHSA-9cwx-2883-4wfx
- Warn: Project is vulnerable to: GHSA-vg6x-rcgg-rjx6
Score
2.5
/10
Last Scanned on 2025-01-27
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 MoreOther packages similar to @mgcrea/fastify-session
@mgcrea/fastify-session-sodium-crypto
Fast sodium-based crypto for fastify-session
@mgcrea/fastify-session-prisma-store
Prisma store for fastify-session
@mgcrea/fastify-session-redis-store
Redis store for fastify-session using ioredis
fastify-sessions
Session plugin for fastify written in TypeScript supporting both stateless and stateful sessions, fork of @mgcrea/fastify-session adding a lot of features while keeping compatibility.