fix(deps): patch baileys to restore WhatsApp device pairing (companion_reg_refresh) - #2727
Conversation
WhatsApp added a companion_reg_refresh stage to device registration (~2026-07-28). Baileys acks and discards it, so pair-success is never emitted and linking a new device is impossible. The handler is absent from rc13, rc14 and master, so no published release fixes it (WhiskeySockets/Baileys#2737, reproduced independently in whatsmeow). Uses the existing patches/ mechanism to vendor two upstream fixes until they are merged: - Baileys#2765: handle companion_reg_refresh (rotate adv secret and re-render the QR without consuming a ref) — restores pairing - Baileys#2602: guard link_code_companion_reg against the notification shape that lacks the crypto fields (Invalid buffer / 400) Also bumps baileys rc13 -> rc14 (latest published), which the patch targets. Verified on a real deployment: pairing works again and inbound messages flow end-to-end.
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRestores WhatsApp device pairing by upgrading Baileys to rc14 and applying a version-pinned patch for the new companion_reg_refresh notification and malformed/variant link-code registration payloads. Reviewers should verify patch-package installation, lockfile consistency, and compatibility with both QR and pairing-code flows; the patch should be removed once an upstream Baileys release includes these fixes. Sequence diagram for restored WhatsApp device pairingsequenceDiagram
actor User
participant WhatsApp
participant PatchedBaileys
participant Evolution
User->>Evolution: Start QR pairing
Evolution->>PatchedBaileys: Create pairing session
PatchedBaileys-->>User: Display QR
User->>WhatsApp: Scan QR
WhatsApp->>PatchedBaileys: companion_reg_refresh
PatchedBaileys->>PatchedBaileys: Rotate adv secret
PatchedBaileys-->>User: Render refreshed QR
WhatsApp->>PatchedBaileys: Pairing confirmation
PatchedBaileys-->>Evolution: pair-success
Evolution-->>User: connection.update open
alt Pairing-code flow
User->>Evolution: requestPairingCode()
Evolution->>PatchedBaileys: requestPairingCode()
WhatsApp-->>PatchedBaileys: link_code_companion_reg
PatchedBaileys-->>Evolution: Pairing code or guarded response
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've reviewed your changes and they look great!
Sourcery assessment
Needs a human reviewer. This changes the WhatsApp device-pairing authentication flow by rotating and persisting the advertising secret, then regenerating the QR code when the server retires the old secret. If the notification handling or QR refresh is wrong, legitimate pairing can fail or an existing pending pairing can become unusable; reverting the code does not restore any secret already rotated and persisted.
…change Scanning the QR code left the phone showing "Check your connection and try again". WhatsApp added a companion_reg_refresh stage to companion registration in late July 2026: the server retires the unpaired companion's adv secret and expects the client to mint a new one. Baileys acks the notification and discards it, so every QR still advertises the retired secret, pair-success never arrives, and the ref pool drains. Upstream PR WhiskeySockets/Baileys#2765 fixes it but is not merged yet, so vendor it with patch-package, taking the patch from evolution-foundation/evolution-api#2727. It also carries #2602, which guards link_code_companion_reg against the shape without crypto fields that crashes the pairing-code path with Boom('Invalid buffer', 400). The baileys version is pinned exactly because the patch is matched by version, and a caret range on a prerelease would drift to rc15 and silently stop applying. Separately, take the WA Web version from fetchLatestWaWebVersion() and fall back to fetchLatestBaileysVersion(). The bundled list has gone stale before and blocked linking that way too (Baileys#2679); both sources agree today, so this is insurance rather than a fix. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
+1 on getting this merged — hitting the exact issue this PR addresses. Self-hosted v2.3.7, new device pairing via QR fails with WhatsApp's "can't connect new devices" error since late July, while existing sessions keep working fine. This is blocking new customer onboarding for us. I can build this branch and test it against a real instance if that helps move it along — will report back with results either way. |
…7.0.0-rc14 No published Baileys release handles the notification, so the fork carries a patch, applied by patch-package on npm's postinstall (upstream develop has the same mechanism). --error-on-fail makes a patch that does not apply fail the install, CI and the Docker build alike; without it patch-package only fails on CI. The Docker builder copies patches/ before npm ci, which runs with dev dependencies and without --ignore-scripts, so patch-package is there when postinstall runs, and the final stage copies the patched node_modules. patches/baileys+7.0.0-rc14.patch is the patch vendored in evolution-foundation#2727 by Clovis Coli Jr (cloviscoli), which compiles two upstream Baileys fixes for rc14: - WhiskeySockets/Baileys#2765 by doryani-ai: on companion_reg_refresh (with a companion_reg_refresh or pair-device-rotate-qr child), mint a new 32-byte adv secret, emit it on creds.update, and re-render the ref on screen with it, reading the secret per render and spending no ref. A session with creds.me set keeps its secret: after pair-success it is the session's, and after requestPairingCode the code exchange derives its own, which a pending pair-success is verified against. - WhiskeySockets/Baileys#2602 by joivo: a link_code_companion_reg notification with no primary_identity_pub is not a primary_hello, and is skipped instead of failing with 'Invalid buffer' (Baileys evolution-foundation#2600). and adds one line of a third: - WhiskeySockets/Baileys#2749 by IamYGT: sendMessageAck reads creds.me?.id, so a notification before login is acked (the ack stanza carries no from for a notification) instead of throwing a TypeError (Baileys evolution-foundation#2738). Without it the refresh was never acked. Analysis of the protocol change: Baileys evolution-foundation#2737 by bankon1t. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s patch Credits the upstream work it carries: evolution-foundation/evolution-api evolution-foundation#2727 (the vendored patch), Baileys #2765, evolution-foundation#2602 and #2749 (the fixes it compiles), and Baileys evolution-foundation#2737 (the analysis). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Problem
Linking a new WhatsApp device is currently impossible on Evolution — QR pairing never completes and the phone shows "couldn't link device / não foi possível conectar no momento".
The root cause is upstream and not an Evolution bug: around 2026-07-28 WhatsApp added a new stage to the companion-registration flow, sending
<notification type='companion_reg_refresh'>after the QR scan. Baileys acks it and discards it, sopair-successis never emitted and the QR ref pool drains. See WhiskeySockets/Baileys#2737 — reproduced independently in whatsmeow, which confirms it affects every open client.Because
companion_reg_refreshis absent from Baileys rc13, rc14 and master, no published Baileys release fixes this. The fix only exists as unmerged PRs.This is the umbrella issue for those reports (#2696, #2679 and others here).
What this PR does
Uses the
patches/mechanism that already exists ondevelop(postinstall: patch-package, currently holding only a README) to vendor the fix until upstream merges it.package.json/package-lock.json— bumpsbaileysfrom7.0.0-rc13to7.0.0-rc14(latest published).patches/baileys+7.0.0-rc14.patch(new) — applies two upstream fixes:companion_reg_refreshby rotating the adv secret and re-rendering the QR without consuming a ref. This is what restores pairing.link_code_companion_reg: WhatsApp sends it in two shapes, and the one without the crypto fields crashes withBoom('Invalid buffer', 400)(that's #2600, which also breaks the pairing-code flow).The patch touches only 4 files (
Socket/messages-recv.js,Socket/socket.js,Utils/companion-reg-client-utils.{js,d.ts}) — no source maps, no unrelated build noise — and applies cleanly against a vanillabaileys@7.0.0-rc14.Verification
Tested on a real deployment (Evolution v2.3.7 base + this patch, Docker):
connection.updatenever reachedopen;requestPairingCode()crashed withInvalid buffer.state=open, session stable, and a real inbound WhatsApp message was delivered through the webhook end-to-end.I also posted the standalone Docker recipe in the linked comment for anyone who needs it before this lands.
Notes for maintainers
7.0.0-rc13instead if you'd rather not bump the dependency in the same PR; the patch just needs regenerating against that version.Summary by Sourcery
Restore WhatsApp device and pairing-code support by updating Baileys and applying the required upstream fixes.
Bug Fixes:
Enhancements:
Build: