Skip to content

Commit d53ea9c

Browse files
authored
fix(whatsapp): avoid mentions inside unterminated inline code (#129281)
* fix(whatsapp): avoid mentions inside unterminated inline code * fix(whatsapp): protect mentions across soft code line breaks * fix(whatsapp): honor escaped multiline code delimiter runs
1 parent 8a93f68 commit d53ea9c

3 files changed

Lines changed: 72 additions & 4 deletions

File tree

‎extensions/whatsapp/src/inbound/outbound-mentions.test.ts‎

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@ describe("resolveWhatsAppOutboundMentions", () => {
1616
});
1717
});
1818

19-
it("rewrites phone-number tokens to LID mention text without device suffixes", () => {
19+
it.each([
20+
{ text: "ping @+5511976136970", expected: "ping @277038292303944" },
21+
{
22+
text: "literal \\` ping @+5511976136970",
23+
expected: "literal \\` ping @277038292303944",
24+
},
25+
{
26+
text: "literal \\\\\\` ping @+5511976136970",
27+
expected: "literal \\\\\\` ping @277038292303944",
28+
},
29+
{
30+
text: "inside ``notify ` @+5511976136970`` then @+5511976136970",
31+
expected: "inside ``notify ` @+5511976136970`` then @277038292303944",
32+
},
33+
])("rewrites visible phone-number mentions to LIDs: $text", ({ text, expected }) => {
2034
expect(
2135
resolveWhatsAppOutboundMentions({
2236
chatJid: "[email protected]",
23-
text: "ping @+5511976136970",
37+
text,
2438
participants: [
2539
{
2640
id: "277038292303944:2@lid",
@@ -29,7 +43,7 @@ describe("resolveWhatsAppOutboundMentions", () => {
2943
],
3044
}),
3145
).toEqual({
32-
text: "ping @277038292303944",
46+
text: expected,
3347
mentionedJids: ["277038292303944@lid"],
3448
});
3549
});
@@ -115,6 +129,30 @@ describe("resolveWhatsAppOutboundMentions", () => {
115129
});
116130
});
117131

132+
it.each([
133+
"Run `notify @+5511976136970",
134+
"Run `notify\n@+5511976136970",
135+
"Run ``notify ` @+5511976136970",
136+
"Run ``notify ` @+5511976136970``",
137+
"literal \\\\` inside @+5511976136970",
138+
])(
139+
"does not rewrite or mention phone numbers inside balanced or unterminated inline code: %j",
140+
(text) => {
141+
expect(
142+
resolveWhatsAppOutboundMentions({
143+
chatJid: "[email protected]",
144+
text,
145+
participants: [
146+
{
147+
id: "277038292303944@lid",
148+
phoneNumber: "[email protected]",
149+
},
150+
],
151+
}),
152+
).toEqual({ text, mentionedJids: [] });
153+
},
154+
);
155+
118156
it("does not mention numeric prefixes inside longer tokens", () => {
119157
expect(
120158
resolveWhatsAppOutboundMentions({

‎extensions/whatsapp/src/inbound/outbound-mentions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export type WhatsAppOutboundMentionResolution = {
1616
};
1717

1818
const CODE_FENCE_RE = /```[\s\S]*?```/g;
19-
const INLINE_CODE_RE = /`[^`\n]+`/g;
19+
const INLINE_CODE_RE = /(?<=(?:^|[^\\])(?:\\\\)*)(`+)[\s\S]*?(?:(?<!`)\1(?!`)|$)/g;
2020
const OUTBOUND_MENTION_RE = /@(\+?\d+)/g;
2121
const KNOWN_USER_JID_RE = /^(\d+)(?::\d+)?@(s\.whatsapp\.net|hosted|lid|hosted\.lid|c\.us)$/i;
2222
const PHONE_JID_DOMAIN_RE = /^(s\.whatsapp\.net|hosted|c\.us)$/i;

‎extensions/whatsapp/src/inbound/send-api.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { PlatformMessageNotDispatchedError } from "openclaw/plugin-sdk/error-run
1212
import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures";
1313
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
1414
import { prepareWhatsAppOutboundMedia } from "../outbound-media-contract.js";
15+
import { markdownToWhatsApp } from "../text-runtime.js";
1516
import { resolveWhatsAppOutboundMentions } from "./outbound-mentions.js";
1617
import { createWebSendApi } from "./send-api.js";
1718
import { normalizeWhatsAppSendResult, type WhatsAppSendResult } from "./send-result.js";
@@ -329,6 +330,35 @@ describe("createWebSendApi", () => {
329330
});
330331
});
331332

333+
it.each([
334+
{ messageText: "Run `notify @15551234567" },
335+
{ messageText: "Run `notify\n@15551234567" },
336+
{ messageText: "Run ``notify ` @15551234567" },
337+
{ messageText: "literal \\` ping @15551234567", nativeMention: true },
338+
{ messageText: "literal \\\\` inside @15551234567" },
339+
])(
340+
"only sends native mentions for visible phone numbers outside inline code: $messageText",
341+
async ({ messageText, nativeMention }) => {
342+
api = createWebSendApi({
343+
sock: { sendMessage, sendPresenceUpdate },
344+
defaultAccountId: "main",
345+
resolveOutboundMentions: ({ jid, text }) =>
346+
resolveWhatsAppOutboundMentions({
347+
chatJid: jid,
348+
text,
349+
participants: [{ id: "[email protected]" }],
350+
}),
351+
});
352+
353+
await api.sendMessage("[email protected]", markdownToWhatsApp(messageText));
354+
355+
expect(sendMessage).toHaveBeenCalledWith("[email protected]", {
356+
text: messageText,
357+
...(nativeMention ? { mentions: ["[email protected]"] } : {}),
358+
});
359+
},
360+
);
361+
332362
it("supports image media with caption", async () => {
333363
const payload = Buffer.from("img");
334364
await api.sendMessage("+1555", "cap", payload, "image/jpeg");

0 commit comments

Comments
 (0)