[Minor] Binary keyset codec accepts unsorted keys: a non-canonical multisig block decodes fine and re-encodes differently
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
marshalKeysetCanonicalcopies the keys andsort.Strings(sorted)before writing (core/encoding.go:557-566).unmarshalKeysetCanonicalreadsthreshold,numKeys, then the keys verbatim intokeys[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:
unmarshalBlockcallsunmarshalKeysetCanonical(data[90:])(core/encoding.go:488) and assigns the result tob.MSKeysetwith 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
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.