The web/session package provides server-side session management with session fixation protection and a pluggable store interface.

Import

Store interface

Implement Store against any backend (Redis, Postgres, DynamoDB). go-security-sql provides a production-ready store over Postgres/MySQL, or any SQL flavor via its pluggable Dialect — see go-security-sql. go-security-redis documents the contract for a Redis store — see go-security-redis.

MemoryStore

For development and tests:
MemoryStore starts a background GC goroutine on NewMemoryStore; call Close when the store is no longer needed to stop it. It is not safe for multi-instance deployments — use Redis in production.

Options

Session interface

Attributes() returns a snapshot copy of all attributes — it’s what FixationProtect uses to copy state into a freshly minted session. The session id is carried in an HTTP cookie via CookieConfig and a small set of helper functions:
WriteSessionCookie always forces HttpOnly: true regardless of cfg, so a misconfigured CookieConfig cannot ship a JS-readable session cookie.

CookieConfig fields

Wiring cookies through securityhttp

The session cookie used by SessionAuthentication, login, and the synchronizer CSRF repository is configured through securityhttp.WithSessionStore, which takes a session.CookieConfig directly (not functional options):
The CookieConfig argument is variadic and optional — omit it to use securityhttp’s secure cookie defaults.

Session fixation protection

session.FixationProtect(ctx, store, old) mints a new session, copies every attribute from old into it, deletes old, and returns the new session — migrating an attacker-fixated pre-login session id to a fresh one at the moment of authentication. SessionAuthentication (the middleware that resolves a session cookie into a core.Authentication on each request) does not call FixationProtect itself — it only re-authenticates from an already-established session. FixationProtect is invoked at the moment of authentication, by the login and credential-verification flows themselves:
  • web/auth/login.go (form login)
  • web/auth/mfa.go (MFA verification)
  • web/auth/passkey.go (passkey login)
  • web/auth/magiclink.go (magic-link login)
  • oauth2/callback.go (OAuth2/OIDC login)
Each of these calls session.FixationProtect(ctx, store, existingSession) on successful authentication so that a session id an attacker may have fixated before login can never be reused after login.

SQL store (separate module)

sqlsession.Postgres{}/sqlsession.MySQL{} are built in; pass your own type implementing sqlsession.Dialect for any other SQL flavor. See go-security-sql for the full interface, schema contract, and encryption details.

Redis store (separate module)

The Redis store serializes sessions with encoding/gob. Register any custom types your session values use: