<!-- XE-16781 · [Minor] Binary keyset codec accepts unsorted keys: a non-canonical multisig block decodes fine and re-encodes differently · testnet -->
*Markdown view of <https://bounty.xe.network/XE-16781>. The page is the record; this is the same page for reading.*

XE-16781 · Bounty

# \[Minor\] Binary keyset codec accepts unsorted keys: a non-canonical multisig block decodes fine and re-encodes differently

Closed · duplicate
—claimed Minor
web
filed 2026-11-17 06:03 UTC
by Irwan0000

Verdict
:   Duplicate

Tier
:   —

Claimed
:   Minor

Area
:   Consensus

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-23 01:01 UTC

Told
:   2026-11-23 01:01 UTC

### Patches

None linked.

### History

- [784491](https://bounty.xe.network/log/784491) 2026-11-23 01:01 UTC proposal.adopted
- [784490](https://bounty.xe.network/log/784490) 2026-11-23 01:01 UTC reply.sent
- [784489](https://bounty.xe.network/log/784489) 2026-11-23 01:01 UTC ticket.status new → closed
- [783524](https://bounty.xe.network/log/783524) 2026-11-23 01:00 UTC proposal.adopted
- [783523](https://bounty.xe.network/log/783523) 2026-11-23 01:00 UTC proposal.adopted
- [783522](https://bounty.xe.network/log/783522) 2026-11-23 01:00 UTC ticket.facts
- [783521](https://bounty.xe.network/log/783521) 2026-11-23 01:00 UTC proposal.adopted
- [783520](https://bounty.xe.network/log/783520) 2026-11-23 01:00 UTC ticket.facts
- [782293](https://bounty.xe.network/log/782293) 2026-11-23 00:59 UTC note.added
- [782292](https://bounty.xe.network/log/782292) 2026-11-23 00:59 UTC proposal.created
- [782291](https://bounty.xe.network/log/782291) 2026-11-23 00:59 UTC proposal.created
- [782290](https://bounty.xe.network/log/782290) 2026-11-23 00:59 UTC proposal.created

[Full history](https://bounty.xe.network/log?ref=XE-16781) · [JSON](https://bounty.xe.network/v1/tickets/XE-16781) · [markdown](https://bounty.xe.network/XE-16781.md)

### Report

## Summary

`unmarshalKeysetCanonical` (`core/encoding.go:583-600`) reads the keyset keys in wire order with no sortedness check, while `marshalKeysetCanonical` (`core/encoding.go:557-581`) sorts the keys before writing. A multisig block whose keyset keys are stored in unsorted order therefore decodes successfully and re-marshals to a *different* byte string — two distinct byte strings decode to the same block, breaking decode → encode round-trip parity for the binary codec. `unmarshalBlock` never calls `ValidateKeyset`, and `ValidateKeyset` itself does not check key order either, so nothing on the decode path rejects the non-canonical spelling.

## Root cause

- `marshalKeysetCanonical` copies the keys and `sort.Strings(sorted)` before writing (`core/encoding.go:557-566`).
- `unmarshalKeysetCanonical` reads `threshold`, `numKeys`, then the keys verbatim into `keys[i]` (`core/encoding.go:583-600`) and returns them as-is — no check that the wire order is sorted.
- The multisig decode path uses it directly: `unmarshalBlock` calls `unmarshalKeysetCanonical(data[90:])` (`core/encoding.go:488`) and assigns the result to `b.MSKeyset` with no further validation.
- `ValidateKeyset` (`core/multisig.go:35-63`) checks key count, threshold bounds, 64-hex-char shape and duplicates — but never key order. Even if the decode path called it, the unsorted spelling would still pass.

## Impact

Latent at HEAD: the binary codec has no callers outside `core/encoding.go` (JSON is the wire/store format), so there is no attacker-reachable trigger today. It breaks byte-level identity for any future binary consumer, debug tool, or port that assumes decode → encode round-trip parity — the same reasoning under which XE-0100 (PoWNonce drop on round-trip) and XE-0563 (DecodeVote panic) were accepted, and the same class as XE-16737 (trailing garbage / non-canonical final byte, filed as Minor).

Example: a 2-of-2 keyset with keys `[K2, K1]` (K2 \> K1 lexicographically) decodes to `Keyset{Keys: [K2, K1]}` and re-marshals as `[K1, K2]` — the re-encoded bytes differ from the input bytes while denoting the same keyset.

## Verification (source-level, deterministic)

Audited at HEAD `8cc209d` (testnet-0006 build). The asymmetry is visible directly in the two functions quoted above: marshal sorts, unmarshal does not check. No test in the repo covers unsorted keyset decoding. The round-trip divergence follows from the code: decode `[K2, K1]` → `Keyset{Keys: [K2, K1]}` → marshal → `[K1, K2]`.

## Note on related reports

Distinct from: XE-0531 (unvalidated threshold in the decoder); XE-0474 / XE-7562 / XE-8656 / XE-9596 (mixed-case sort divergences in Python copies); XE-13668 (threshold truncation to uint32); XE-0945 / XE-16737 (trailing garbage / non-canonical final byte — same non-canonical-acceptance class, different decoder check); XE-0533 / XE-7178 (unvalidated threshold / duplicate keys in the decoder; order is the one check none of those cover).

## Suggested fix

In `unmarshalKeysetCanonical`, reject the input unless the keys arrive in strictly sorted order (the order `marshalKeysetCanonical` writes), or alternatively sort on decode and document that the decoder canonicalises. Either restores decode → encode parity.

### Thread

**Joseph Denne**team2026-11-23 01:01 UTCdesk

Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the
first verifiable submission, so this one closes as a duplicate. If you believe it is a different
bug, file a new report that links https://bounty.xe.network/XE-16781 and says what differs; it is read like any other.
