Open freman opened 6 years ago
@freman
Some servers and software return cookies with paths encoded... There are some characters in RFC6265 which should be encoded/escaped, but space isn't one of the ones listed.
If encoding spaces isn't part of the spec, please specify which "servers and software" you are referring to. (The more popular the server/software, the more priority we should give to supporting.)
I copied cookiejar, modified jar.go to attempt to unescape the path, there might be a better option, but it worked for this instance.
I don't see anything wrong with your implementation. Probably worth sending a patch with the changes above (plus adding to jar_test.go
).
@meirf
If encoding spaces isn't part of the spec, please specify which "servers and software" you are referring to. (The more popular the server/software, the more priority we should give to supporting.)
In this particular instance it's keycloak but I've had issues with a couple of other things before (including sadly the door security system in the office)
As usual, I'd be normally be on the side arguing to follow the letter RFC but it seems with Go's http client I can't construct a request with any amount of escaping that cookie jar would consider valid against the given cookie.
I don't see anything wrong with your implementation. Probably worth sending a patch with the changes above (plus modification to jar_test.go).
Sure, gladly. I was content just using a fork called "coooookie" for our own code but I'm finding anything we use from third parties now needs forking to use with our keycloak (as a client)
edit:
ooooh, jar_test.go's simple cookie test uses url.Parse(s)
to both set it's cookies and test it's cookies, so it's automatically de-escaping
Change https://golang.org/cl/122592 mentions this issue: net/http/cookiejar: unescape cookie paths
My reading of RFC 6265 is that encoding spaces in the cookie path is wrong.
RFC 6265 operates on the request-uri as defined in RFC 2616. My reading of 2616 is, that the request-uri is the decoded value: While a path may be written as /aaa%2fbbb during transport, the actually request-uri is "/aaa/bbb".
This is what net/url.URL.Path represents and this value is used as the request-uri's path part in the cookiejar.
The whole idea of a cookie path is to prevent the cookie to be sent back somewhere it does not belong. If the Path attribute in a Set-Cookie header is wrong (unacceptable for the path in the request), the cookie is dropped. Like it is for domains. I really do not like the idea of relaxing this security feature for a broken server.
Especially as this happens totally opaque between the http.Client and the Jar.
I think the right solution for your use case would be to manage this cookie manually: Extract it manually from the Set-Cookie header, persist it and inject it back in subsequent requests manually.
The main question before changing cookiejar.Jar's behaviour would be to investigate how browsers handle such Set-Cookie values. (And if they only decode %20 or all %## sequences.)
If majority of actual browsers think it is safe to do this unescaping then we could start thinking if adopting this too.
I think this issue can be closed.
For the record I just knocked out a forked cookie jar to solve it for us, but afik chrome, firefox, safari don't seem to have the issue, and I lost track of where this issue went and if my patches were accepted or not. This is really an edge case around some upstream java software doing something it probably shouldn't but I couldn't change that end of the conversation.
Some servers and software return cookies with paths encoded
eg.
/path/contains%20some%20spaces
Go's default cookiejar can't match those when we make calls back no matter how we make them
There are some characters in RFC6265 which should be encoded/escaped, but space isn't one of the ones listed. This might be a side effect of go being really nice with urls and making it so we rarely have to deal with escaped paths in code.
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?What did you do?
https://play.golang.org/p/_3MyXdTJbm-
nb. The cookie set in that example is forced escaping to emulate what the software I'm calling is doing.
What did you expect to see?
all is well
What did you see instead?
cookie not found cookie not found
How did you work around it
I copied cookiejar, modified jar.go to attempt to unescape the path, there might be a better option, but it worked for this instance.