Skip to content

Add failing test case for double cookie setting#62

Open
aeneasr wants to merge 2 commits intojustinas:masterfrom
aeneasr:fix-61
Open

Add failing test case for double cookie setting#62
aeneasr wants to merge 2 commits intojustinas:masterfrom
aeneasr:fix-61

Conversation

@aeneasr
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

aeneasr commented Mar 9, 2021

ping @justinas

Copy link
Copy Markdown
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.

Comment thread context_legacy.go
defer cmMutex.Unlock()

ctx, ok := contextMap[req]
if !ok {
Copy link
Copy Markdown
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.")
}

Comment thread context_legacy.go

ctx, ok := contextMap[req]

if !ok {
Copy link
Copy Markdown
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.

Comment thread handler_legacy_test.go
n.RegenerateToken(w, r)
}))

r := &http.Request{Method: "GET", URL: &url.URL{
Copy link
Copy Markdown
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 {

Comment thread handler_legacy_test.go
}

func TestNoDoubleCookie(t *testing.T) {
var n *CSRFHandler
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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