Skip to content

Stabilize serde variant indices for persisted enums - #2023

Closed
tylerhawkes wants to merge 1 commit into
openmls:mainfrom
xmtp:fix-serde-variant-order
Closed

tylerhawkes wants to merge 1 commit into
openmls:mainfrom
xmtp:fix-serde-variant-order

Conversation

@tylerhawkes

@tylerhawkes tylerhawkes commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

bincode and postcard encode enum variant tags as positional u32s, so when
variants get inserted mid-declaration the index of every following variant
shifts and persisted data silently decodes as the wrong variant.

Since 0.7.x, ProposalType/Proposal, Extension/ExtensionType, and
CredentialType have all had variants added (Grease, AppEphemeral,
AppDataUpdate, AppDataDictionary) — and AppAck removed from
Proposal/ProposalType — in positions that shift the indices.

This PR restores the 0.7.x order, appends every new variant to the end,
and replaces the removed AppAck with a hidden _AppAck unit placeholder
at its original index 7. JSON/CBOR variant names, the TLS wire format,
and the public API are all unchanged.

The four enums with feature-gated variants also get a note in the
existing variant-order comment: new non-optional variants must not be
added after a #[cfg(feature = ...)] variant, because toggling the
feature would otherwise shift every following index and silently corrupt
persisted data.

This means that for non human-readable serde implementations that after a
non-optional variant is added that enabling the feature would be a breaking
change for consumers of the library.

On the _AppAck placeholder

Open to discussion. The placeholder is doing two jobs: it occupies index 7
(which no real persisted data hits — openmls never produced AppAck) and,
more importantly, it keeps SelfRemove at index 8 and Custom at index 9,
which 0.7.x bincode/postcard data does hit. Without the placeholder,
persisted Custom(N: u16) silently decodes as Grease(N) on
ProposalType, and persisted SelfRemove either fails or silently
corrupts on Proposal.

The cost is that exhaustive matches on Proposal/ProposalType in
external code need an _AppAck arm. If a tidier alternative is preferred,
marking these enums #[non_exhaustive] as a one-time API change would
let the placeholder be dropped and the variant deleted cleanly — happy
to take that direction.

This can go away by using #2026 for the serde impls which wouldn't break 0.8 users.

Split from #2021.

`ProposalType`, `Proposal`, `Extension`, `ExtensionType`, and `CredentialType`
all gained variants since openmls-0.7.1 in positions that shifted the
serde variant index. Self-describing formats (JSON, CBOR) deserialize enum
variants by name and are unaffected, but non-self-describing formats like
bincode and postcard decode the variant tag as a positional `u32`. A
persisted `Proposal::Custom`, `Proposal::SelfRemove`, `Extension::Unknown`,
`ExtensionType::Unknown`, or `CredentialType::Other` written by openmls-0.7.x
deserializes as some other variant on the current main branch, silently
corrupting state.

Restore the 0.7.x variant order and append all new variants (`Grease`,
`AppEphemeral`, `AppDataUpdate`, `AppDataDictionary`) at the end of each
enum. For the removed `AppAck` variant on `Proposal` and `ProposalType`,
introduce a hidden `_AppAck` placeholder at index 7 (with
`#[serde(rename = "AppAck")]` to preserve the JSON variant name) so
subsequent variant indices stay stable. `AppAck` was never produced by
the library so no real persisted data round-trips through this variant.

Also extends the variant-order comment on the four enums with
feature-gated variants (`ProposalType`, `Proposal`, `Extension`,
`ExtensionType`) with a note that new non-optional variants must not be
added after a `#[cfg(feature = ...)]` variant — toggling the feature
would otherwise shift every following index for non-self-describing
formats.

Match arms in `messages/codec.rs`, `messages/proposals_in.rs`, and
`group/mls_group/proposal_store.rs` are updated to cover the new
`_AppAck` variant (`unreachable!()`, since the placeholder is never
produced at runtime).
@wysiwys

wysiwys commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Closing this PR in favor of the changes in #2034

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants