fix: use AppStateSyncKeyData.fromObject instead of .create for app-state keys#2610
Open
ropic wants to merge 1 commit into
Open
fix: use AppStateSyncKeyData.fromObject instead of .create for app-state keys#2610ropic wants to merge 1 commit into
ropic wants to merge 1 commit into
Conversation
…ate keys When reading app-state-sync-key data from any auth-state provider (Prisma, Redis, files), the stored JSON is deserialized with BufferJSON.reviver which correctly restores Buffer objects. However, wrapping the result with AppStateSyncKeyData.create() does not coerce the restored Buffer fields to the Uint8Array types that the proto library expects for all downstream operations. This produces incorrect keyData in the HKDF derivation step, which in turn generates a wrong AES-256-CBC key and causes "error:1C800064:Provider routines::bad decrypt" on every attempt to decode app-state mutations (labels, archives, mutes, etc.). AppStateSyncKeyData.fromObject() performs full type conversion of all fields from a plain JS object, which is exactly what is needed after JSON deserialization with BufferJSON.reviver. The fix is extracted into a shared deserializeAppStateSyncKey helper in proto-helpers.ts to avoid duplication across the three providers. Affects all three auth-state providers: Prisma, Redis, and file-based. Tested with the Prisma provider — no app-state events fired before this fix; all events (labels.association, chats.update, etc.) fire correctly after.
Contributor
Reviewer's guide (collapsed on small PRs)Reviewer's GuideReplaces direct use of proto.Message.AppStateSyncKeyData.create with a shared helper that uses fromObject for app-state-sync-key deserialization across all auth-state providers, ensuring proper type conversion and fixing app-state decryption, while centralizing the logic in a new proto-helpers utility. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In
deserializeAppStateSyncKey, consider tightening thetypeparameter to a string literal/union (e.g.,'app-state-sync-key' | ...) andvalueto a more specific type instead ofunknownso callers get better type safety and autocompletion. - The providers currently pass raw
typestrings intodeserializeAppStateSyncKey; to avoid subtle bugs from typos in these strings, you might centralize the known key types as an enum or constant map and use that across the auth-state implementations.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `deserializeAppStateSyncKey`, consider tightening the `type` parameter to a string literal/union (e.g., `'app-state-sync-key' | ...`) and `value` to a more specific type instead of `unknown` so callers get better type safety and autocompletion.
- The providers currently pass raw `type` strings into `deserializeAppStateSyncKey`; to avoid subtle bugs from typos in these strings, you might centralize the known key types as an enum or constant map and use that across the auth-state implementations.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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.
Problem
All three auth-state providers (Prisma, Redis, files) read
app-state-sync-keydata using:This causes
error:1C800064:Provider routines::bad decryptwhenever Baileys tries to decode app-state mutations (labels, archives, mutes, etc.). The result is that no app-state events ever fire.Root cause
The write path serializes keys with
JSON.stringify(value, BufferJSON.replacer), encodingBuffer/Uint8Arrayfields as{"type":"Buffer","data":"<base64>"}. The read path restores them viaJSON.parse(stored, BufferJSON.reviver), which correctly reconstructsBufferobjects.However,
.create(value)performs no type conversion — it copies fields as-is and does not coerce a restoredBufferback to theUint8Arraythe proto field expects. This produces a structurally wrongkeyDatain the HKDF derivation step, leading to a wrong AES-256-CBC key and thebad decrypterror..fromObject(value)is designed to accept a plain JS object and properly convert all fields to their correct proto types — exactly what is needed afterBufferJSON.reviverdeserialization.Fix
Replace
.create()with.fromObject()in the three auth-state files. The logic is extracted into a shareddeserializeAppStateSyncKeyhelper insrc/utils/proto-helpers.tsto avoid duplication across providers.Files changed:
src/utils/proto-helpers.ts(new — shared helper)src/utils/use-multi-file-auth-state-prisma.tssrc/utils/use-multi-file-auth-state-provider-files.tssrc/utils/use-multi-file-auth-state-redis-db.tsVerified behavior
After this fix:
bad decrypt)regularcollection)labels.association,chats.update, and other app-state events fire correctlyTested with Evolution API v2.3.7, Baileys 7.0.0-rc.9, PostgreSQL auth-state provider.
Summary by Sourcery
Use a shared helper to correctly deserialize app-state sync keys for all auth-state providers so app-state events can be decrypted and processed.
Bug Fixes:
Enhancements: