Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seatbelt: add option to skip csrf checks #21

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions example/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (

func main() {
app := seatbelt.New(seatbelt.Option{
TemplateDir: "templates",
Reload: true,
LocaleDir: "locales",
TemplateDir: "templates",
Reload: true,
LocaleDir: "locales",
SkipCSRFPaths: []string{"/api"},
})

app.Use(func(fn func(ctx *seatbelt.Context) error) func(*seatbelt.Context) error {
Expand All @@ -26,6 +27,9 @@ func main() {
app.Get("/", func(c *seatbelt.Context) error {
return c.Render("index", nil)
})
app.Post("/api", func(c *seatbelt.Context) error {
return c.JSON(201, map[string]string{"message": "Hello, world!"})
})
app.Get("/rendertostring", func(c *seatbelt.Context) error {
page := c.RenderToBytes("index", nil)
_, err := c.Response().Write([]byte(page))
Expand Down
15 changes: 15 additions & 0 deletions seatbelt.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ type Option struct {
// SkipServeFiles does not automatically serve static files from the
// project's /public directory when set to true. Default is false.
SkipServeFiles bool

// SkipCSRFPaths is used to skip the CSRF validation to POST, PUT, PATCH,
// DELETE, etc requests to paths that match one of the given paths.
SkipCSRFPaths []string
}

// setDefaults sets the default values for Seatbelt options.
Expand Down Expand Up @@ -408,6 +412,17 @@ func New(opts ...Option) *App {
// Initialize the underlying chi mux so that we can setup our default
// middleware stack.
mux := chi.NewRouter()
mux.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, skipPath := range opt.SkipCSRFPaths {
if strings.HasPrefix(r.URL.Path, skipPath) {
r = csrf.UnsafeSkipCheck(r)

}
}
h.ServeHTTP(w, r)
})
})
mux.Use(csrf.Protect(signingKey, csrf.Path("/")))

sess := session.New(signingKey, session.Options{
Expand Down
66 changes: 66 additions & 0 deletions seatbelt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,69 @@ func TestSubRouter(t *testing.T) {
}
})
}

func TestCSRFSkipPaths(t *testing.T) {
app := New(Option{
SkipCSRFPaths: []string{"/api", "/skip-me"},
})

app.Get("/", func(c *Context) error {
return c.JSON(200, map[string]string{"message": "ok"})
})
app.Post("/", func(c *Context) error {
return c.NoContent()
})
app.Post("/api", func(c *Context) error {
return c.NoContent()
})
app.Put("/skip-me/test", func(c *Context) error {
return c.NoContent()
})

srv := httptest.NewServer(app)
defer srv.Close()

cases := []struct {
path string
method string
status int
}{
{
path: "/",
method: http.MethodGet,
status: 200,
},
{
path: "/",
method: http.MethodPost,
status: 403,
},
{
path: "/api",
method: http.MethodPost,
status: 204,
},
{
path: "/skip-me/test",
method: http.MethodPut,
status: 204,
},
}

for _, c := range cases {
t.Run(c.method+" "+c.path, func(t *testing.T) {
req, err := http.NewRequest(c.method, srv.URL+c.path, nil)
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}

if resp.StatusCode != c.status {
t.Fatalf("expected %d but got %d", c.status, resp.StatusCode)
}
})
}
}
Loading