fix(msgsecret): preserve caller origSender across @lid↔phone chat migration#1126
Open
dgattupalli696 wants to merge 2 commits intotulir:mainfrom
Open
fix(msgsecret): preserve caller origSender across @lid↔phone chat migration#1126dgattupalli696 wants to merge 2 commits intotulir:mainfrom
dgattupalli696 wants to merge 2 commits intotulir:mainfrom
Conversation
…ration decryptMsgSecret was unconditionally overwriting the caller's origSender with the realSender returned by GetMessageSecret. The SQL lookup resolves @lid↔@s.whatsapp.net via whatsmeow_lid_map so the key is found regardless of which form was stored, but realSender reflects the *stored* form — not necessarily the form the peer used to derive the HKDF context. Concretely: after a DM is migrated to @lid addressing, a poll secret stored pre-migration under `<phone>@s.whatsapp.net` is still looked up successfully when the vote arrives keyed on `<lid>@lid` (thanks to the CASE in getMsgSecret). But decryptMsgSecret then replaces our caller's `<lid>@lid` origSender with the stored `<phone>@s.whatsapp.net` one, feeds that into generateMsgSecretKey, and produces an HKDF context that no longer matches what the voter used — so gcmutil.Decrypt fails with 'cipher: message authentication failed'. Fix: derive with the caller-supplied origSender (which matches the form present on the echoed origMsgKey, i.e. the form the peer used). Keep a fallback path that retries with realSender so legacy stores whose keys predate LID-aware derivation continue to decrypt. Observed symptom: DecryptPollVote returning 'cipher: message authentication failed' for polls in DMs that WhatsApp has silently migrated to @lid. Signed-off-by: dgattupalli <219828309+dgattupalli696@users.noreply.github.com>
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.
Bug
DecryptPollVote(and, by extension, anydecryptMsgSecretcaller) fails withon DMs that WhatsApp has migrated to
@lidaddressing, whenever the poll's message secret was stored before the migration under the@s.whatsapp.netform of the sender.Root cause
GetMessageSecretintentionally resolves@@s.whatsapp.netviawhatsmeow_lid_mapin its SQL query (seestore/sqlstore/store.go), so the row is found regardless of which form is passed in. But it also returns the **stored** sender form asrealSender, anddecryptMsgSecretunconditionally overwrites the caller's origSender with that value:lidThe HKDF context inside
generateMsgSecretKeyis built from the sender JID string. The peer that encrypted the vote derived its key using the sender form present on the echoedi.e. the same form the caller originally had inorigSender. Overriding with a differently-representedrealSenderproduces a mismatched HKDF context and GCM auth fails.origMsgKeyFix
Keep the caller's
origSenderfor HKDF derivation (it matches the peer's derivation). If that fails, fall back to the storedrealSenderso legacy stores whose secrets predate any LID-aware behaviour still decrypt.Reproduction
@lidaddressing on the peer's side.cli.DecryptPollVote(ctx, evt)returnscipher: message authentication failed.With this patch applied, decryption succeeds.
Notes
realSenderso any existing store that relied on the overwrite behaviour continues to work.encryptMsgSecret) has the same override I haven't touched it because, for the sending side, the caller's origSender and the resolved realSender should coincide (we're about to encrypt to the peer whose form we used when storing). Happy to extend the change if a maintainer sees value.pattern