Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions internal/api/decode_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ const maxDecodeWork = 256 << 10
// until the body decodes or a bound is reached. A body with one wrong-type
// field returns the error of encoding/json unchanged. Any other error
// (syntax, a wrong top-level type) is returned unchanged.
//
// The search for the member finds it by the dotted path that encoding/json
// reports, which names a member of a struct, a slice or an array and nothing
// else. Where the path is not that of one member (a map key with a dot in it,
// a key that does not decode, a type that refuses null) the member is not
// found, or nulling it does not clear the error. The loop stops at the first
// pass that leaves the body unchanged and at the first error that names a
// field again, and the answer holds what was found so far. The request types
// that reach unmarshalBody have no such place:
// TestRequestTypesStayInsideTheDecodeWalk fails when one gains it.
func unmarshalBody(body []byte, dst any) error {
err := json.Unmarshal(body, dst)
te, ok := errors.AsType[*json.UnmarshalTypeError](err)
Expand All @@ -34,18 +44,20 @@ func unmarshalBody(body []byte, dst any) error {
}
var errs bodyErrors
errs.add(te)
named := map[string]bool{te.Field: true}
work, budget := body, maxDecodeWork-len(body) // the first decode counts
for !errs.full() && budget >= len(work) {
next, ok := nullMembers(work, te.Field)
// null is longer than a value of 1 to 3 bytes, so the body can grow.
if !ok || len(next) > budget {
if !ok || len(next) > budget || bytes.Equal(next, work) {
break
}
work, budget = next, budget-len(next)
te, ok = errors.AsType[*json.UnmarshalTypeError](json.Unmarshal(work, dst))
if !ok || te.Field == "" {
if !ok || te.Field == "" || named[te.Field] {
break
}
named[te.Field] = true
errs.add(te)
}
return errs.err()
Expand Down
81 changes: 81 additions & 0 deletions internal/api/decode_body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -212,3 +213,83 @@ func TestDecodeError(t *testing.T) {
t.Errorf("frame with a syntax error: %q", got)
}
}

// decodeCount decodes into V and counts the decodes of its body, as passes
// of unmarshalBody (calls is set by the test).
type decodeCount[T any] struct {
V T
calls *int
}

func (c *decodeCount[T]) UnmarshalJSON(b []byte) error {
*c.calls++
return json.Unmarshal(b, &c.V)
}

// decodeRefuses is the type of the member x: it refuses every value, null
// too, with a type error that names x.
type decodeRefuses struct{}

func (*decodeRefuses) UnmarshalJSON([]byte) error {
return &json.UnmarshalTypeError{Value: "string", Type: reflect.TypeFor[int](), Field: "x"}
}

// TestUnmarshalBodyStopsWithoutProgress: unmarshalBody stops at the first
// pass that cannot clear the error. In each case the search finds another
// member than the one that failed, or the failed member itself, and nulling
// it leaves the same error, so main decoded the body 100 times. The answer
// is the one error, json's own.
func TestUnmarshalBodyStopsWithoutProgress(t *testing.T) {
type keyed struct {
M map[string]struct{ B int } `json:"m"`
}
type ints struct {
M map[int]int `json:"m"`
}
type slashed struct {
M map[string]int `json:"m"`
}
type refuses struct {
X decodeRefuses `json:"x"`
Y int `json:"y"`
}
for _, tc := range []struct {
name, body string
decode func(body []byte, calls *int) error
wantParam string
wantCalls int
}{
{name: "a map key with a dot and the members it spells", body: `{"m":{"a.b":{"B":"x"},"a":{"b":{"B":2}}}}`,
decode: func(b []byte, n *int) error { return unmarshalBody(b, &decodeCount[keyed]{calls: n}) },
wantParam: "m.a.b.B", wantCalls: 2},
{name: "a map key that is a path escape and the key it spells", body: `{"m":{"a/b":"x","a~1b":"y"}}`,
decode: func(b []byte, n *int) error { return unmarshalBody(b, &decodeCount[slashed]{calls: n}) },
wantParam: "m.a~1b", wantCalls: 2},
{name: "a map key that does not decode", body: `{"m":{"x":1}}`,
decode: func(b []byte, n *int) error { return unmarshalBody(b, &decodeCount[ints]{calls: n}) },
wantParam: "m.x", wantCalls: 2},
{name: "a type that refuses null", body: `{"x":5,"y":6}`,
decode: func(b []byte, n *int) error { return unmarshalBody(b, &decodeCount[refuses]{calls: n}) },
wantParam: "x", wantCalls: 2},
// The member is null already: there is nothing to replace, so no
// second decode runs.
{name: "a type that refuses null, sent null", body: `{"x":null,"y":6}`,
decode: func(b []byte, n *int) error { return unmarshalBody(b, &decodeCount[refuses]{calls: n}) },
wantParam: "x", wantCalls: 1},
} {
t.Run(tc.name, func(t *testing.T) {
var calls int
err := tc.decode([]byte(tc.body), &calls)
items := invalidParamsFor(err)
if len(items) != 1 || items[0].Name != tc.wantParam {
t.Fatalf("items = %+v, want the one field %q (err %v)", items, tc.wantParam, err)
}
if _, ok := errors.AsType[*json.UnmarshalTypeError](err); !ok {
t.Fatalf("err %T: a field that two decodes name is one problem, and the answer is json's own error", err)
}
if calls != tc.wantCalls {
t.Fatalf("%d decodes, want %d", calls, tc.wantCalls)
}
})
}
}
Loading
Loading