Repository navigation
Lock persisted enum variant indices via custom serde impls - #2026
Closed
tylerhawkes wants to merge 2 commits into
Closed
tylerhawkes wants to merge 2 commits into
tylerhawkes wants to merge 2 commits into
Conversation
`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).
The five persisted enums fixed by the previous commit (`CredentialType`, `ExtensionType`, `Extension`, `ProposalType`, `Proposal`) currently rely on the *declaration order* in source to determine their serde `variant_index`, because serde's derive macro uses `iter().enumerate()` to assign indices positionally — Rust's explicit discriminant syntax (`Basic = 1`) is ignored by the derive. That convention is fragile: a future PR that adds a variant in the middle of one of these enums silently shifts every following index and reintroduces the bincode/postcard wire-format corruption this branch just fixed. The documentation comment helps, but a one-line review miss is all it takes. Replace the derived `Serialize`/`Deserialize` impls with hand-written ones that pin each variant to an explicit `variant_index` via `serialize_unit_variant` / `serialize_newtype_variant` / `serialize_tuple_variant`. The wire bytes are byte-identical to the previous positional encoding — these impls produce the same calls the derive used to emit, just with the indices spelled out as numeric literals at the call site. The matching `Deserialize` impls use the standard `deserialize_enum` + `EnumAccess` + variant-identifier visitor pattern, accepting both u64 indices (for bincode/postcard) and variant name strings (for JSON/CBOR). The `_AppAck` placeholder on `Proposal` and `ProposalType` keeps its historical `"AppAck"` JSON name via the explicit string passed to `serialize_unit_variant`, replacing the previous `#[serde(rename = "AppAck")]` attribute on the variant. Future maintainers adding a variant to any of these enums must pick a new, never-reused index and add it to both the `Serialize` match and the variant-identifier visitor. The compiler's exhaustiveness check enforces that omissions are caught.
This was referenced May 15, 2026
Contributor
|
Closing this PR as addressed by #2034 |
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.
After #2023 the five enum variant indices are pinned by source-file order
alone, which is fragile: serde's derive macro assigns indices via
iter().enumerate(), so a future PR that inserts a variant mid-enumsilently re-breaks bincode/postcard the same way 0.7.x → 0.8.x did.
This replaces the derived
Serialize/DeserializeonCredentialType,ExtensionType,Extension,ProposalType, andProposalwithhand-written impls that pin each variant's
variant_indexas a numericliteral at the call site.
Wire bytes are byte-identical to the previous positional encoding — the
same
serialize_*_variantcalls, just with the indices spelled out — andthe compiler's exhaustiveness check on the new
Serializematch nowforces anyone adding a variant to make a deliberate choice about its
index.
Stacked on top of #2023; the diff will include those commits until #2023
lands, head commit is the only new content. Drop this one if the size
isn't worth the safety — #2023 stands without it.
Split from #2021.