-
Notifications
You must be signed in to change notification settings - Fork 314
feat: decrypt secret-encrypted message edits #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FlavioPulli
wants to merge
1
commit into
evolution-foundation:main
Choose a base branch
from
FlavioPulli:fix/secret-message-edit-decrypt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package whatsmeow_service | ||
|
|
||
| // Decryption of secret-encrypted message EDITS. | ||
| // | ||
| // When a phone edits a message in a @lid-addressed chat, WhatsApp delivers the | ||
| // edit as a `secretEncryptedMessage` (MESSAGE_EDIT): the new content is | ||
| // encrypted with the "message secret" of the ORIGINAL message — a secret this | ||
| // whatsmeow session already holds (it arrived inside the original message | ||
| // itself). Without this handling, the encrypted event is forwarded to the | ||
| // webhook as-is, so consumers learn THAT an edit happened but not WHAT changed. | ||
| // This closes the loop: decrypt with the official primitive | ||
| // (Client.DecryptSecretEncryptedMessage, which natively supports | ||
| // EncSecretMessageEdit) and rewrite the event into the CANONICAL cleartext | ||
| // shape (protocolMessage MESSAGE_EDIT + editedMessage) — the same shape | ||
| // BuildEdit produces — so webhook consumers handle both paths identically. | ||
| // | ||
| // Kept in its own file (plus a single line at the handler call-site) on | ||
| // purpose: rebasing over new evolution-go/whatsmeow releases stays trivial. | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "go.mau.fi/whatsmeow/proto/waE2E" | ||
| "go.mau.fi/whatsmeow/types/events" | ||
| "google.golang.org/protobuf/proto" | ||
| ) | ||
|
|
||
| // resolveSecretEncryptedEdit detects a secret-encrypted edit | ||
| // (secretEncryptedMessage MESSAGE_EDIT) and, when decryption succeeds, | ||
| // rewrites evt.Message into the canonical cleartext shape. Fail-open: on any | ||
| // failure (missing secret, unsupported type, …) the event passes through | ||
| // untouched — behavior falls back to what it was before this change (webhook | ||
| // receives the encrypted payload). | ||
| func (mycli *MyClient) resolveSecretEncryptedEdit(evt *events.Message) { | ||
| enc := evt.Message.GetSecretEncryptedMessage() | ||
| if enc == nil || enc.GetSecretEncType() != waE2E.SecretEncryptedMessage_MESSAGE_EDIT { | ||
| return | ||
| } | ||
| decrypted, err := mycli.WAClient.DecryptSecretEncryptedMessage(context.Background(), evt) | ||
| if err != nil { | ||
| // Without the original message's secret (e.g. the message predates this | ||
| // pairing) there is nothing to do — the consumer still receives the | ||
| // cleartext target (targetMessageKey) and can at least flag the message | ||
| // as edited. | ||
| mycli.loggerWrapper.GetLogger(mycli.userID).LogWarn("[%s] secret-edit: failed to decrypt edit of %s: %v", mycli.userID, enc.GetTargetMessageKey().GetID(), err) | ||
| return | ||
| } | ||
| mycli.loggerWrapper.GetLogger(mycli.userID).LogInfo("[%s] secret-edit: edit of %s decrypted — forwarding in cleartext", mycli.userID, enc.GetTargetMessageKey().GetID()) | ||
| // Phones encrypt the FULL edit envelope (the plaintext is already a | ||
| // protocolMessage MESSAGE_EDIT with editedMessage inside) — use it as-is; | ||
| // re-wrapping would double the nesting. The fallback covers a plaintext | ||
| // that carries only the new content. | ||
| if pm := decrypted.GetProtocolMessage(); pm != nil && | ||
| pm.GetType() == waE2E.ProtocolMessage_MESSAGE_EDIT && pm.GetEditedMessage() != nil { | ||
| evt.Message = decrypted | ||
| return | ||
| } | ||
| evt.Message = &waE2E.Message{ | ||
| ProtocolMessage: &waE2E.ProtocolMessage{ | ||
| Type: waE2E.ProtocolMessage_MESSAGE_EDIT.Enum(), | ||
| Key: enc.GetTargetMessageKey(), | ||
| EditedMessage: decrypted, | ||
| TimestampMS: proto.Int64(evt.Info.Timestamp.UnixMilli()), | ||
| }, | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Guard against a nil targetMessageKey before logging its ID
Both log lines call enc.GetTargetMessageKey().GetID() without confirming targetMessageKey is non-nil. If a payload is missing targetMessageKey, this will panic while attempting to log. Please add a nil check and either skip logging the ID or use a safe placeholder to avoid crashes on malformed inputs.