feat(server): session/auth/CSRF/guard primitives + graduate toward Stable (#131, #132) - #153
Merged
Merged
Conversation
…te toward Stable (#131, #132) Collected work to land the two open `server` tickets in one branch. #132 — session, auth, and middleware primitives: - session() + memoryStore(): HMAC-signed session-id cookie (Web Crypto, cross-runtime), payload in a pluggable SessionStore (in-memory default, bring-your-own without bundling a client). `ctx.session` is a Proxy — data via plain props, lifecycle via $id/$isNew/$data/$regenerate/$destroy/ $clear. Secret rotation, rolling sessions, prototype-pollution filtering, session-fixation defense via $regenerate. - csrf() + csrfToken(): OWASP double-submit cookie (signed when a secret is given); token via x-csrf-token header or _csrf body field; composes with the security module. - guard(): predicate route guard mirroring the router's guard ergonomics. - basicAuth()/bearerAuth(): Authorization parsing with a verify() hook, resolved user on ctx.state. - crypto utilities: signValue/unsignValue/timingSafeEqual/randomToken/ randomId/base64Url* built on globalThis.crypto.subtle (no node:crypto). - Shared cookie helpers extracted to src/server/cookies.ts (+ appendSetCookie). #131 — promote `server` toward Stable: - Stability section in the guide: exit criteria, frozen ctx/app surface, per-runtime support matrix; intro + README notes; version history. - app.listen() now supports Deno via Deno.serve. - ctx.session is additive/optional — no breaking changes to the 1.14 surface. - New exports wired through server/index.ts and the /full bundle. Tests: tests/server-stable.test.ts (sessions, CSRF, guards, auth, crypto; secret rotation, rolling, $destroy-revive, immutable-response cookies) plus server coverage in the cross-runtime smoke. Full suite (2785), tsc, eslint, build, full-bundle/doc-export checks, and cross-runtime (Node/Bun) pass. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Collected work for the two open
servertickets, landed in one branch and targetingdev.Closes #131. Closes #132.
#132 — session, auth, and middleware primitives
session()+memoryStore()— HMAC-signed session-id cookie (Web Crypto, cross-runtime), payload in a pluggableSessionStore(in-memory default; bring-your-own Redis/DB without bundling a client).ctx.sessionis a Proxy: payload via plain props, lifecycle via$id/$isNew/$data/$regenerate/$destroy/$clear. Includes secret rotation, rolling sessions, prototype-pollution filtering, and session-fixation defense.csrf()+csrfToken()— OWASP double-submit cookie (signed when asecretis supplied); token viax-csrf-tokenheader or_csrfbody field. Composes with thesecuritymodule (integrity vs. output).guard()— predicate route guard mirroring the router's guard ergonomics.basicAuth()/bearerAuth()—Authorizationparsing with averify()hook; resolved user onctx.state.signValue/unsignValue/timingSafeEqual/randomToken/randomId/base64Url*, all onglobalThis.crypto.subtle(nonode:crypto).src/server/cookies.ts(+appendSetCookie); no behavioral change to existingctx.setCookie.#131 — promote
servertoward Stablectx/appsurface, per-runtime support matrix; intro + README notes; version history.app.listen()now supports Deno viaDeno.serve(Node/Bun/Deno covered).ctx.sessionis additive/optional — no breaking changes to the 1.14 surface.server/index.tsand the/fullbundle.Zero-dependency & secure-by-default
No runtime dependencies added; each primitive is independently importable/tree-shakeable. Sessions default to
httpOnly+SameSite=Lax; signing is HMAC-SHA-256; comparisons are constant-time.Verification
tests/server-stable.test.ts: sessions, CSRF, guards, auth, crypto — incl. secret rotation, rolling,$destroy-revive, immutable-response cookies).tsc,eslint,bun run build,check:full-bundle(in sync),check:doc-exports(server 23/23) all green.Review
Ran an adversarial multi-agent review of the diff; applied the confirmed hardening: don't cache a rejected HMAC key-import, guard
timingSafeEqualagainst empty input, revive a session written-to after$destroy(), simplify the Denolisten()address resolution, and strengthened tests/docs.🤖 Generated with Claude Code