rhythmjs

Search documentation

Search guides, the tutorial and every package.

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.

Shell
bun add @rhythmjs/security @rhythmjs/rhythm @rhythmjs/router

All 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(), then cors(). secureHeaders calls next() first and sets its headers on the way back out, so it covers every response. cors must be on the app so preflight requests are answered even for paths that only have a GET route.
  • Router level, in this order: csrf(), then rateLimit(), 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.