fix(mpp): stop BodyDigest panicking on JSON-marshallable body types - #116
Open
ygd58 wants to merge 1 commit into
Open
fix(mpp): stop BodyDigest panicking on JSON-marshallable body types#116ygd58 wants to merge 1 commit into
ygd58 wants to merge 1 commit into
Conversation
toBytes only recognized []byte, string, and the exact type map[string]any; every other value — a decoded slice, a typed struct, json.RawMessage, a bare scalar — fell into the default case and panicked. server.VerifyOrChallenge calls BodyDigest.Compute on whatever the application passes as the request body, so any handler that decoded JSON into something other than map[string]any could crash the process while setting up a payment challenge. BodyDigest's exported signature (body any) already promises to accept arbitrary bodies. Route the default case through json.Marshal instead of rejecting it, matching what map[string]any already did. Add an explicit json.RawMessage case so raw pre-encoded JSON round-trips without a redundant marshal. The remaining panic path is narrowed to genuinely non-JSON- serializable Go values (chan, func, unsupported cyclic structures) — values no ordinary request-body decoding can produce, so this stays a programmer-error signal rather than something reachable from request handling. No exported signature change, so this doesn't affect existing callers of BodyDigest.Compute/Verify. Adds regression coverage for slice, struct, json.RawMessage, and bare int bodies (all previously panicking), plus a test confirming a truly unsupported type (chan) still panics. Fixes tempoxyz#95
ygd58
force-pushed
the
fix/digest-panic-json-marshallable
branch
from
August 23, 2026 14:34
68500bf to
f0bac2b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
toBytesinpkg/mpp/digest.goonly recognized[]byte,string, and the exact typemap[string]any. Every other value — a decoded slice, a typed struct,json.RawMessage, a bare scalar — fell into thedefaultcase and panicked.server.VerifyOrChallengecallsBodyDigest.Computeon whatever the application passes as the request body, so a handler that decoded JSON into anything other thanmap[string]anycould crash the process while setting up a payment challenge.Reproduces on main:
Fix
BodyDigest's exported signature (body any) already promises to accept arbitrary bodies, so this routes thedefaultcase throughjson.Marshalinstead of rejecting it — the same pathmap[string]anyalready used. Added an explicitjson.RawMessagecase so raw pre-encoded JSON round-trips without a redundant marshal.The remaining panic path is narrowed to genuinely non-JSON-serializable Go values (
chan,func, unsupported cyclic structures) — values no ordinary request-body decoding can produce, so it stays a programmer-error signal rather than something reachable from request handling.No exported signature change —
BodyDigest.Compute/Verifykeep the samefunc(body any) string/func(digest string, body any) boolshape, so this doesn't affect existing callers. This answers the issue's option 1 (accept JSON-marshallable values through the existing digest path) without the breaking API change option 2 would need.Testing
Added regression coverage in
digest_test.gofor slice, struct,json.RawMessage, and bareintbodies (all previously panicking), plus a test confirming a truly unsupported type (chan) still panics as expected.All pass, including the pre-existing map/string/bytes cases (no regression). Also ran the full
./pkg/mpp/...suite andgo vet ./pkg/mpp/...clean.Note: the module's
go.modcurrently requires Go >= 1.26, which wasn't available in my build environment (newest apt package was 1.23), so I temporarily lowered the localgodirective plus agopkg.in/yaml.v3replace to fetch test-only deps, verified everything, then reverted both before this commit — neither shows up in the diff. Happy to re-verify against 1.26 if there's a way to point me at it, or if CI just confirms directly.Fixes #95