Skip to content

fix: prevent decoder crashes on unhashable map keys and deep nesting - #139

Merged
shamaton merged 2 commits into
mainfrom
fix/decoder-dos
Oct 15, 2026
Merged

shamaton merged 2 commits into
mainfrom
fix/decoder-dos

Conversation

@shamaton

Copy link
Copy Markdown
Owner

Summary

  • Reject bin and unhashable ext values as interface{} map keys instead of panicking
  • Limit decode nesting depth to 10000 to prevent fatal stack overflow
  • Applies to both Unmarshal and the streaming Decoder

Background / Motivation

Untrusted input could crash the process during decode:

  • A bin value used as a map key panicked with hash of unhashable type
  • Deeply nested arrays/maps/structs caused an unrecoverable stack overflow (not catchable with recover)

Changes

  • Map key validation: bin codes rejected up front; ext-decoded keys and struct/array-typed keys checked for hashability before insertion
  • Depth tracking on array/map/struct decoding, returning ErrExceededMaxDepth past the limit
  • New errors: ErrCanNotSetBinAsMapKey, ErrUnhashableMapKey, ErrExceededMaxDepth

Test Plan

  • go vet ./... / go test -race ./... pass
  • Regression tests for bin/ext/container map keys and depth limit (at limit, limit+1, 5M nesting) across buffer and stream decoders
  • Decode benchmarks: no significant regression; B/op and allocs/op unchanged

Notes

  • Hashability checks are limited to ext-coded keys and struct/array key types, since reflect.Value.Comparable allocates

Untrusted MessagePack input could crash the process in two ways:

- A map key decoded as bin (or as an unhashable ext value) panicked
  with "hash of unhashable type". canSetAsMapKey now rejects
  Bin8/Bin16/Bin32, and the reflect map path checks interface keys
  before SetMapIndex. Ext-decoded keys are checked for hashability.

- Unbounded recursion on nested arrays/maps/structs caused a fatal,
  unrecoverable stack overflow. Decoding now tracks nesting depth and
  returns ErrExceededMaxDepth beyond 10000 levels.

Both the buffer and stream decoders are fixed. New errors:
ErrCanNotSetBinAsMapKey, ErrUnhashableMapKey, ErrExceededMaxDepth.

Benchmarks show no significant regression and no change in B/op or
allocs/op.
@github-actions github-actions Bot added the type: bug Confirmed or likely defect label Oct 15, 2026
@codecov

codecov Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.35294% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.79%. Comparing base (b6fde96) to head (72a11e2).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
internal/stream/decoding/decoding.go 68.96% 5 Missing and 4 partials ⚠️
internal/common/decodingutil/guard.go 72.72% 5 Missing and 1 partial ⚠️
internal/decoding/decoding.go 75.00% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #139      +/-   ##
==========================================
- Coverage   92.91%   92.79%   -0.13%     
==========================================
  Files          74       75       +1     
  Lines        7017     7131     +114     
==========================================
+ Hits         6520     6617      +97     
- Misses        311      321      +10     
- Partials      186      193       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shamaton
shamaton merged commit e96d731 into main Oct 15, 2026
27 checks passed
@shamaton
shamaton deleted the fix/decoder-dos branch October 15, 2026 14:02
@github-actions github-actions Bot mentioned this pull request Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Confirmed or likely defect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant