| 141 | } |
| 142 | |
| 143 | func FuzzUnmarshalKeyComparison(f *testing.F) { |
| 144 | // Add seed corpus for expected valid key and the known problematic cases. |
| 145 | f.Add("field") |
| 146 | f.Add("FIELD") |
| 147 | f.Add("field\x00") |
| 148 | |
| 149 | f.Fuzz(func(t *testing.T, key string) { |
| 150 | // Generate valid JSON with the fuzzed key. |
| 151 | // json.Encoder ensures the key is properly escaped (e.g. \x00 becomes \u0000). |
| 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 | // Add seed corpus. |