Gathering detailed insights and metrics for @mgcrea/fastify-session
Gathering detailed insights and metrics for @mgcrea/fastify-session
Gathering detailed insights and metrics for @mgcrea/fastify-session
Gathering detailed insights and metrics for @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.
npm install @mgcrea/fastify-session
Typescript
Module System
Min. Node Version
Node Version
NPM Version
77.9
Supply Chain
99.5
Quality
78.5
Maintenance
50
Vulnerability
100
License
TypeScript (98.41%)
JavaScript (1.59%)
Total Downloads
263,357
Last Day
466
Last Week
1,909
Last Month
9,328
Last Year
86,408
34 Stars
136 Commits
9 Forks
3 Watching
2 Branches
5 Contributors
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
2
22
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.
1npm install @mgcrea/fastify-session @fastify/cookie 2# or 3pnpm add @mgcrea/fastify-session @fastify/cookie
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};
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};
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};
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
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
Heavily inspired from
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
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
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
10 existing vulnerabilities detected
Details
Score
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 More