Skip to content

fix: prevent decoder crashes on unhashable map keys and deep nesting (v2) - #140

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

shamaton merged 2 commits into
v2from
fix/decoder-dos-v2

Conversation

@shamaton

Copy link
Copy Markdown
Owner

Summary

  • v2 port of the decoder crash fixes from main
  • Reject bin and unhashable ext values as interface{} map keys instead of panicking
  • Limit decode nesting depth to 10000 to prevent fatal stack overflow

Background / Motivation

Same issues as on v3: a bin map key panicked, and deeply nested input caused an unrecoverable stack overflow. The v2 decoders share the same structure, so the fix is ported with minimal adaptation.

Changes

  • Same map key validation and depth tracking as main, for both buffer and stream decoders
  • Adapted to v2's inline ext handling in setStruct
  • New errors: ErrCanNotSetBinAsMapKey, ErrUnhashableMapKey, ErrExceededMaxDepth

Test Plan

  • go vet ./... / go test -race ./... pass
  • Same regression tests as main
  • Decode benchmarks vs v2: geomean +0.8%; B/op and allocs/op unchanged

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 84.74576% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.08%. Comparing base (9212d7d) to head (61fe9d6).

Files with missing lines Patch % Lines
internal/common/decodingutil/guard.go 72.72% 5 Missing and 1 partial ⚠️
internal/decoding/decoding.go 75.00% 3 Missing and 3 partials ⚠️
internal/stream/decoding/decoding.go 79.31% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##               v2     #140      +/-   ##
==========================================
- Coverage   98.35%   98.08%   -0.27%     
==========================================
  Files          73       74       +1     
  Lines        5880     5993     +113     
==========================================
+ Hits         5783     5878      +95     
- Misses         56       67      +11     
- Partials       41       48       +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 5e534d7 into v2 Oct 15, 2026
21 checks passed
@shamaton
shamaton deleted the fix/decoder-dos-v2 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