Strip BOM from input JSON when found

Fixes #6988

Signed-off-by: Anders Eknert <anders@eknert.com>
This commit is contained in:
Anders Eknert
2024-09-04 22:46:03 +02:00
committed by Ashutosh Narkar
parent 3c2fc5da85
commit efef7d238e
2 changed files with 18 additions and 0 deletions
+4
View File
@@ -114,6 +114,10 @@ func Reference(x interface{}) *interface{} {
// Unmarshal decodes a YAML, JSON or JSON extension value into the specified type.
func Unmarshal(bs []byte, v interface{}) error {
if len(bs) > 2 && bs[0] == 0xef && bs[1] == 0xbb && bs[2] == 0xbf {
bs = bs[3:] // Strip UTF-8 BOM, see https://www.rfc-editor.org/rfc/rfc8259#section-8.1
}
if json.Valid(bs) {
return unmarshalJSON(bs, v, false)
}
+14
View File
@@ -122,3 +122,17 @@ func TestInvalidYAMLValidJSON(t *testing.T) {
t.Fatal(err)
}
}
func TestUnmarshalJSONUTF8BOM(t *testing.T) {
bomFail := []byte{0xef, 0xbb, 0xbf, 0x22, 0x5c, 0x2f, 0x22, 0x0a} // "\/" preceded by UTF-8 BOM
if json.Valid(bomFail) {
t.Fatal("expected invalid JSON")
}
var x any
err := util.Unmarshal(bomFail, &x)
if err != nil {
t.Fatal("expected BOM to be stripped", err)
}
}