Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json: do not escape html special values (#14518) #14637

Merged
merged 2 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion types/json/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func marshalStringTo(buf, s []byte) []byte {
start := 0
for i := 0; i < len(s); {
if b := s[i]; b < utf8.RuneSelf {
if htmlSafeSet[b] {
if safeSet[b] {
i++
continue
}
Expand Down
1 change: 1 addition & 0 deletions types/json/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (s *testJSONSuite) TestBinaryJSONMarshalUnmarshal(c *C) {
`{"a": [1, "2", {"aa": "bb"}, 4, null], "b": true, "c": null}`,
`{"aaaaaaaaaaa": [1, "2", {"aa": "bb"}, 4.1], "bbbbbbbbbb": true, "ccccccccc": "d"}`,
`[{"a": 1, "b": true}, 3, 3.5, "hello, world", null, true]`,
`{"a": "&<>"}`,
}
for _, str := range strs {
parsedBJ := mustParseBinaryFromString(c, str)
Expand Down
17 changes: 8 additions & 9 deletions types/json/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,20 @@ const (
const unknownTypeCodeErrorMsg = "unknown type code: %d"
const unknownTypeErrorMsg = "unknown type: %s"

// htmlSafeSet holds the value true if the ASCII character with the given
// array position can be safely represented inside a JSON string, embedded
// inside of HTML <script> tags, without any additional escaping.
// safeSet holds the value true if the ASCII character with the given array
// position can be represented inside a JSON string without any further
// escaping.
//
// All values are true except for the ASCII control characters (0-31), the
// double quote ("), the backslash character ("\"), HTML opening and closing
// tags ("<" and ">"), and the ampersand ("&").
var htmlSafeSet = [utf8.RuneSelf]bool{
// double quote ("), and the backslash character ("\").
var safeSet = [utf8.RuneSelf]bool{
' ': true,
'!': true,
'"': false,
'#': true,
'$': true,
'%': true,
'&': false,
'&': true,
'\'': true,
'(': true,
')': true,
Expand All @@ -89,9 +88,9 @@ var htmlSafeSet = [utf8.RuneSelf]bool{
'9': true,
':': true,
';': true,
'<': false,
'<': true,
'=': true,
'>': false,
'>': true,
'?': true,
'@': true,
'A': true,
Expand Down