Skip to content

Commit 4e53fef

Browse files
ArdyJunatarobtme
andauthored
fix: prevent panic in uniqueItems validation for unhashable types (#1042) (#1045)
* fix: prevent panic in uniqueItems validation for unhashable types Fixes #1042 The uniqueItems check in handleArray used map[any]struct{} as a hash set. When array items contain non-hashable types such as map[string]interface{} (produced by malformed JSON like [{"k":"v"}] for a []string field), inserting into the map panics with: http: panic serving ...: hash of unhashable type: map[string]interface {} This caused the server to return no response at all — not even a 500. Fix: introduce a uniqueItemsContains helper that attempts the O(1) map lookup on the fast path. A deferred recover() catches the panic for unhashable types and falls back to an O(n) reflect.DeepEqual linear scan, which: - Never panics regardless of element type - Still detects duplicate unhashable values correctly - Has no impact on the common case (hashable types hit the fast path) Malformed input like [{"k":"v"}] for a []string field now correctly returns a 422 'expected string' validation error instead of crashing. Regression tests added in validate_test.go: - uniqueItems with unhashable object element must not panic - uniqueItems detects duplicate unhashable objects - uniqueItems with mixed hashable and unhashable elements must not panic * perf(validate): keep uniqueItems check linear for hashable items The uniqueItems fix rebuilt a fresh map from all previously-seen items on every element, making the check O(n^2) for every uniqueItems array (not just the unhashable edge case). Since array length is client-controlled, that's a quadratic-blowup vector. Grow a single map incrementally for hashable items (O(1) lookups, the common case) and only fall back to a reflect.DeepEqual scan for genuinely unhashable items (maps/slices), preserving the recover-based guard from #1042. --------- Co-authored-by: Robert Thomas <[email protected]>
1 parent a8a668c commit 4e53fef

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎validate.go‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,48 @@ func handleArray[T any](r Registry, s *Schema, path *PathBuffer, mode ValidateMo
635635
}
636636

637637
if s.UniqueItems {
638+
// Hashable items are tracked in a map for O(1) lookups. Non-hashable
639+
// items (e.g. map[string]interface{} from malformed JSON like [{}])
640+
// can't be used as map keys and would panic with "hash of unhashable
641+
// type" (issue #1042), so they are recorded separately and compared
642+
// with reflect.DeepEqual. This keeps the common all-hashable case
643+
// linear while still detecting duplicates among unhashable items.
638644
seen := make(map[any]struct{}, len(arr))
645+
var unhashable []any
646+
647+
// contains reports whether item was already seen, recording it if not.
648+
contains := func(item any) (dup bool) {
649+
hashable := true
650+
func() {
651+
defer func() {
652+
if recover() != nil {
653+
hashable = false
654+
}
655+
}()
656+
if _, ok := seen[item]; ok {
657+
dup = true
658+
} else {
659+
seen[item] = struct{}{}
660+
}
661+
}()
662+
if hashable {
663+
return dup
664+
}
665+
666+
// Slow path for unhashable items only.
667+
for _, u := range unhashable {
668+
if reflect.DeepEqual(u, item) {
669+
return true
670+
}
671+
}
672+
unhashable = append(unhashable, item)
673+
return false
674+
}
675+
639676
for _, item := range arr {
640-
if _, ok := seen[item]; ok {
677+
if contains(item) {
641678
res.Add(path, arr, validation.MsgExpectedArrayItemsUnique)
642679
}
643-
seen[item] = struct{}{}
644680
}
645681
}
646682

‎validate_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,41 @@ var validateTests = []struct {
761761
input: map[string]any{"value": []any{1, 2, 1, 3}},
762762
errs: []string{"expected array items to be unique"},
763763
},
764+
{
765+
// Regression test for issue #1042:
766+
// uniqueItems validation must NOT panic when array items are
767+
// non-hashable types (e.g. objects decoded from JSON like [{}]).
768+
// Expected: a 422 type-validation error, not a server crash.
769+
name: "uniqueItems with unhashable object element must not panic",
770+
typ: reflect.TypeFor[struct {
771+
Value []string "json:\"value\" uniqueItems:\"true\""
772+
}](),
773+
input: map[string]any{"value": []any{map[string]any{"key": "val"}}},
774+
errs: []string{"expected string"},
775+
},
776+
{
777+
// Regression test for issue #1042:
778+
// Duplicate unhashable objects should still be detected as non-unique.
779+
name: "uniqueItems detects duplicate unhashable objects",
780+
typ: reflect.TypeFor[struct {
781+
Value []any "json:\"value\" uniqueItems:\"true\""
782+
}](),
783+
input: map[string]any{"value": []any{
784+
map[string]any{"a": 1},
785+
map[string]any{"a": 1},
786+
}},
787+
errs: []string{"expected array items to be unique"},
788+
},
789+
{
790+
// Regression test for issue #1042:
791+
// Mixed arrays (some hashable, some not) must not panic.
792+
name: "uniqueItems with mixed hashable and unhashable elements must not panic",
793+
typ: reflect.TypeFor[struct {
794+
Value []any "json:\"value\" uniqueItems:\"true\""
795+
}](),
796+
input: map[string]any{"value": []any{"ok", map[string]any{"k": "v"}}},
797+
},
798+
764799
{
765800
name: "map success",
766801
typ: reflect.TypeFor[map[string]int](),

0 commit comments

Comments
 (0)