-
Notifications
You must be signed in to change notification settings - Fork 126
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
Add failing test case for double cookie setting #62
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,12 @@ type csrfContext struct { | |
token string | ||
// reason for the failure of CSRF check | ||
reason error | ||
// wasSent is true if `Set-Cookie` was called | ||
// for the `name=csrf_token` already. This prevents | ||
// duplicate `Set-Cookie: csrf_token` headers. | ||
// For more information see: | ||
// https://github.com/justinas/nosurf/pull/61 | ||
wasSent bool | ||
} | ||
|
||
var ( | ||
|
@@ -79,6 +85,32 @@ func ctxSetToken(req *http.Request, token []byte) *http.Request { | |
return req | ||
} | ||
|
||
func ctxSetSent(req *http.Request) { | ||
cmMutex.Lock() | ||
defer cmMutex.Unlock() | ||
|
||
ctx, ok := contextMap[req] | ||
if !ok { | ||
ctx = new(csrfContext) | ||
contextMap[req] = ctx | ||
} | ||
|
||
ctx.wasSent = true | ||
} | ||
|
||
func ctxWasSent(req *http.Request) bool { | ||
cmMutex.RLock() | ||
defer cmMutex.RUnlock() | ||
|
||
ctx, ok := contextMap[req] | ||
|
||
if !ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, could probably assume that when |
||
return false | ||
} | ||
|
||
return ctx.wasSent | ||
} | ||
|
||
func ctxSetReason(req *http.Request, reason error) *http.Request { | ||
cmMutex.Lock() | ||
defer cmMutex.Unlock() | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -5,6 +5,8 @@ package nosurf | |||
import ( | ||||
"net/http" | ||||
"net/http/httptest" | ||||
"net/url" | ||||
"strings" | ||||
"testing" | ||||
) | ||||
|
||||
|
@@ -20,3 +22,24 @@ func TestClearsContextAfterTheRequest(t *testing.T) { | |||
t.Errorf("Instead, the context entry remains: %v", contextMap[req]) | ||||
} | ||||
} | ||||
|
||||
func TestNoDoubleCookie(t *testing.T) { | ||||
var n *CSRFHandler | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: separate declaration not needed, just do |
||||
n = New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||||
n.RegenerateToken(w, r) | ||||
})) | ||||
|
||||
r := &http.Request{Method: "GET", URL: &url.URL{ | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps Line 20 in 4d86df7
|
||||
Scheme: "http", | ||||
Host: "dummy.us", | ||||
Path: "/", | ||||
}} | ||||
w := httptest.NewRecorder() | ||||
|
||||
n.ServeHTTP(w, r) | ||||
|
||||
count := strings.Count(w.HeaderMap.Get("Set-Cookie"), "csrf_token") | ||||
if count > 1 { | ||||
t.Errorf("Expected one CSRF cookie, got %d", count) | ||||
} | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be okay to assume context already exists here, like
Reason()
does.nosurf/context_legacy.go
Lines 87 to 90 in 4d86df7