Open jixunmoe opened 4 years ago
As a workaround, I've created an alternative cookie jar which forces the path to be /
.
package cookiejar
import (
"net/http"
"net/http/cookiejar"
"net/url"
)
type Jar struct {
*cookiejar.Jar
}
func (j *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) {
for _, cookie := range cookies {
cookie.Path = "/"
}
j.Jar.SetCookies(u, cookies)
}
func New(opts *cookiejar.Options) (*Jar, error) {
var err error
jar := &Jar{}
jar.Jar, err = cookiejar.New(opts)
return jar, err
}
/cc @bradfitz
/cc @nigeltao
This seems like a real bug. The "Note" in RFC 6265 5.2 makes it pretty clear:
NOTE: The algorithm below is more permissive than the grammar in Section 4.1. For example, the algorithm strips leading and trailing whitespace from the cookie name and value (but maintains internal whitespace), whereas the grammar in Section 4.1 forbids whitespace in these positions. User agents use this algorithm so as to interoperate with servers that do not follow the recommendations in Section 4.
So while sending a Set-Cookie header of the form Set-Cookie: SessionID=some_value;path =/;HttpOnly; is a violation of RFC 6265 we probably should accept it.
@jixunmoe You write
with valid Set-Cookie header The Set-Cookie header is technically not valid.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes. I'm using the latest go release for Windows.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Use
http.Client
with cookie jar to access site with validSet-Cookie
header, but the cookie did not persist.The first two requests:
In the second request, the cookie did not persist. When looking at the cookie jar after the first request, the cookie has been stored under the path
/html
instead of expected/
, which applies to the whole domain.When I further examine the response header from the initial request, there's a white-space after the
path
key. This extra whitespace has causedreadSetCookies
to not recognise the key-pair, and the cookie has been stored relative to the document path (i.e./html
).A quick read in RFC6265#5.2, step 4 states that the user-agent should strip leading or trailing whitespaces for the key/value string.
An attempt to fix this behaviour:
What did you expect to see?
The cookie is carried with subsequent requests.
What did you see instead?
The cookie was not set in subsequent requests, due to path mismatch.