Fix JSON Unicode decoding and YAML float validation

This commit is contained in:
2026-11-28 23:25:12 +00:00
parent 9cc9f6f777
commit 9f7744bda8
4 changed files with 128 additions and 4 deletions

View File

@@ -3,17 +3,29 @@
package validation
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math/big"
"strconv"
"strings"
"go.yaml.in/yaml/v4"
)
// Decode uses go-yaml's parser and scalar resolution, adapting its node graph to
// the documented JSON data model. Numbers retain their precision as json.Number.
// Decode uses the standard JSON decoder for JSON syntax and go-yaml otherwise,
// adapting both to the documented data model without losing number precision.
func Decode(data []byte) (any, error) {
if json.Valid(data) {
d := json.NewDecoder(bytes.NewReader(data))
d.UseNumber()
value, err := jsonValue(d, "")
if err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
return value, nil
}
var document yaml.Node
if err := yaml.Load(data, &document, yaml.WithUniqueKeys()); err != nil {
return nil, fmt.Errorf("decode: %w", err)
@@ -26,6 +38,50 @@ func Decode(data []byte) (any, error) {
return value, nil
}
// jsonValue consumes library-decoded tokens so duplicate keys, including keys
// with equivalent escape spellings, cannot be lost in a map unmarshalling step.
// Decode has already checked that the input is one syntactically valid value.
func jsonValue(d *json.Decoder, path string) (any, error) {
token, err := d.Token()
if err != nil {
return nil, err
}
switch token {
case json.Delim('{'):
value := make(map[string]any)
for d.More() {
token, err := d.Token()
if err != nil {
return nil, err
}
key := token.(string) // Valid JSON objects have string keys.
childPath := path + "/" + escape(key)
if _, exists := value[key]; exists {
return nil, fmt.Errorf("%s (byte %d): duplicate mapping key %q", pointer(childPath), d.InputOffset(), key)
}
value[key], err = jsonValue(d, childPath)
if err != nil {
return nil, err
}
}
_, err = d.Token() // Closing brace.
return value, err
case json.Delim('['):
value := make([]any, 0)
for d.More() {
child, err := jsonValue(d, fmt.Sprintf("%s/%d", path, len(value)))
if err != nil {
return nil, err
}
value = append(value, child)
}
_, err = d.Token() // Closing bracket.
return value, err
default:
return token, nil
}
}
type decoder struct {
active map[*yaml.Node]bool
values map[*yaml.Node]any
@@ -136,6 +192,11 @@ func scalar(n *yaml.Node) (any, error) {
// Normalize YAML's leading '+', '.5', and '5.' spellings, retaining the
// decimal digits instead of converting through float64.
text := strings.TrimPrefix(strings.ReplaceAll(n.Value, "_", ""), "+")
// Check syntax before supplying missing zeroes. Range errors are allowed:
// the exact decimal representation need not fit in a machine float.
if _, err := strconv.ParseFloat(text, 64); err != nil && !errors.Is(err, strconv.ErrRange) {
return nil, fmt.Errorf("invalid float %q: %w", n.Value, err)
}
mantissa, exponent, hasExponent := strings.Cut(strings.ToLower(text), "e")
sign := ""
if strings.HasPrefix(mantissa, "-") {