sessions
Cookie and filesystem session management for Go's net/http, with pluggable backends and key rotation.
Repository Health
Technical Analysis
gorilla/sessions provides cookie-based and filesystem-based HTTP session storage for Go’s net/http, along with the infrastructure to build custom backends. It wraps gorilla/securecookie to sign and optionally encrypt session data, exposes a Session type with typed helpers for flash messages, and lets a single request read from and save to multiple session stores at once through a shared Registry.
The package defines a small Store interface — Get, New, and Save — that the community has implemented for most major datastores, including Redis, PostgreSQL, MySQL, MongoDB, DynamoDB, and BoltDB, making it straightforward to swap the bundled CookieStore or FilesystemStore for a production-grade backend without changing handler code. It has shipped as part of the Gorilla web toolkit since 2012 and is a de facto standard for session handling in idiomatic Go HTTP services.
What You Get
- CookieStore for signed and optionally AES-encrypted session cookies via gorilla/securecookie
- FilesystemStore for server-side session storage with automatic file cleanup on expiry
- A Store interface (Get/New/Save) so custom backends plug in without touching handler code
- Cross-request Registry that lets multiple named sessions be fetched and saved together with sessions.Save(r, w)
- Flash message helpers (AddFlash/Flashes) for one-time post-redirect notices
- Built-in key rotation support for rotating authentication and encryption keys without invalidating existing sessions
Common Use Cases
- Storing a logged-in user’s identity and CSRF token in a signed cookie for a Go web app
- Persisting flash messages across a redirect after a form submission
- Swapping the default cookie store for a Redis- or Postgres-backed Store in production deployments
- Managing multiple independent sessions (e.g. auth session + preferences session) in one request
Under The Hood
Architecture The package centers on three thin abstractions: Session (the per-request value bag with Values, Options, and IsNew), Store (the Get/New/Save contract implemented by CookieStore and FilesystemStore in store.go), and Registry (sessions.go), which caches decoded sessions on the request’s context via GetRegistry(r) so repeated store.Get() calls within a handler don’t re-decode cookie data, and lets sessions.Save(r, w) flush every registered session in one call. CookieStore delegates all signing/encryption to gorilla/securecookie’s CodecsFromPairs, encoding Session.Values directly into the cookie; FilesystemStore instead encodes only a random session.ID into the cookie and persists the actual Values to a file under a package-level sync.RWMutex (fileMutex) guarding concurrent reads/writes, with save/load/erase as small private helpers in store.go. Options (options.go) is a plain struct mirrored onto http.Cookie by newCookieFromOptions (cookie.go), and lex.go contributes a single isCookieNameValid helper adapted from Go’s standard library. Because Store is only three methods, swapping the persistence layer requires no changes to Session, Registry, or handler code — the abstraction boundary is deliberately narrow and stable.
Tech Stack gorilla/sessions is pure Go (module github.com/gorilla/sessions, go 1.23) with a single external dependency, github.com/gorilla/securecookie, which supplies the HMAC signing and optional AES encryption codecs used by both stores. It has no web framework dependency — it operates directly on net/http.Request and http.ResponseWriter, so it works unmodified with net/http or any router built on it. Session values are serialized with the standard library’s encoding/gob (registered via gob.Register in an init() function), so any custom type stored in Values must itself be gob-registered. Build tooling is a plain Makefile driving race-enabled tests, golangci-lint, gosec, and govulncheck; there is no ORM, database driver, or bundler — deployment is simply importing the package into a Go binary.
Code Quality Tests exist for all core source files with matching test counterparts, and CI runs them with race detection and coverage across Linux, macOS, and Windows on every push, uploading coverage to Codecov. Separate workflows run golangci-lint, gosec, and govulncheck on every push and pull request — an unusually thorough setup for a package this size. Error handling is explicit and idiomatic: store methods return errors and callers are shown checking them throughout the documentation; a MultiError type aggregates per-session save failures so a multi-session failure doesn’t mask individual errors. Naming is conventional exported-Go style with doc comments on every exported symbol. The main type-safety gap is inherent to the design rather than an oversight: session values are stored in a generic map requiring type assertions at every read site, and the maintainers themselves label the filesystem store as still experimental.
What Makes It Unique gorilla/sessions doesn’t introduce novel session-storage techniques — cookie signing and pluggable backend interfaces are well-established patterns predating this package. Its distinguishing choice is minimalism: a small Store interface that the community has independently implemented for over twenty backend adapters without needing changes to this package, and a registry mechanism for coordinating multiple simultaneous sessions per request that’s more ergonomic than most competing libraries’ single-session APIs. Built-in key-pair rotation is a small but genuinely useful operational feature that many hand-rolled cookie-session implementations omit. Overall this is standard, well-executed engineering rather than innovation.
Used by 8 apps in this directory
authentik
Authentication · Security
The self-hosted Identity Provider that replaces Okta, Auth0, and Entra ID with a unified SSO platform supporting SAML, OAuth2/OIDC, LDAP, RADIUS, and WebAuthn.
Fathom Lite
Analytics
A simple, self-hosted website analytics tool built with Go and Preact that lets you understand your traffic without handing data to third parties.
Gitea
Devops · Developer Tools · Project Management
Self-hosted DevOps in a single Go binary — Git hosting, GitHub Actions-compatible CI/CD, and 30+ package registries without any SaaS dependency.
Hatchet
AI Development · Developer Tools · Automation
A Postgres-backed orchestration engine for background tasks, AI agents, and durable workflows that replaces Redis queues and multi-datastore durable execution platforms with a single self-hostable service.
Ory Kratos
Authentication
API-first identity and user management that handles login, registration, MFA, and recovery so your application never has to.
Rill
Analytics · Data Engineering
The fastest BI tool for humans and agents — define metrics, models, and dashboards as code and query them instantly on ClickHouse or DuckDB.
seonaut
Marketing · Developer Tools
Self-hosted SEO auditing that crawls your entire website and surfaces critical issues — broken links, duplicate meta tags, redirect loops, and more — before they hurt your rankings.
Wakapi
Developer Tools · Analytics
Self-hosted WakaTime-compatible coding statistics backend that gives developers full control over their coding activity data.