Go removes double quotes in cookies. Is there a way to keep double quotes in cookies in Go?
For example, I'm sending a small JSON message and "SetCookie" strips double quote.
w.SetCookie("my_cookie_name", small_json_message)
More about Cookies:
The HTTP RFC defines quoted string values. See https://www.rfc-editor.org/rfc/rfc7230#section-3.2.6
The proposed cookie RFC explicitly says double quotes are allowed in cookie values:
cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )
Go currently has a condition to insert double quote into cookies, so obviously double quotes are allowed.
;
is the cookie delimiter.Values with ASCII characters outside the limited ASCII range may be quoted (The RFC calls this the
quoted_string
) which expands the allowed character set.JSON does not contain the
;
character, so for;
to appear in JSON it can only appear in string values. In JSON, string values are already quoted.I've confirmed testing using a simple k:v JSON payload and it works fine on all major browsers with no issues.
Cookies are typically generated by server data, not user data. This means well structured, not arbitrary, JSON may be used
JSON easily can conform to the cookie RFC. Additionally, even though it's not an issue with this example of JSON, regarding the hypothetical concern of not conforming to the RFC:
- A cookie is transmitted as a HTTP headers. Many HTTP headers commonly disregard the RFC. For example, the
Sec-Ch-Ua
header created by Chrome, includes several "bad" characters.
Sec-Ch-Ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"
- "comma" is "disallowed" and it's used all the time.
- Even if double quotes were "wrong", which they are not, but if they were, there are lots of in-the-wild examples of cookies containing quotes.
For reference, here's the relevant section of RFC 6265
set-cookie-header = "Set-Cookie:" SP set-cookie-string
set-cookie-string = cookie-pair *( ";" SP cookie-av )
cookie-pair = cookie-name "=" cookie-value
cookie-name = token
cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )
cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
; US-ASCII characters excluding CTLs,
; whitespace DQUOTE, comma, semicolon,
; and backslash
Where one can see whitespace DQUOTE
is disallowed and DQUOTE
is allowed.