Skip to content

Commit 8f6d68e

Browse files
vdoblerbradfitz
authored andcommitted
net/http: send more cookie values in double quotes
According to RFC 6255 a cookie value may contain neither spaces " " nor commas ",". But browsers seem to handle these pretty well and such values are not uncommon in the wild so we do allow spaces and commas in cookie values too. Up to now we use the double-quoted wire format only for cookie values with leading and/or trailing spaces and commas. Values with internal spaces/commas are sent without the optional double quotes. This seems to be a problem for some agents. This CL changes the behaviour for cookie values with spaces or commas: Such values are always sent in double quotes. This should not have any impact on existing agents and the increases of data transmitted is negligible. Fixes #18627 Change-Id: I575a98d589e048aa39d976a3c984550daaca730a Reviewed-on: https://go-review.googlesource.com/37328 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 1611839 commit 8f6d68e

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/net/http/cookie.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func sanitizeCookieValue(v string) string {
328328
if len(v) == 0 {
329329
return v
330330
}
331-
if v[0] == ' ' || v[0] == ',' || v[len(v)-1] == ' ' || v[len(v)-1] == ',' {
331+
if strings.IndexByte(v, ' ') >= 0 || strings.IndexByte(v, ',') >= 0 {
332332
return `"` + v + `"`
333333
}
334334
return v

src/net/http/cookie_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var writeSetCookiesTests = []struct {
6969
// are disallowed by RFC 6265 but are common in the wild.
7070
{
7171
&Cookie{Name: "special-1", Value: "a z"},
72-
`special-1=a z`,
72+
`special-1="a z"`,
7373
},
7474
{
7575
&Cookie{Name: "special-2", Value: " z"},
@@ -85,7 +85,7 @@ var writeSetCookiesTests = []struct {
8585
},
8686
{
8787
&Cookie{Name: "special-5", Value: "a,z"},
88-
`special-5=a,z`,
88+
`special-5="a,z"`,
8989
},
9090
{
9191
&Cookie{Name: "special-6", Value: ",z"},
@@ -398,9 +398,12 @@ func TestCookieSanitizeValue(t *testing.T) {
398398
{"foo\"bar", "foobar"},
399399
{"\x00\x7e\x7f\x80", "\x7e"},
400400
{`"withquotes"`, "withquotes"},
401-
{"a z", "a z"},
401+
{"a z", `"a z"`},
402402
{" z", `" z"`},
403403
{"a ", `"a "`},
404+
{"a,z", `"a,z"`},
405+
{",z", `",z"`},
406+
{"a,", `"a,"`},
404407
}
405408
for _, tt := range tests {
406409
if got := sanitizeCookieValue(tt.in); got != tt.want {

0 commit comments

Comments
 (0)