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

Add failing test case for double cookie setting #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

aeneasr
Copy link
Contributor

@aeneasr aeneasr commented Oct 14, 2020

The problem is that ServeHTTP detects that the cookie is missing. So it calls RegenerateToken() to create a new token. Because the downstream handler doesn't know that (and really has no way of currently knowing), it also calls RegenerateToken(). This then adds a second cookie to the HTTP response, causing this behavior.

See #61

@aeneasr
Copy link
Contributor Author

aeneasr commented Oct 14, 2020

Ok, 7ee434d should fix this. @justinas if you have some down time please give it a 👀

If you need help with maintainership, LMK!

@aeneasr aeneasr force-pushed the fix-61 branch 2 times, most recently from 63db8e2 to 3dd5c4d Compare October 14, 2020 09:34
@aeneasr
Copy link
Contributor Author

aeneasr commented Mar 9, 2021

ping @justinas

Copy link
Owner

@justinas justinas left a 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 {
Copy link
Owner

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

if !ok {
panic("Reason should never be set when there's no token" +
" (context) yet.")
}


ctx, ok := contextMap[req]

if !ok {
Copy link
Owner

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{
Copy link
Owner

Choose a reason for hiding this comment

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

Perhaps dummyGet would work?

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
Copy link
Owner

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(...)?

@aeneasr
Copy link
Contributor Author

aeneasr commented Mar 11, 2021

Thank you for the review, I'll try to address them as soon as I have a couple of free minutes!

@prologic
Copy link

Hey @justinas @aeneasr 👋 We really ought to get this PR's feedback addressed and finally merged don't you think? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants