| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package json |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"encoding/json" |
| 10 |
"fmt" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/google/go-cmp/cmp" |
| 15 |
) |
| 16 |
|
| 17 |
func TestUnmarshalCaseSensitivity(t *testing.T) { |
| 18 |
type Nested struct { |
| 19 |
Field string `json:"field"` |
| 20 |
} |
| 21 |
type Target struct { |
| 22 |
Field string |
| 23 |
TaggedField string `json:"custom_tag"` |
| 24 |
Nested *Nested |
| 25 |
} |
| 26 |
|
| 27 |
tests := []struct { |
| 28 |
name string |
| 29 |
json string |
| 30 |
want Target |
| 31 |
}{ |
| 32 |
{ |
| 33 |
name: "exact match", |
| 34 |
json: `{"Field": "value", "custom_tag": "tagged", "Nested": {"field": "nested"}}`, |
| 35 |
want: Target{ |
| 36 |
Field: "value", |
| 37 |
TaggedField: "tagged", |
| 38 |
Nested: &Nested{ |
| 39 |
Field: "nested", |
| 40 |
}, |
| 41 |
}, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "case mismatch", |
| 45 |
json: `{"field": "value", "Custom_tag": "tagged", "Nested": {"Field": "nested"}}`, |
| 46 |
want: Target{ |
| 47 |
Field: "", |
| 48 |
TaggedField: "", |
| 49 |
Nested: &Nested{ |
| 50 |
Field: "", |
| 51 |
}, |
| 52 |
}, |
| 53 |
}, |
| 54 |
} |
| 55 |
|
| 56 |
for _, tt := range tests { |
| 57 |
t.Run(tt.name, func(t *testing.T) { |
| 58 |
var got Target |
| 59 |
if err := Unmarshal([]byte(tt.json), &got); err != nil { |
| 60 |
t.Fatalf("Unmarshal failed: %v", err) |
| 61 |
} |
| 62 |
if diff := cmp.Diff(tt.want, got); diff != "" { |
| 63 |
t.Errorf("Unmarshal mismatch (-want +got):\n%s", diff) |
| 64 |
} |
| 65 |
}) |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestNewDecoderCaseSensitivity(t *testing.T) { |
| 70 |
type Target struct { |
| 71 |
Field string `json:"field"` |
| 72 |
TaggedField string `json:"custom_tag"` |
| 73 |
} |
| 74 |
|
| 75 |
tests := []struct { |
| 76 |
name string |
| 77 |
json string |
| 78 |
want Target |
| 79 |
}{ |
| 80 |
{ |
| 81 |
name: "exact match", |
| 82 |
json: `{"field": "value", "custom_tag": "tagged"}`, |
| 83 |
want: Target{Field: "value", TaggedField: "tagged"}, |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "case mismatch", |
| 87 |
json: `{"Field": "value", "Custom_tag": "tagged"}`, |
| 88 |
want: Target{}, |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
for _, tt := range tests { |
| 93 |
t.Run(tt.name, func(t *testing.T) { |
| 94 |
var got Target |
| 95 |
dec := NewDecoder(strings.NewReader(tt.json)) |
| 96 |
if err := dec.Decode(&got); err != nil { |
| 97 |
t.Fatalf("Decode failed: %v", err) |
| 98 |
} |
| 99 |
if diff := cmp.Diff(tt.want, got); diff != "" { |
| 100 |
t.Errorf("Decode mismatch (-want +got):\n%s", diff) |
| 101 |
} |
| 102 |
}) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func TestUnmarshalNullCharacter(t *testing.T) { |
| 107 |
type Target struct { |
| 108 |
Field string `json:"field"` |
| 109 |
} |
| 110 |
|
| 111 |
tests := []struct { |
| 112 |
name string |
| 113 |
json string |
| 114 |
want string |
| 115 |
}{ |
| 116 |
{ |
| 117 |
name: "null char in middle", |
| 118 |
json: `{"fi\u0000eld": "value"}`, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "null char at end", |
| 122 |
json: `{"field\u0000": "value"}`, |
| 123 |
}, |
| 124 |
{ |
| 125 |
name: "null char at start", |
| 126 |
json: `{"\u0000field": "value"}`, |
| 127 |
}, |
| 128 |
} |
| 129 |
|
| 130 |
for _, tt := range tests { |
| 131 |
t.Run(tt.name, func(t *testing.T) { |
| 132 |
var got Target |
| 133 |
if err := Unmarshal([]byte(tt.json), &got); err != nil { |
| 134 |
t.Fatalf("Unmarshal failed: %v", err) |
| 135 |
} |
| 136 |
if got.Field != "" { |
| 137 |
t.Errorf("Field was set: %s", tt.json) |
| 138 |
} |
| 139 |
}) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func FuzzUnmarshalKeyComparison(f *testing.F) { |
| 144 |
|
| 145 |
f.Add("field") |
| 146 |
f.Add("FIELD") |
| 147 |
f.Add("field\x00") |
| 148 |
|
| 149 |
f.Fuzz(func(t *testing.T, key string) { |
| 150 |
|
| 151 |
|
| 152 |
var buf bytes.Buffer |
| 153 |
enc := json.NewEncoder(&buf) |
| 154 |
enc.SetEscapeHTML(false) |
| 155 |
if err := enc.Encode(key); err != nil { |
| 156 |
return |
| 157 |
} |
| 158 |
encodedKey := buf.Bytes() |
| 159 |
jsonData := fmt.Sprintf(`{%s: "value"}`, encodedKey) |
| 160 |
|
| 161 |
type Target struct { |
| 162 |
Field string `json:"field"` |
| 163 |
} |
| 164 |
|
| 165 |
var got Target |
| 166 |
if err := Unmarshal([]byte(jsonData), &got); err != nil { |
| 167 |
return |
| 168 |
} |
| 169 |
|
| 170 |
if got.Field == "value" && key != "field" { |
| 171 |
t.Errorf("Unmarshal matched key %q (JSON: %s) to field 'field'", key, encodedKey) |
| 172 |
} |
| 173 |
}) |
| 174 |
} |
| 175 |
|
| 176 |
func FuzzUnmarshalFieldIsolation(f *testing.F) { |
| 177 |
|
| 178 |
f.Add("other", "value") |
| 179 |
f.Add("safe", "overwrite") |
| 180 |
f.Add("safe\x00", "attack") |
| 181 |
|
| 182 |
f.Fuzz(func(t *testing.T, key, val string) { |
| 183 |
|
| 184 |
var buf bytes.Buffer |
| 185 |
enc := json.NewEncoder(&buf) |
| 186 |
enc.SetEscapeHTML(false) |
| 187 |
if err := enc.Encode(key); err != nil { |
| 188 |
return |
| 189 |
} |
| 190 |
encodedKey := buf.Bytes() |
| 191 |
buf.Reset() |
| 192 |
if err := enc.Encode(val); err != nil { |
| 193 |
return |
| 194 |
} |
| 195 |
encodedVal := buf.Bytes() |
| 196 |
|
| 197 |
jsonData := fmt.Sprintf(`{"safe": "value", %s: %s}`, encodedKey, encodedVal) |
| 198 |
|
| 199 |
type Target struct { |
| 200 |
Safe string `json:"safe"` |
| 201 |
} |
| 202 |
|
| 203 |
var got Target |
| 204 |
if err := Unmarshal([]byte(jsonData), &got); err != nil { |
| 205 |
return |
| 206 |
} |
| 207 |
|
| 208 |
if got.Safe != "value" && key != "safe" { |
| 209 |
t.Errorf("Field 'safe' improperly modified by key %q (JSON: %s). Got %q, want %q", key, encodedKey, got.Safe, "value") |
| 210 |
} |
| 211 |
}) |
| 212 |
} |