Repository navigation
Commit 4e53fef
* 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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
635 | 635 | | |
636 | 636 | | |
637 | 637 | | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
638 | 644 | | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
639 | 676 | | |
640 | | - | |
| 677 | + | |
641 | 678 | | |
642 | 679 | | |
643 | | - | |
644 | 680 | | |
645 | 681 | | |
646 | 682 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
761 | 761 | | |
762 | 762 | | |
763 | 763 | | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
764 | 799 | | |
765 | 800 | | |
766 | 801 | | |
| |||
0 commit comments