Fix for sending to WhatsApp user IDs

This commit is contained in:
Darren Clarke 2025-12-04 13:40:04 +01:00
parent b82d3cc726
commit 31eb1d92b4
19 changed files with 1023 additions and 859 deletions

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/bridge-frontend",
"version": "3.3.2",
"version": "3.3.3",
"type": "module",
"scripts": {
"dev": "next dev",

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/bridge-migrations",
"version": "3.3.2",
"version": "3.3.3",
"type": "module",
"scripts": {
"migrate:up:all": "tsx migrate.ts up:all",

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/bridge-whatsapp",
"version": "3.3.2",
"version": "3.3.3",
"main": "build/main/index.js",
"author": "Darren Clarke <darren@redaranj.com>",
"license": "AGPL-3.0-or-later",

View file

@ -26,11 +26,7 @@ export default class WhatsappService extends Service {
connections: { [key: string]: any } = {};
loginConnections: { [key: string]: any } = {};
static browserDescription: [string, string, string] = [
"Bridge",
"Chrome",
"2.0",
];
static browserDescription: [string, string, string] = ["Bridge", "Chrome", "2.0"];
constructor(server: Server, options: never) {
super(server, options);
@ -47,7 +43,7 @@ export default class WhatsappService extends Service {
}
// Prevent path traversal by checking for suspicious patterns
if (id.includes('..') || id.includes('/') || id.includes('\\')) {
if (id.includes("..") || id.includes("/") || id.includes("\\")) {
throw new Error(`Path traversal detected in bot ID: ${id}`);
}
@ -102,20 +98,14 @@ export default class WhatsappService extends Service {
auth: state,
generateHighQualityLinkPreview: false,
msgRetryCounterMap,
shouldIgnoreJid: (jid) =>
isJidBroadcast(jid) || isJidStatusBroadcast(jid),
shouldIgnoreJid: (jid) => isJidBroadcast(jid) || isJidStatusBroadcast(jid),
});
let pause = 5000;
socket.ev.process(async (events) => {
if (events["connection.update"]) {
const update = events["connection.update"];
const {
connection: connectionState,
lastDisconnect,
qr,
isNewLogin,
} = update;
const { connection: connectionState, lastDisconnect, qr, isNewLogin } = update;
if (qr) {
logger.info("got qr code");
const botDirectory = this.getBotDirectory(botID);
@ -130,8 +120,7 @@ export default class WhatsappService extends Service {
logger.info("opened connection");
} else if (connectionState === "close") {
logger.info({ lastDisconnect }, "connection closed");
const disconnectStatusCode = (lastDisconnect?.error as any)?.output
?.statusCode;
const disconnectStatusCode = (lastDisconnect?.error as any)?.output?.statusCode;
if (disconnectStatusCode === DisconnectReason.restartRequired) {
logger.info("reconnecting after got new login");
await this.createConnection(botID, server, options);
@ -174,10 +163,7 @@ export default class WhatsappService extends Service {
const verifiedFile = `${directory}/verified`;
if (fs.existsSync(verifiedFile)) {
const { version, isLatest } = await fetchLatestBaileysVersion();
logger.info(
{ version: version.join("."), isLatest },
"using WA version",
);
logger.info({ version: version.join("."), isLatest }, "using WA version");
await this.createConnection(botID, this.server, {
browser: WhatsappService.browserDescription,
@ -188,10 +174,7 @@ export default class WhatsappService extends Service {
}
}
private async queueMessage(
botID: string,
webMessageInfo: proto.IWebMessageInfo,
) {
private async queueMessage(botID: string, webMessageInfo: proto.IWebMessageInfo) {
const {
key: { id, fromMe, remoteJid },
message,
@ -204,11 +187,9 @@ export default class WhatsappService extends Service {
"Message field",
);
}
const isValidMessage =
message && remoteJid !== "status@broadcast" && !fromMe;
const isValidMessage = message && remoteJid !== "status@broadcast" && !fromMe;
if (isValidMessage) {
const { audioMessage, documentMessage, imageMessage, videoMessage } =
message;
const { audioMessage, documentMessage, imageMessage, videoMessage } = message;
const isMediaMessage =
audioMessage || documentMessage || imageMessage || videoMessage;
@ -288,10 +269,7 @@ export default class WhatsappService extends Service {
}
}
private async queueUnreadMessages(
botID: string,
messages: proto.IWebMessageInfo[],
) {
private async queueUnreadMessages(botID: string, messages: proto.IWebMessageInfo[]) {
for await (const message of messages) {
await this.queueMessage(botID, message);
}
@ -334,10 +312,7 @@ export default class WhatsappService extends Service {
}
}
async register(
botID: string,
callback?: AuthCompleteCallback,
): Promise<void> {
async register(botID: string, callback?: AuthCompleteCallback): Promise<void> {
const { version } = await fetchLatestBaileysVersion();
await this.createConnection(
botID,
@ -355,7 +330,10 @@ export default class WhatsappService extends Service {
attachments?: Array<{ data: string; filename: string; mime_type: string }>,
): Promise<void> {
const connection = this.connections[botID]?.socket;
const recipient = `${phoneNumber.replace(/\D+/g, "")}@s.whatsapp.net`;
const digits = phoneNumber.replace(/\D+/g, "");
// LIDs are 15+ digits, phone numbers with country code are typically 10-14 digits
const suffix = digits.length > 14 ? "@lid" : "@s.whatsapp.net";
const recipient = `${digits}${suffix}`;
// Send text message if provided
if (message) {
@ -368,7 +346,9 @@ export default class WhatsappService extends Service {
const MAX_TOTAL_SIZE = getMaxTotalAttachmentSize();
if (attachments.length > MAX_ATTACHMENTS) {
throw new Error(`Too many attachments: ${attachments.length} (max ${MAX_ATTACHMENTS})`);
throw new Error(
`Too many attachments: ${attachments.length} (max ${MAX_ATTACHMENTS})`,
);
}
let totalSize = 0;
@ -378,20 +358,26 @@ export default class WhatsappService extends Service {
const estimatedSize = (attachment.data.length * 3) / 4;
if (estimatedSize > MAX_ATTACHMENT_SIZE) {
logger.warn({
logger.warn(
{
filename: attachment.filename,
size: estimatedSize,
maxSize: MAX_ATTACHMENT_SIZE
}, 'Attachment exceeds size limit, skipping');
maxSize: MAX_ATTACHMENT_SIZE,
},
"Attachment exceeds size limit, skipping",
);
continue;
}
totalSize += estimatedSize;
if (totalSize > MAX_TOTAL_SIZE) {
logger.warn({
logger.warn(
{
totalSize,
maxTotalSize: MAX_TOTAL_SIZE
}, 'Total attachment size exceeds limit, skipping remaining');
maxTotalSize: MAX_TOTAL_SIZE,
},
"Total attachment size exceeds limit, skipping remaining",
);
break;
}

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/bridge-worker",
"version": "3.3.2",
"version": "3.3.3",
"type": "module",
"main": "build/main/index.js",
"author": "Darren Clarke <darren@redaranj.com>",

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/link",
"version": "3.3.2",
"version": "3.3.3",
"type": "module",
"scripts": {
"dev": "next dev -H 0.0.0.0",

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack",
"version": "3.3.2",
"version": "3.3.3",
"description": "Link from the Center for Digital Resilience",
"scripts": {
"dev": "dotenv -- turbo dev",

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/bridge-common",
"version": "3.3.2",
"version": "3.3.3",
"main": "build/main/index.js",
"type": "module",
"author": "Darren Clarke <darren@redaranj.com>",

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/bridge-ui",
"version": "3.3.2",
"version": "3.3.3",
"scripts": {
"build": "tsc -p tsconfig.json"
},

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/eslint-config",
"version": "3.3.2",
"version": "3.3.3",
"description": "amigo's eslint config",
"main": "index.js",
"author": "Abel Luck <abel@guardianproject.info>",

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/jest-config",
"version": "3.3.2",
"version": "3.3.3",
"description": "",
"main": "index.js",
"author": "Abel Luck <abel@guardianproject.info>",

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/logger",
"version": "3.3.2",
"version": "3.3.3",
"description": "Shared logging utility for Link Stack monorepo",
"main": "./dist/index.js",
"module": "./dist/index.mjs",

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/signal-api",
"version": "3.3.2",
"version": "3.3.3",
"type": "module",
"main": "build/index.js",
"exports": {

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/typescript-config",
"version": "3.3.2",
"version": "3.3.3",
"description": "Shared TypeScript config",
"license": "AGPL-3.0-or-later",
"author": "Abel Luck <abel@guardianproject.info>",

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/ui",
"version": "3.3.2",
"version": "3.3.3",
"description": "",
"scripts": {
"build": "tsc -p tsconfig.json"

View file

@ -1,7 +1,7 @@
{
"name": "@link-stack/zammad-addon-bridge",
"displayName": "Bridge",
"version": "3.3.2",
"version": "3.3.3",
"description": "An addon that adds CDR Bridge channels to Zammad.",
"scripts": {
"build": "node '../zammad-addon-common/dist/build.js'",

View file

@ -1,6 +1,6 @@
{
"name": "@link-stack/zammad-addon-common",
"version": "3.3.2",
"version": "3.3.3",
"description": "",
"bin": {
"zpm-build": "./dist/build.js",

View file

@ -1,7 +1,7 @@
{
"name": "@link-stack/zammad-addon-hardening",
"displayName": "Hardening",
"version": "3.3.2",
"version": "3.3.3",
"description": "A Zammad addon that hardens a Zammad instance according to CDR's needs.",
"scripts": {
"build": "node '../zammad-addon-common/dist/build.js'",

1768
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff