
Security News
Django Joins curl in Pushing Back on AI Slop Security Reports
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
itty-session
Advanced tools
itty-session is a cookie-based session middleware for itty-router on Cloudflare Workers.
This is not an official library and is not affiliated with Kevin R. Whitley.
Cookie-based sessions are currently the most secure way to create sessions for your web app.
No, JWTs are not the solution.
Specifically, the defaults set for itty-session are httpOnly
, secure
, sameSite: strict
, which is the most secure according to OWASP's cheat sheets (see links above).
I'll elaborate later. Right now, let's get down to brass tacks.
Install itty-session via your favourite package manager. You know the drill.
npm install itty-session
pnpm install itty-session
yarn add itty-session
Itty-session requires a database to store session data.
Currently the only database supported is Cloudflare's D1.
By default, itty-session will use the SESSIONS
database and the sessions
table.
Expected wrangler.toml
example configuration (with a second database):
d1_databases = [
{ binding = "DB", database_name = "my-database", database_id = "ABCD-0123-4567-8901-ABCD-EFGH" },
{ binding = "SESSIONS", database_name = "my-sessions", database_id = "ABCD-0123-4567-8901-ABCD-EFGH" },
]
If you're using a different database, you can specify it via the dbName
option, but make sure it matches the binding name in your wrangler.toml
file.
A table named sessions
is also required and will not automatically be created. Make sure it matches the table name in your wrangler.toml
file.
Table configuration for sessions
:
CREATE TABLE "sessions" (
"sid" TEXT UNIQUE,
"data" TEXT,
"expiry" INTEGER
);
You can use Wrangler to create the table for you. Example:
npx wrangler d1 execute karaoke-sessions --local --file=./sessions.sql
(or--remote
to push to remote D1 instance)
The following example is for cloudflare workers using itty-router as a base.
import { AutoRouter, cors, withContent } from 'itty-router';
import { createSessionsMiddleware } from 'itty-session';
import D1Provider from 'itty-session/providers/d1';
const { sessionPreflight, sessionify } = createSessionsMiddleware({
logging: true,
Provider: D1Provider,
providerOptions: {
dbName: 'SESSIONS',
tableName: 'sessions',
}
});
const router = AutoRouter({
before: [sessionPreflight],
finally: [sessionify],
});
router.post('/login', withContent, async (request) => {
const { content } = request;
// placeholder for real auth, please don't do this!
if (!content?.username !== 'test' || !content?.password !== 'test') {
return {
success: false,
message: 'invalid credentials',
};
}
// creds are correct
request.session.username = 'test';
request.session.isLoggedIn = true;
return {
success: true,
message: 'logged in',
user: {
username: user.username,
isLoggedIn: true,
},
};
});
router.get('/logout', async (request) => {
request.session?.destroy();
return {
success: true,
message: 'logged out',
};
});
export default router;
As I continue developing this, a few items to take care of:
FAQs
Session management middleware for itty-router
The npm package itty-session receives a total of 2 weekly downloads. As such, itty-session popularity was classified as not popular.
We found that itty-session 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.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.