encoding/json: Compact should be consistent with escaping #34070
Comments
I think each of the options has merit, except doing nothing. In #30357 I complained that Compact behaved surprisingly, doing more than what was documented, and returning a less compact result in contrast to what the function name implied. Doing nothing means that issue persists. As for the three other options I think:
|
I agree that we need to do something here. I think the best solution is to stop doing any escaping at all. Like others have mentioned in this thread, that's what is documented, I doubt that any programs would break. If any was depending on undocumented behavior, one could argue that their program should have been using My opinion is that we can give this a try in the current cycle for 1.14, and reconsider or revert if a large amount of users complains once it lands in master or in any of the pre-releases. I'm happy to review a CL. |
Change https://golang.org/cl/200217 mentions this issue: |
As requested by @mvdan I'm filing an issue to decide on the escaping behaviour of
Compact
for go1.14.#30357 was originally filed as
Compact
escapes U+2028 and U+2029 which increases the output size without being documented. The fix for that issue was documentation (CL 173417) that turned out to be incorrect and was reverted by CL 188717.Compact
currently escapes U+2028 and U+2029, but it doesn't escape <, > or &.Compact
is thus only performing a subset of HTML escaping and it's output is not safe to embed inside HTML<script>
tags as CL 173417 had documented.If I understand correctly, the escaping of U+2028 and U+2029 was added not for HTML but rather for JavaScript interpreters that might consume a directly embedded JSON literal.
The options seem to be:
Stop performing any escaping in
Compact
as the original issue requested. The escaping of escaping U+2028 and U+2029 was added in CL 10883045, and reading over the comments, it does seem like it was intentional to haveCompact
escape U+2028 and U+2029.Have
Compact
properly perform HTML escaping, asHTMLEscape
, does and be documented as such. This is likely the safest option, as the only consequence to overly aggressive escaping is larger encoded JSON.Document that
Compact
escapes U+2028 and U+2029, but not <, > or & and is thus not safe to embed in HTML without separately callingHTMLEscape
.Do nothing~
/cc @PhilipBorgesen @rsc @bradfitz
The text was updated successfully, but these errors were encountered: