Description
gorilla/csrf is an HTTP middleware library for Go that protects web applications against cross-site request forgery. Token returns the token to put in an HTML form, a JSON response body or a header. TemplateField helper replaces a csrfField template tag with a hidden input in html/template templates. Handler interface, including the Gorilla toolkit, net/http, Goji, Gin and Echo, and it is compatible with middleware helpers such as Alice and Negroni. Protect should be 32 bytes long, persist across restarts, stay secret and not be hardcoded in source code. On POST, PUT, PATCH, DELETE and similar requests, the middleware checks the request headers first and then the form body for the token. The examples cover HTML forms, JavaScript applications, Google App Engine, SameSite settings and other options.…
Highlights
- CSRF middleware for net/http and routers
- csrf.Token for forms, JSON and headers
- Template field helper for html/template
- Works with Gin, Echo and Gorilla
- Examples for JavaScript apps and SameSite
Ratings & Reviews
No ratings yet
Be the first to rate Csrf.
People also visit
Csrf FAQ
What is gorilla/csrf?
A Go middleware library that protects web applications from cross-site request forgery.
Which frameworks does it work with?
Any framework built on Go's http.Handler interface, including the Gorilla toolkit, net/http, Goji, Gin and Echo.
How do I add a token to a form?
Use csrf.TemplateField in html/template to insert a hidden field, or get the value with csrf.Token(r) and send it in a JSON body or a header.
What key should I use?
A 32-byte key that persists across restarts and is kept secret. Do not hardcode it in open-source code, and a new random key will invalidate existing cookies.
Does it work with JavaScript apps?
Yes. The README has an example for JavaScript applications that send the token with their requests.
What are the best Csrf alternatives?
Popular alternatives to Csrf include Wafris RB, Go Http Metrics and Goshs. See all Csrf alternatives






