The showcase property: an Untrusted struct field is populated by json.Unmarshal with provenance intact, so printing it later sanitizes even though the bytes arrived through a JSON decode.
(t *testing.T)
| 54 | // json.Unmarshal with provenance intact, so printing it later sanitizes even |
| 55 | // though the bytes arrived through a JSON decode. |
| 56 | func TestUntrusted_survivesJSONDecode(t *testing.T) { |
| 57 | var entry struct { |
| 58 | Content Untrusted `json:"content"` |
| 59 | } |
| 60 | payload := `{"content":"log\u001b[31mline"}` |
| 61 | require.NoError(t, json.Unmarshal([]byte(payload), &entry)) |
| 62 | assert.Equal(t, "log\x1b[31mline", entry.Content.Raw()) |
| 63 | assert.NotContains(t, entry.Content.String(), esc) |
| 64 | } |
| 65 | |
| 66 | func TestUntrusted_jsonRoundTrip(t *testing.T) { |
| 67 | u := NewUntrusted("x\x1b[0m") |