-
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?
Conversation
63db8e2
to
3dd5c4d
Compare
ping @justinas |
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.
Sorry for sleeping on this! Just a few comments from my side.
defer cmMutex.Unlock() | ||
|
||
ctx, ok := contextMap[req] | ||
if !ok { |
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.
Lines 87 to 90 in 4d86df7
if !ok { | |
panic("Reason should never be set when there's no token" + | |
" (context) yet.") | |
} |
|
||
ctx, ok := contextMap[req] | ||
|
||
if !ok { |
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.
Same here, could probably assume that when ctxWasSent
is called, the context is already present.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps dummyGet
would work?
Line 20 in 4d86df7
func dummyGet() *http.Request { |
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: separate declaration not needed, just do n := New(...)
?
Thank you for the review, I'll try to address them as soon as I have a couple of free minutes! |
The problem is that
ServeHTTP
detects that the cookie is missing. So it callsRegenerateToken()
to create a new token. Because the downstream handler doesn't know that (and really has no way of currently knowing), it also callsRegenerateToken()
. This then adds a second cookie to the HTTP response, causing this behavior.See #61