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

net/http/cookiejar: cookie names containing underscore prefix cannot be manipulated #24527

Closed
qZanity opened this issue Mar 25, 2018 · 7 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@qZanity
Copy link

qZanity commented Mar 25, 2018

What version of Go are you using (go version)?

go version go1.8.1 windows/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=G:\dev\Go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
set PKG_CONFIG=pkg-config
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2

What did you do?

When using the http cookiejar, you cannot manipulate cookies that start with a underscore in the cookie names. If you attempt to change the cookie value, it just sets a duplicate cookie and does not change the original value. You also cannot delete these cookies using any of the known methods.

For example a cookie called "_myCookie" cannot be manipulated programmatically using *func (Jar) Cookies however, the cookiejar does handle the cookies automatically.

What did you expect to see?

Expected cookie value to change or cookie to be deleted.

What did you see instead?

Duplicate cookie with same name.

@FiloSottile FiloSottile changed the title net/http/cookiejar cookie names containing underscore prefix cannot be manipulated net/http/cookiejar: cookie names containing underscore prefix cannot be manipulated Mar 26, 2018
@FiloSottile FiloSottile added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 26, 2018
@FiloSottile FiloSottile added this to the Go1.11 milestone Mar 26, 2018
@fraenkel
Copy link
Contributor

@qZanity Can you show us some code?

@odeke-em
Copy link
Member

/cc @vdobler too

@vdobler
Copy link
Contributor

vdobler commented Mar 28, 2018

I cannot reproduce the problem. See https://play.golang.org/p/U6l6-5T54ds

@qZanity Maybe the underlying case of the problem you experience is not in the cookiejar but somewhere else? And maybe you could clarify what exactly you mean with

cannot be manipulated programmatically using func (Jar) Cookies

Note that Cookies() just reads cookies stored in the jar, it even returns copies of the cookies and cannot be used to modify the jar or cookies stored in it. The only way to modify cookies in the jar is through SetCookies. For deletion you need to set something like http.Cookie{Name: "nameofcookie", Value: "", MaxAge: -1}.

From my point of view it works as intended.

@qZanity
Copy link
Author

qZanity commented Mar 28, 2018

Thanks to everyone acting quickly on this.

@vdobler I checked your snippet and yes no issues there.

But check this: https://play.golang.org/p/7VP-SKwUlO5

This is a simple implementation using CookieJar with http.Client. You can see that after I SetCookie() a duplicate cookie is created and sent in the next request. I can't get my ahead around why this is happening? To me it looks like a bug, But its seems to be site specific. My development server is local, so I've used Adidas as an example here and it behaves the same way.

Cheers.

@fraenkel
Copy link
Contributor

The sample helped.
If you add a fmt.Printf("Jar %#v\n", jar), you will see what is going on.
There are 2 domains rather than what you expected.

www.adidas.co.uk;/;_abck":cookiejar.entry{Name:"_abck", Value:"test",
adidas.co.uk;/;_abck":cookiejar.entry{Name:"Value:"4EC318FFD79589413220DF05332AB30AC7EFB7199A1B000031B4BB5AB0C1B578

@vdobler
Copy link
Contributor

vdobler commented Mar 28, 2018

No, sorry this is not a bug.

You have been tripped by a common misunderstanding regarding the
identity of a cookie: The identity of a cookie is not determined
by its name alone but by the tripple (name,path,domain) in a browser
(or its cookiejar). So you can have douzens of cookies with the
same name if they differ on path and/or domain.

The following explanation is not correct in each and every
technical detail but provides a valid overview.

While the name is always given explicitly the path and domain of
a cookie are often determined implicitly from the URL. The _abck
cookie gets its path from the URL but its domain is set in the
response Header which looks like
Set-Cookie: _abck=4ACB08[...]; [...] path=/; domain=.adidas.co.uk
(which makes _abck a domain cookie instead of a host cookie for
www.adidas.co.uk).

If you just do a SetCookie with a different domain (in your case
the implicit domain extracted from the URL) you store a new, different
cookie. The fact that this new, different cookie has the same name
like the existing is of no concern here: Cookie identity is based
on the whole tripple of name, path and domain.

To fix you atempt to modifiy a cookie in the jar you have to
mimick what the Set-Cookie header does, something like

// Now lets manipluate one of the cookies. FIXED
cookie := &http.Cookie{Name: "_abck", Value: "test", Domain: ".adidas.co.uk"}
jar.SetCookies(cURL, []*http.Cookie{cookie})

But pay attention: This would not only update the Value of the
stored cookie but also its expiration time. With the current
Jar implementation of the standard library it is impossible to
get the information needed to update a cookie out of the Jar:
You have to remember the details (name, path, domain) of how each
cookie was stored to modify it afterwords by providing the same
identity tripple. (There are stlib Jar clones out there which
allow complete data retrieval. I could recommend one :-)

The result of Cookie() with several cookies with the same name
looks strange but is okay, actually even required by the spec.
The order of such same-name-cookies is even deterministic and
specified.

So it's not a bug, Jar works as required by RFC 6265 (which is
complicated and not widely known).

@qZanity
Copy link
Author

qZanity commented Mar 29, 2018

@fraenkel @vdobler

Totally understood and thanks for pointing that out, That was actually my first suspicion but and i actually modified the URI myself, obviously due to the 301 redirect it was still reverting back to the other domain thus creating two cookies.

Thanks for the explanation, Solved.

@qZanity qZanity closed this as completed Mar 29, 2018
@golang golang locked and limited conversation to collaborators Mar 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants