Skip to content
Merged
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
54 changes: 53 additions & 1 deletion defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package huma

import (
"encoding/json"
"errors"
"io"
)

Expand All @@ -19,7 +20,7 @@ var DefaultJSONFormat = Format{
enc.SetEscapeHTML(false)
return enc.Encode(v)
},
Unmarshal: json.Unmarshal,
Unmarshal: unmarshalJSONUnique,
}

// DefaultFormats is a map of default formats that can be set in the API's
Expand Down Expand Up @@ -93,3 +94,54 @@ func DefaultConfig(title string, version string) Config {
},
}
}

// countColons counts the colons outside strings in data, which for valid JSON
// is the number of object members.
func countColons(data []byte) int {
var escaped, inString bool
count := 0
for _, c := range data {
switch {
case escaped:
escaped = false
case inString && c == '\\':
escaped = true
case c == '"':
inString = !inString
case !inString && c == ':':
count++
}
}
return count
}

// countMembers counts the object members in a value decoded into any.
func countMembers(v any) int {
count := 0
switch v := v.(type) {
case map[string]any:
count += len(v)
for _, item := range v {
count += countMembers(item)
}
case []any:
for _, item := range v {
count += countMembers(item)
}
}
return count
}

// unmarshalJSONUnique is json.Unmarshal that also rejects duplicate object keys
// when decoding into an untyped value, as done for validation. Validation sees
// only the last duplicate while decoding into a struct merges them, so a
// duplicate could carry values that were never validated.
func unmarshalJSONUnique(data []byte, v any) error {
if err := json.Unmarshal(data, v); err != nil {
return err
}
if p, ok := v.(*any); ok && countMembers(*p) < countColons(data) {
return errors.New("duplicate object key")
}
return nil
}
49 changes: 49 additions & 0 deletions defaults_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package huma_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/danielgtaylor/huma/v2"
)

func TestDefaultJSONFormatUnmarshal(t *testing.T) {
tests := []struct {
name string
input string
err string
target func() any
}{
{name: "scalar", input: `1`},
{name: "nested", input: `{"a":{"b":[{"c":1},{"c":2}]},"b":[1,{"a":1}]}`},
{name: "same key in sibling objects", input: `[{"a":1},{"a":2}]`},
{name: "case variants are distinct", input: `{"a":1,"A":2}`},
{name: "colons and escaped quotes in strings", input: `{"a:b":"c:\\\"d:","e":["f:"]}`},
{name: "duplicate top-level key", input: `{"a":1,"a":2}`, err: "duplicate object key"},
{name: "duplicate nested key", input: `{"inner":{"a":9},"inner":{}}`, err: "duplicate object key"},
{name: "duplicate in array item", input: `[{"a":1,"a":1}]`, err: "duplicate object key"},
{name: "empty object", input: `{}`},
{name: "escaped backslash before closing quote", input: `{"\\":1,"a":2}`},
{name: "json text inside a string", input: `{"a":"{\"a\":1,\"a\":2}"}`},
{name: "keys colliding after utf-8 replacement", input: "{\"\xff\":1,\"\xfe\":2}", err: "duplicate object key"},
{name: "typed target not checked", input: `{"a":1,"a":2}`, target: func() any { return &map[string]int{} }},
{name: "invalid json", input: `{"a":`, err: "unexpected end of JSON input"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var v any
target := any(&v)
if test.target != nil {
target = test.target()
}
err := huma.DefaultJSONFormat.Unmarshal([]byte(test.input), target)
if test.err == "" {
require.NoError(t, err)
return
}
assert.EqualError(t, err, test.err)
})
}
}
9 changes: 8 additions & 1 deletion formats/cbor/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"github.com/fxamacker/cbor/v2"
)

// cborDecMode decodes the way validation reads the body: exact-case keys and
// no duplicates, so a decoded value can never skip validation.
var cborDecMode, _ = cbor.DecOptions{
DupMapKey: cbor.DupMapKeyEnforcedAPF,
FieldNameMatching: cbor.FieldNameMatchingCaseSensitive,
}.DecMode()

var cborEncMode, _ = cbor.EncOptions{
// Canonical enc opts
Sort: cbor.SortCanonical,
Expand All @@ -34,7 +41,7 @@ var DefaultCBORFormat = huma.Format{
Marshal: func(w io.Writer, v any) error {
return cborEncMode.NewEncoder(w).Encode(v)
},
Unmarshal: cbor.Unmarshal,
Unmarshal: cborDecMode.Unmarshal,
}

func init() {
Expand Down
32 changes: 32 additions & 0 deletions formats/cbor/cbor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,41 @@ import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDefaultCBORFormatUnmarshal(t *testing.T) {
type body struct {
Source string `json:"source,omitempty"`
}
tests := []struct {
name string
input []byte
err string
want body
}{
// {"source":"ok"}
{name: "exact key", input: []byte{0xA1, 0x66, 's', 'o', 'u', 'r', 'c', 'e', 0x62, 'o', 'k'}, want: body{Source: "ok"}},
// {"Source":"ok"}
{name: "case variant ignored", input: []byte{0xA1, 0x66, 'S', 'o', 'u', 'r', 'c', 'e', 0x62, 'o', 'k'}},
// {"source":"a","source":"b"}
{name: "duplicate key", input: []byte{0xA2, 0x66, 's', 'o', 'u', 'r', 'c', 'e', 0x61, 'a', 0x66, 's', 'o', 'u', 'r', 'c', 'e', 0x61, 'b'}, err: "duplicate map key"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var v body
err := DefaultCBORFormat.Unmarshal(test.input, &v)
if test.err != "" {
assert.ErrorContains(t, err, test.err)
return
}
require.NoError(t, err)
assert.Equal(t, test.want, v)
})
}
}

func TestRoundTrip(t *testing.T) {
data := map[any]any{"hello": "world"}

Expand Down
46 changes: 46 additions & 0 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,52 @@ func TestFeatures(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, resp.Code)
},
},
{
Name: "request-body-case-variant-validated",
Register: func(t *testing.T, api huma.API) {
huma.Register(api, huma.Operation{
Method: http.MethodPut,
Path: "/body",
}, func(ctx context.Context, input *struct {
Body struct {
Role string `json:"role" enum:"viewer,editor"`
}
}) (*struct{}, error) {
return nil, nil
})
},
Method: http.MethodPut,
URL: "/body",
Body: `{"role":"viewer","Role":"admin"}`,
Assert: func(t *testing.T, resp *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code)
assert.Contains(t, resp.Body.String(), `"location":"body.role"`)
},
},
{
Name: "request-body-duplicate-key",
Register: func(t *testing.T, api huma.API) {
huma.Register(api, huma.Operation{
Method: http.MethodPut,
Path: "/body",
}, func(ctx context.Context, input *struct {
Body struct {
Inner *struct {
A int `json:"a,omitempty" maximum:"5"`
} `json:"inner,omitempty"`
}
}) (*struct{}, error) {
return nil, nil
})
},
Method: http.MethodPut,
URL: "/body",
Body: `{"inner":{"a":9},"inner":{}}`,
Assert: func(t *testing.T, resp *httptest.ResponseRecorder) {
assert.Equal(t, http.StatusBadRequest, resp.Code)
assert.Contains(t, resp.Body.String(), "duplicate object key")
},
},
{
Name: "body-example-promoted-to-media-type",
Register: func(t *testing.T, api huma.API) {
Expand Down
41 changes: 39 additions & 2 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,12 @@ func handleMapString(r Registry, s *Schema, path *PathBuffer, mode ValidateMode,
continue
}

if m[actualKey] != nil && s.DependentRequired[k] != nil {
if actualKey != k {
// Validated below with the other case variants.
continue
}

if m[k] != nil && s.DependentRequired[k] != nil {
for _, dependent := range s.DependentRequired[k] {
if m[dependent] != nil {
continue
Expand All @@ -853,10 +858,12 @@ func handleMapString(r Registry, s *Schema, path *PathBuffer, mode ValidateMode,
}

path.Push(k)
Validate(r, v, path, mode, m[actualKey], res)
Validate(r, v, path, mode, m[k], res)
path.Pop()
}

validateCaseVariants(r, s, path, mode, m, res)

if addl, ok := s.AdditionalProperties.(bool); ok && !addl {
addlPropLoop:
for k := range m {
Expand Down Expand Up @@ -892,6 +899,36 @@ func handleMapString(r Registry, s *Schema, path *PathBuffer, mode ValidateMode,
}
}

// validateCaseVariants validates keys that only case-insensitively match a
// property. encoding/json assigns every such key to the field, last one
// winning, so each must pass the property's schema too.
func validateCaseVariants(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, m map[string]any, res *ValidateResult) {
for actual, val := range m {
if _, ok := s.Properties[actual]; ok {
continue
}
for _, k := range s.propertyNames {
if !strings.EqualFold(actual, k) || (val == nil && (!s.requiredMap[k] || s.Nullable)) {
continue
}

if val != nil && s.DependentRequired[k] != nil {
for _, dependent := range s.DependentRequired[k] {
if m[dependent] != nil {
continue
}

res.Add(path, m, s.msgDependentRequired[k][dependent])
}
}

path.Push(k)
Validate(r, s.Properties[k], path, mode, val, res)
path.Pop()
}
}
}

func handleMapAny(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, m map[any]any, res *ValidateResult) {
if s.MinProperties != nil {
if len(m) < *s.MinProperties {
Expand Down
Loading
Loading