Bound JSON-compatible value copying
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// Package jsonvalue validates and defensively copies JSON-compatible value
|
||||
// trees used by configuration, request, and prepared-state boundaries.
|
||||
// Package jsonvalue validates and defensively copies bounded JSON-compatible
|
||||
// value trees used by configuration, request, and prepared-state boundaries.
|
||||
package jsonvalue
|
||||
|
||||
import (
|
||||
@@ -8,29 +8,39 @@ import (
|
||||
"math"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const maxSafeJSONInteger = 1<<53 - 1
|
||||
const (
|
||||
maxContainerDepth = 100
|
||||
maxProducedNodes = 100_000
|
||||
)
|
||||
|
||||
type visit struct {
|
||||
typ reflect.Type
|
||||
ptr uintptr
|
||||
}
|
||||
|
||||
type traversalState struct {
|
||||
active map[visit]struct{}
|
||||
producedNodes int
|
||||
}
|
||||
|
||||
// Copy validates and deeply copies a JSON-compatible value while preserving
|
||||
// compatible concrete map, slice, array, scalar, and number types.
|
||||
// compatible concrete map, slice, array, scalar, and number types. It rejects
|
||||
// cycles and values that exceed the package's traversal limits.
|
||||
func Copy(src any) (any, error) {
|
||||
return copyValue(reflect.ValueOf(src), "value", make(map[visit]struct{}), true)
|
||||
return copyValue(reflect.ValueOf(src), "value", newTraversalState(), true, 0)
|
||||
}
|
||||
|
||||
// CopyMap validates and deeply copies an extra-parameter map while preserving
|
||||
// compatible concrete map, slice, array, scalar, and number types.
|
||||
// compatible concrete map, slice, array, scalar, and number types. It rejects
|
||||
// empty object keys, cycles, and values that exceed the package's traversal
|
||||
// limits.
|
||||
func CopyMap(src map[string]any) (map[string]any, error) {
|
||||
if src == nil {
|
||||
return nil, nil
|
||||
}
|
||||
copied, err := copyValue(reflect.ValueOf(src), "extra_params", make(map[visit]struct{}), false)
|
||||
copied, err := copyValue(reflect.ValueOf(src), "extra_params", newTraversalState(), false, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -44,71 +54,90 @@ func CopyMap(src map[string]any) (map[string]any, error) {
|
||||
func copyValue(
|
||||
value reflect.Value,
|
||||
path string,
|
||||
seen map[visit]struct{},
|
||||
state *traversalState,
|
||||
allowEmptyMapKeys bool,
|
||||
containerDepth int,
|
||||
) (any, error) {
|
||||
if !value.IsValid() {
|
||||
resolved, cleanup, isNull, err := state.resolveIndirection(value, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cleanup()
|
||||
if isNull {
|
||||
if err := state.produceNode(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
if value.Kind() == reflect.Interface {
|
||||
if value.IsNil() {
|
||||
return nil, nil
|
||||
}
|
||||
return copyValue(value.Elem(), path, seen, allowEmptyMapKeys)
|
||||
}
|
||||
value = resolved
|
||||
if !value.CanInterface() {
|
||||
return nil, fmt.Errorf("%s: value cannot be copied", path)
|
||||
}
|
||||
if number, ok := value.Interface().(json.Number); ok {
|
||||
if _, err := json.Marshal(number); err != nil {
|
||||
if !validJSONNumber(number) {
|
||||
return nil, fmt.Errorf("%s: invalid JSON number", path)
|
||||
}
|
||||
f, err := strconv.ParseFloat(number.String(), 64)
|
||||
if err != nil || math.IsNaN(f) || math.IsInf(f, 0) {
|
||||
return nil, fmt.Errorf("%s: invalid JSON number", path)
|
||||
if err := state.produceNode(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return number, nil
|
||||
}
|
||||
|
||||
switch value.Kind() {
|
||||
case reflect.Bool, reflect.String:
|
||||
if err := state.produceNode(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return value.Interface(), nil
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
if value.Int() < -maxSafeJSONInteger || value.Int() > maxSafeJSONInteger {
|
||||
return nil, fmt.Errorf("%s: integer is outside the JSON-safe range", path)
|
||||
if err := state.produceNode(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return value.Interface(), nil
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
if value.Uint() > maxSafeJSONInteger {
|
||||
return nil, fmt.Errorf("%s: integer is outside the JSON-safe range", path)
|
||||
if err := state.produceNode(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return value.Interface(), nil
|
||||
case reflect.Float32, reflect.Float64:
|
||||
number := value.Convert(reflect.TypeOf(float64(0))).Float()
|
||||
number := value.Float()
|
||||
if math.IsNaN(number) || math.IsInf(number, 0) {
|
||||
return nil, fmt.Errorf("%s: floating-point value must be finite", path)
|
||||
}
|
||||
if err := state.produceNode(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return value.Interface(), nil
|
||||
case reflect.Pointer:
|
||||
case reflect.Map:
|
||||
if value.IsNil() {
|
||||
if err := state.produceNode(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
current := visit{typ: value.Type(), ptr: value.Pointer()}
|
||||
if _, ok := seen[current]; ok {
|
||||
return nil, fmt.Errorf("%s: cyclic value is not supported", path)
|
||||
nextDepth, err := state.enterContainer(path, containerDepth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
seen[current] = struct{}{}
|
||||
defer delete(seen, current)
|
||||
return copyValue(value.Elem(), path, seen, allowEmptyMapKeys)
|
||||
case reflect.Map:
|
||||
return copyMapValue(value, path, seen, allowEmptyMapKeys)
|
||||
return copyMapValue(value, path, state, allowEmptyMapKeys, nextDepth)
|
||||
case reflect.Slice:
|
||||
if value.IsNil() {
|
||||
if err := state.produceNode(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
return copySequenceValue(value, path, seen, allowEmptyMapKeys)
|
||||
nextDepth, err := state.enterContainer(path, containerDepth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return copySequenceValue(value, path, state, allowEmptyMapKeys, nextDepth)
|
||||
case reflect.Array:
|
||||
return copySequenceValue(value, path, seen, allowEmptyMapKeys)
|
||||
nextDepth, err := state.enterContainer(path, containerDepth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return copySequenceValue(value, path, state, allowEmptyMapKeys, nextDepth)
|
||||
default:
|
||||
return nil, fmt.Errorf("%s: unsupported JSON value type %s", path, value.Type())
|
||||
}
|
||||
@@ -117,22 +146,26 @@ func copyValue(
|
||||
func copyMapValue(
|
||||
value reflect.Value,
|
||||
path string,
|
||||
seen map[visit]struct{},
|
||||
state *traversalState,
|
||||
allowEmptyMapKeys bool,
|
||||
containerDepth int,
|
||||
) (any, error) {
|
||||
if value.IsNil() {
|
||||
return nil, nil
|
||||
}
|
||||
if value.Type().Key().Kind() != reflect.String {
|
||||
return nil, fmt.Errorf("%s: map key type %s is not supported", path, value.Type().Key())
|
||||
}
|
||||
if err := state.produceNode(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := state.ensureChildCapacity(path, value.Len()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
current := visit{typ: value.Type(), ptr: value.Pointer()}
|
||||
if _, ok := seen[current]; ok {
|
||||
if _, ok := state.active[current]; ok {
|
||||
return nil, fmt.Errorf("%s: cyclic value is not supported", path)
|
||||
}
|
||||
seen[current] = struct{}{}
|
||||
defer delete(seen, current)
|
||||
state.active[current] = struct{}{}
|
||||
defer delete(state.active, current)
|
||||
|
||||
keys := value.MapKeys()
|
||||
sort.Slice(keys, func(i, j int) bool {
|
||||
@@ -152,7 +185,13 @@ func copyMapValue(
|
||||
if name == "" && !allowEmptyMapKeys {
|
||||
return nil, fmt.Errorf("%s: map key must not be empty", path)
|
||||
}
|
||||
copied, err := copyValue(value.MapIndex(key), path+"."+name, seen, allowEmptyMapKeys)
|
||||
copied, err := copyValue(
|
||||
value.MapIndex(key),
|
||||
path+"."+name,
|
||||
state,
|
||||
allowEmptyMapKeys,
|
||||
containerDepth,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -190,17 +229,25 @@ func copyMapValue(
|
||||
func copySequenceValue(
|
||||
value reflect.Value,
|
||||
path string,
|
||||
seen map[visit]struct{},
|
||||
state *traversalState,
|
||||
allowEmptyMapKeys bool,
|
||||
containerDepth int,
|
||||
) (any, error) {
|
||||
if err := state.produceNode(path); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := state.ensureChildCapacity(path, value.Len()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var current visit
|
||||
if value.Kind() == reflect.Slice {
|
||||
current = visit{typ: value.Type(), ptr: value.Pointer()}
|
||||
if _, ok := seen[current]; ok {
|
||||
if _, ok := state.active[current]; ok {
|
||||
return nil, fmt.Errorf("%s: cyclic value is not supported", path)
|
||||
}
|
||||
seen[current] = struct{}{}
|
||||
defer delete(seen, current)
|
||||
state.active[current] = struct{}{}
|
||||
defer delete(state.active, current)
|
||||
}
|
||||
|
||||
values := make([]any, value.Len())
|
||||
@@ -210,8 +257,9 @@ func copySequenceValue(
|
||||
copied, err := copyValue(
|
||||
value.Index(i),
|
||||
fmt.Sprintf("%s[%d]", path, i),
|
||||
seen,
|
||||
state,
|
||||
allowEmptyMapKeys,
|
||||
containerDepth,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -248,6 +296,73 @@ func copySequenceValue(
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func validJSONNumber(number json.Number) bool {
|
||||
var parsed json.Number
|
||||
if err := json.Unmarshal([]byte(number.String()), &parsed); err != nil {
|
||||
return false
|
||||
}
|
||||
return parsed.String() == number.String()
|
||||
}
|
||||
|
||||
func newTraversalState() *traversalState {
|
||||
return &traversalState{active: make(map[visit]struct{})}
|
||||
}
|
||||
|
||||
func (state *traversalState) produceNode(path string) error {
|
||||
if state.producedNodes >= maxProducedNodes {
|
||||
return fmt.Errorf("%s: JSON value work limit exceeded", path)
|
||||
}
|
||||
state.producedNodes++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (state *traversalState) enterContainer(path string, depth int) (int, error) {
|
||||
depth++
|
||||
if depth > maxContainerDepth {
|
||||
return 0, fmt.Errorf("%s: JSON container depth limit exceeded", path)
|
||||
}
|
||||
return depth, nil
|
||||
}
|
||||
|
||||
func (state *traversalState) ensureChildCapacity(path string, count int) error {
|
||||
if count > maxProducedNodes-state.producedNodes {
|
||||
return fmt.Errorf("%s: JSON value work limit exceeded", path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (state *traversalState) resolveIndirection(
|
||||
value reflect.Value,
|
||||
path string,
|
||||
) (reflect.Value, func(), bool, error) {
|
||||
var visits []visit
|
||||
cleanup := func() {
|
||||
for _, current := range visits {
|
||||
delete(state.active, current)
|
||||
}
|
||||
}
|
||||
|
||||
for value.IsValid() && (value.Kind() == reflect.Interface || value.Kind() == reflect.Pointer) {
|
||||
if value.IsNil() {
|
||||
return reflect.Value{}, cleanup, true, nil
|
||||
}
|
||||
if value.Kind() == reflect.Pointer {
|
||||
current := visit{typ: value.Type(), ptr: value.Pointer()}
|
||||
if _, ok := state.active[current]; ok {
|
||||
cleanup()
|
||||
return reflect.Value{}, nil, false, fmt.Errorf("%s: cyclic value is not supported", path)
|
||||
}
|
||||
state.active[current] = struct{}{}
|
||||
visits = append(visits, current)
|
||||
}
|
||||
value = value.Elem()
|
||||
}
|
||||
if !value.IsValid() {
|
||||
return reflect.Value{}, cleanup, true, nil
|
||||
}
|
||||
return value, cleanup, false, nil
|
||||
}
|
||||
|
||||
func canAssignNil(typ reflect.Type) bool {
|
||||
switch typ.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
|
||||
Reference in New Issue
Block a user