Security@rhythmjs/security
Overview
Four independent middleware modules for the edge of your app: CORS, CSRF, rate limiting, and secure headers.
On this page
Install#
@rhythmjs/security ships four independent middleware modules, each on its own subpath export; there is no root barrel: /cors, /csrf, /rate-limit, and /secure-headers. CORS & CSRF and rate limiting & secure headers have their own pages.
bun add @rhythmjs/security @rhythmjs/rhythm @rhythmjs/routerAll of it is ordinary middleware over the HTTP context from @rhythmjs/router/context. Edge concerns (secureHeaders, cors) go on the Rhythm app in front of mount(router); request-gating concerns (csrf, rateLimit) go on a router or on individual routes. The app is always new Rhythm().use(mount(router)) served with Bun.serve({ fetch: toFetchHandler(app) }).
Where each piece goes#
- App level, before
mount(router):secureHeaders(), thencors().secureHeaderscallsnext()first and sets its headers on the way back out, so it covers every response.corsmust be on the app so preflight requests are answered even for paths that only have aGETroute. - Router level, in this order:
csrf(), thenrateLimit(), then your own guards. Cheap rejections first, then anything that costs more.
Nothing is added to the context itself: these modules only set response headers or answer with an error.
Identity is yours#
The package does not resolve users or check roles. Authentication and authorization are ordinary middleware you write with derive and (ctx, next) guards; the tutorial builds both by hand. For sessions, OAuth, and requireSession(), use the better-auth integration.