Skip to content

Instantly share code, notes, and snippets.

@0xdabbad00
Created April 3, 2015 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xdabbad00/1c9c6c293e57d5a24431 to your computer and use it in GitHub Desktop.
Save 0xdabbad00/1c9c6c293e57d5a24431 to your computer and use it in GitHub Desktop.
ApplyProtectionFromCSRF function
// Gist associated with http://0xdabbad00.com/2015/04/03/choosing_libraries_for_go_web_servers/
// ApplyProtectionFromCSRF makes all POST messages check for a csrf_token
func (application *Application) ApplyProtectionFromCSRF(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
protected := nosurf.New(h)
failureHandler := func(w http.ResponseWriter, r *http.Request) {
log.Errorf("Possible CSRF attack")
w.Write([]byte("400: Request could not be handled"))
w.WriteHeader(400)
}
protected.SetFailureHandler(http.HandlerFunc(failureHandler))
protected.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment