A Go middleware library providing CSRF protection for web applications with support for HTML forms and JavaScript frameworks.
gorilla/csrf is a Go middleware library that provides cross-site request forgery (CSRF) protection for web applications and APIs. It prevents malicious actors from executing unauthorized actions on behalf of authenticated users by validating tokens on state-changing requests like POST, PUT, and DELETE.
Go developers building web applications or RESTful APIs that require robust CSRF protection, especially those using frameworks like Gorilla, Gin, Echo, or standard net/http.
It offers a simple, secure, and framework-agnostic solution with sensible defaults, battle-tested design, and flexibility to integrate with both server-rendered HTML forms and modern JavaScript frontends.
Package gorilla/csrf provides Cross Site Request Forgery (CSRF) prevention middleware for Go web applications & services 🔒
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Works with any Go web framework using the standard http.Handler interface, including Gorilla, Gin, Echo, and net/http, as highlighted in the README's compatibility list.
Implements unique-per-request masked tokens to mitigate BREACH attacks and defaults to Secure, HttpOnly cookies with configurable SameSite policies for hardened protection.
Provides csrf.TemplateField for HTML forms and csrf.Token for JSON APIs, making it easy to adapt to both server-rendered pages and modern JavaScript frontends.
Offers options like csrf.TrustedOrigins for CORS and customizable header/field names, easing integration into existing or legacy systems without breaking changes.
The middleware consumes the request body when tokens are in form values, which can conflict with other middleware that need to read the body, requiring careful ordering in the chain.
Requires a persistent 32-byte auth key that must be kept secret and survive application restarts, adding complexity to deployment and secret management compared to stateless alternatives.
Relies on cookies for token storage, which may not suit applications using token-based auth without sessions or environments where cookies are disabled or problematic.