Serialize pairing-code state through the complete pairing lifecycle
Summary
Concurrent requestPairingCode calls can corrupt shared pairing state before the device pairing response is processed.
src/Socket/messages-recv.ts function decipherLinkPublicKey derives its decryption key from authState.creds.pairingCode. src/Socket/socket.ts function requestPairingCode also writes creds.pairingCode and creds.me. Therefore, this state remains relevant after the companion-registration IQ resolves.
A second request can replace the first request's pairing code. When the user completes the first pairing attempt, the response can then be undecryptable.
Scope
Affected areas:
src/Socket/socket.ts:requestPairingCodeand pairing state persistence.src/Socket/messages-recv.ts:decipherLinkPublicKeyand dependent pairing-response handling.
The change must serialize the complete pending pairing lifecycle, or carry pairing code and device identity as per-request state through registration and response decryption.
Do not release a request guard when the registration IQ resolves. The pairing response can arrive later, after the user enters the pairing code.
Design consideration
Rejecting a second request while a pairing request is pending is the smaller implementation. However, if the client abandons pairing without closing the socket, that approach can block new pairing requests until a timeout.
Per-request pairing state avoids that block but requires a wider lifecycle change. Select and document the behavior for abandoned pairing attempts.
Acceptance criteria
- A second
requestPairingCodecannot overwrite state required to decrypt an earlier pairing response. - A pairing response remains decryptable after overlapping pairing-code requests are attempted.
- The implementation covers registration, delayed user completion, response decryption, success, failure, timeout, and socket teardown.
- If a pending-request guard is used, it remains active until the complete pairing lifecycle ends and has defined cleanup for timeout and socket teardown.
- Tests cover the selected concurrency behavior and a delayed pairing response.
Backlinks
- Originating pull request: https://github.com/WhiskeySockets/Baileys/pull/2769
- Review discussion: https://github.com/WhiskeySockets/Baileys/pull/2769#discussion_r3770207225
- Requested by: @jules2002guy
Source: WhiskeySockets/Baileys