Skip to content
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

FIX: Messages with IDs starting with 66 and 67 #1224

Closed
wants to merge 1 commit into from

Conversation

vphelipe
Copy link

@vphelipe vphelipe commented Jan 31, 2025

🇧🇷 🇧🇷 🇧🇷 🇧🇷 🇧🇷

Pull Request: Fixing encodeBinaryNode in Whiskeysockets

Problem Description

The encodeBinaryNode method in the Whiskeysockets library had a bug in the writeString function. The issue stemmed from an incorrect order of type checks, causing strings that should be encoded as text to be misinterpreted as hexadecimal.

Problematic Behavior

The isHex(str) check in writeString occurred before decoding as a JID or falling back to writeStringRaw(str). As a result, strings with hexadecimal-like characters were treated as hex values, producing malformed nodes.

This affected the sendMessageAck function, which generates and sends acknowledgment nodes. Malformed nodes were not recognized by recipients, resulting in infinite resends and message flow blockages.

Example received message triggering the bug:

{
  "key": {
    "remoteJid": "[email protected]",
    "fromMe": false,
    "id": "679d1b73e54738f418353c1f"
  },
  "messageTimestamp": 1738349428,
  "pushName": "Adcol  Contabilidade",
  "message": {
    "conversation": "Prezado(a)\n...",
    "messageContextInfo": {
      "deviceListMetadataVersion": 2
    }
  }
}

Malformed ACK node sent:

{
  "tag": "ack",
  "attrs": {
    "id": "679d1b73e54738f418353c1f",
    "to": "556182615929:[email protected]",
    "class": "receipt"
  }
}

Solution Implemented

The writeString function was updated to ensure:

  1. Tokens from TOKEN_MAP are prioritized.
  2. Nibble-encoded strings are processed appropriately.
  3. Strings are decoded as JIDs if possible.
  4. Non-JIDs default to raw text encoding with writeStringRaw.
  5. Hexadecimal packing is the final fallback.

Updated writeString implementation:

const writeString = (str) => {
    const tokenIndex = TOKEN_MAP[str];
    if (tokenIndex) {
        if (typeof tokenIndex.dict === 'number') {
            pushByte(TAGS.DICTIONARY_0 + tokenIndex.dict);
        }
        pushByte(tokenIndex.index);
    }
    else if (isNibble(str)) {
        writePackedBytes(str, 'nibble');
    }
    else {
        const decodedJid = (0, jid_utils_1.jidDecode)(str);
        if (decodedJid) {
            writeJid(decodedJid);
        }
        else {
            writeStringRaw(str);
        }
    }
};

Benefits

  • Correctly encodes strings resembling hexadecimal values but acting as identifiers.
  • Resolves malformed nodes in sendMessageAck.
  • Eliminates infinite ACK resend loops.

Tests Performed

Tests included:

  1. IDs starting with 66 and 67.

All tests confirmed correct ACK formatting and server acceptance without retries.

Conclusion

This PR resolves a critical encoding issue in encodeBinaryNode, ensuring reliable and efficient message handling. It eliminates malformed ACK nodes and improves library stability and communication performance.

THANKS to Renato (https://github.com/renatoiub) for providing me with numbers that send this type of message for testing.

This fix addresses an issue in the encodeBinaryNode function where messages with IDs starting with "66" or "67" were being encoded incorrectly. The root cause was an improper order of checks in the writeString function, causing valid strings to be misinterpreted as hexadecimal values. This led to malformed nodes being generated and sent repeatedly, blocking message flows.

Changes:
Reordered checks in the writeString function to ensure:
Tokens are prioritized using TOKEN_MAP.
Nibble-encoded strings are processed correctly.
Valid strings default to JID decoding or raw encoding before hex checks.
Updated logic ensures accurate handling of IDs resembling hexadecimal strings but requiring string encoding.
Impact:
Resolves infinite ACK resend loops caused by malformed nodes.
Improves reliability in processing message acknowledgments.
Enhances network efficiency by eliminating unnecessary retries.
@whiskeysockets-bot
Copy link
Contributor

Thanks for your contribution.

The next step is to wait for review and approval to merge it to main repository

The community can help reacting with a thumb up (:thumbsup:) for approval and rocket (:rocket:) for who has tested it.

To test this PR you can run the following command below:

# NPM
npm install @whiskeysockets/baileys@vphelipe/Baileysbsb#master
# YARN v2
yarn add @whiskeysockets/baileys@vphelipe/Baileysbsb#master

@T3uZ
Copy link

T3uZ commented Jan 31, 2025

functional

purpshell added a commit that referenced this pull request Jan 31, 2025
@purpshell
Copy link
Collaborator

This PR shouldn't be merged as the hex encoding is now totally useless ( if (str) creates dead code). Thanks anyways

@purpshell purpshell closed this Jan 31, 2025
@purpshell purpshell reopened this Jan 31, 2025
@purpshell purpshell closed this Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants