Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Added CSRF middleware (fix issue #243) #271

Merged
merged 3 commits into from
Mar 5, 2017
Merged

Added CSRF middleware (fix issue #243) #271

merged 3 commits into from
Mar 5, 2017

Conversation

stanislas-m
Copy link
Member

Adapt gorilla/csrf into a Buffalo middleware.

@stanislas-m stanislas-m added the enhancement New feature or request label Mar 4, 2017
// the supplied Referer header.
ErrBadReferer = errors.New("referer invalid")
// ErrNoToken is returned if no CSRF token is supplied in the request.
ErrNoToken = errors.New("CSRF token not found in request")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be ErrNoCSRFToken since this is available for the entire middleware package, it's potentially stealing that error name from another piece of middleware.

ErrNoToken = errors.New("CSRF token not found in request")
// ErrBadToken is returned if the CSRF token in the request does not match
// the token in the session, or is otherwise malformed.
ErrBadToken = errors.New("CSRF token invalid")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, should probably be ErrBadCSRFToken.


// EnableCSRF enable CSRF protection on routes using this middleware.
// This middleware is adapted from gorilla/csrf
func EnableCSRF() buffalo.MiddlewareFunc {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to CSRF or CSRFProtection something like that.

Also, there's no need to return a buffalo.MiddlewareFunc here, just make the function itself implement it.

func CSRF(next buffalo.Handler) buffalo.Handler {
...
}


func ctCSRFApp() *buffalo.App {
h := func(c buffalo.Context) error {
return c.Render(200, render.String(c.(*buffalo.DefaultContext).Data()["authenticity_token"].(string)))
Copy link
Member

@markbates markbates Mar 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return c.Render(200, render.String(c.Value("authenticity_token").(string)))
  • it's a lot shorter and easier on the eyes. :)

@markbates
Copy link
Member

Overall looks great! I just had a few small comments that will make a lot easier, and cleaner. Thanks for this!

}
a := buffalo.Automatic(buffalo.Options{})
a.GET("/csrf", middleware.EnableCSRF()(h))
a.POST("/csrf", middleware.EnableCSRF()(h))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a.GET("/csrf", middleware.CSRF(h))
a.POST("/csrf", middleware.CSRF(h))

When you update the CSRF function to be a buffalo.MiddlewareFunc itself, it becomes a lot easier to wrap buffalo.Handler functions.

You could also do the same thing with:

a.Use(middleware.CSRF)
a.GET("/csrf", h)
a.POST("/csrf", h)

@stanislas-m
Copy link
Member Author

@markbates Thanks for your review!

@markbates
Copy link
Member

Awesome! Thanks!

@markbates markbates merged commit 2a8e0a1 into gobuffalo:master Mar 5, 2017
@stanislas-m
Copy link
Member Author

No problem. :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants