Skip to content

Commit 20afe8c

Browse files
authored
fix: clean invalid csrf cookies (#246)
1 parent edd8924 commit 20afe8c

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

csrf/csrf.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package csrf
33
import (
44
"context"
55
"net/http"
6+
"time"
67

78
"github.com/gorilla/securecookie"
89
"github.com/rs/zerolog/log"
@@ -65,6 +66,7 @@ func New() func(http.Handler) http.Handler {
6566
Path: "/",
6667
Secure: true,
6768
HttpOnly: true,
69+
MaxAge: int(time.Hour/time.Second) * 24 * 7,
6870
})
6971

7072
next.ServeHTTP(w, r.WithContext(
@@ -86,6 +88,16 @@ func Verify(r *http.Request, token string) bool {
8688
return v.UserID == session.GetSession(r.Context()).UserID
8789
}
8890

91+
func Clear(w http.ResponseWriter) {
92+
http.SetCookie(w, &http.Cookie{
93+
Name: CookiesName,
94+
Value: "",
95+
Path: "/",
96+
Secure: true,
97+
HttpOnly: true,
98+
})
99+
}
100+
89101
type cookieValue struct {
90102
UserID int32 `json:"user_id"`
91103
}

episode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ func (h *handler) deleteEpisodePatch(w http.ResponseWriter, r *http.Request) err
333333
}
334334

335335
if !csrf.Verify(r, r.PostForm.Get(csrf.FormName)) {
336+
csrf.Clear(w)
336337
http.Error(w, "csrf failed", http.StatusBadRequest)
337338
return nil
338339
}

review.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func (h *handler) handleReview(w http.ResponseWriter, r *http.Request) error {
1616
}
1717

1818
if !csrf.Verify(r, r.PostForm.Get(csrf.FormName)) {
19+
csrf.Clear(w)
1920
http.Error(w, "csrf failed", http.StatusBadRequest)
2021
return nil
2122
}

subject.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ func (h *handler) deleteSubjectPatch(w http.ResponseWriter, r *http.Request) err
673673
}
674674

675675
if !csrf.Verify(r, r.PostForm.Get(csrf.FormName)) {
676+
csrf.Clear(w)
676677
http.Error(w, "csrf failed", http.StatusBadRequest)
677678
return nil
678679
}

0 commit comments

Comments
 (0)