{"id":"XE-16781","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":2799,"title":"[Minor] Binary keyset codec accepts unsorted keys: a non-canonical multisig block decodes fine and re-encodes differently","area":"consensus","claimed_tier":"minor","assessed_tier":null,"verdict":"duplicate","duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-17T06:03:25.216874+00:00","updated_at":"2026-11-23T01:01:14.314183+00:00","source_created_at":null,"told_at":"2026-11-23T01:01:14.341816+00:00","status_changed_at":"2026-11-23T01:01:14.326042+00:00","url":"https://bounty.xe.network/XE-16781","body":"## Summary\r\n\r\n`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.\r\n\r\n## Root cause\r\n\r\n- `marshalKeysetCanonical` copies the keys and `sort.Strings(sorted)` before writing (`core/encoding.go:557-566`).\r\n- `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.\r\n- 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.\r\n- `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.\r\n\r\n## Impact\r\n\r\nLatent 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).\r\n\r\nExample: 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.\r\n\r\n## Verification (source-level, deterministic)\r\n\r\nAudited 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]`.\r\n\r\n## Note on related reports\r\n\r\nDistinct 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).\r\n\r\n## Suggested fix\r\n\r\nIn `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.","patch_refs":[],"thread":[{"id":19448,"source":"desk","source_ref":"reply:17125","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the\nfirst verifiable submission, so this one closes as a duplicate. If you believe it is a different\nbug, file a new report that links https://bounty.xe.network/XE-16781 and says what differs; it is read like any other.","created_at":"2026-11-23T01:01:14.314183+00:00"}]}