Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep a push name from escaping its mention span

Yudhi Armyndharis05c34347a699
diff --git a/dashboard/src/components/chats/MessageBody.test.ts b/dashboard/src/components/chats/MessageBody.test.ts
index f536ad089..0edb9c649 100644
--- a/dashboard/src/components/chats/MessageBody.test.ts
+++ b/dashboard/src/components/chats/MessageBody.test.ts
@@ -9,6 +9,7 @@ import { test, before, afterEach } from 'node:test';
import assert from 'node:assert/strict';
import { createElement } from 'react';
import { MENTION_CLOSE, MENTION_OPEN } from '../../utils/messageFormatter.ts';
+import { buildMentionNameMap, resolveMentions } from '../../utils/chatMessages.ts';
let rtl: typeof import('@testing-library/react');
let MessageBody: (typeof import('./MessageBody.tsx'))['default'];
@@ -52,3 +53,26 @@ test('an unresolved mention (no matching participant) is still plain "@digits" t
assert.equal(container.querySelectorAll('a').length, 0, 'digits alone are not a URL');
assert.ok(container.textContent?.includes('@166868170059932'));
});
+
+test('a push name that smuggles the closing delimiter still renders as one <bdi> with no anchor', () => {
+ const names = buildMentionNameMap([
+ { author: '[email protected]', chatName: `X${MENTION_CLOSE}bit.ly/free` } as Parameters<
+ typeof buildMentionNameMap
+ >[0][number],
+ ]);
+ const { container } = rtl.render(
+ createElement(MessageBody, { text: resolveMentions('hi @6281112345 there', names) }),
+ );
+ assert.equal(container.querySelectorAll('a').length, 0);
+ assert.equal(container.querySelectorAll('bdi').length, 1);
+ assert.equal(container.querySelector('bdi')?.textContent, '@Xbit.ly/free');
+});
+
+test('a mention inside *bold* renders bold with the <bdi> as a child, not literal asterisks', () => {
+ const names = buildMentionNameMap([{ author: '[email protected]', chatName: 'Ravi' }]);
+ const { container } = rtl.render(
+ createElement(MessageBody, { text: resolveMentions('*Reminder @6281112345 at 10*', names) }),
+ );
+ assert.equal(container.querySelector('strong')?.textContent, 'Reminder @Ravi at 10');
+ assert.ok(container.querySelector('strong bdi'), 'the mention sits inside the bold element');
+});
diff --git a/dashboard/src/utils/chatMessages.test.ts b/dashboard/src/utils/chatMessages.test.ts
index b7da11600..43e988444 100644
--- a/dashboard/src/utils/chatMessages.test.ts
+++ b/dashboard/src/utils/chatMessages.test.ts
@@ -581,3 +581,31 @@ test('resolveMentions does not fire right after a closing backtick', () => {
const names = buildMentionNameMap([msg({ author: '[email protected]', chatName: 'Ravi' })]);
assert.equal(resolveMentions('`x`@12345678', names), '`x`@12345678');
});
+
+test('a push name made only of other invisible code points is blank too, not just the Hangul filler', () => {
+ for (const cp of [
+ '\u200B',
+ '\u00AD',
+ '\u2060',
+ '\u200E',
+ '\u034F',
+ '\u115F',
+ '\u1160',
+ '\uFFA0',
+ '\u2800',
+ '\u180E',
+ ]) {
+ const names = buildMentionNameMap([msg({ author: '[email protected]', chatName: cp + cp })]);
+ assert.equal(names.size, 0, `U+${cp.codePointAt(0)?.toString(16)} should count as blank`);
+ }
+ // A visible character among them keeps the name usable.
+ const names = buildMentionNameMap([msg({ author: '[email protected]', chatName: '\u200BRavi\u200B' })]);
+ assert.equal(resolveMentions('@6281112345', names), wrap('@\u200BRavi\u200B'));
+});
+
+test('a push name carrying the span delimiters cannot close its own mention early', () => {
+ const names = buildMentionNameMap([
+ msg({ author: '[email protected]', chatName: `X${MENTION_CLOSE}bit.ly/free ${MENTION_OPEN}Y` }),
+ ]);
+ assert.equal(resolveMentions('hi @6281112345', names), `hi ${wrap('@Xbit.ly/free')}`);
+});
diff --git a/dashboard/src/utils/chatMessages.ts b/dashboard/src/utils/chatMessages.ts
index 427c6a614..e00f34058 100644
--- a/dashboard/src/utils/chatMessages.ts
+++ b/dashboard/src/utils/chatMessages.ts
@@ -125,19 +125,24 @@ export function buildMentionNameMap(messages: Pick<ChatMessage, 'author' | 'chat
for (const m of messages) {
if (!m.author || !m.chatName) continue;
const local = m.author.split('@')[0].split(':')[0];
- const name = blankMentionName(m.chatName) ? '' : m.chatName.trim();
+ // A push name is sender-controlled: strip the span delimiters so a name cannot close its own
+ // mention early and hand its tail back to Linkify and the format parser.
+ const name = m.chatName.replace(MENTION_DELIMITERS, '').trim();
// Rows are ascending by time, so the last write is the participant's current push name.
- if (name && /^\d+$/.test(local)) map.set(local, name);
+ if (name && !blankMentionName(name) && /^\d+$/.test(local)) map.set(local, name);
}
return map;
}
-// U+3164 HANGUL FILLER renders as nothing but is not Unicode whitespace, so `.trim()` leaves it
-// behind — a push name of only fillers passed the old blank check and rendered a bare "@".
-const HANGUL_FILLER = /ㅤ/g;
+const MENTION_DELIMITERS = new RegExp(`[${MENTION_OPEN}${MENTION_CLOSE}]`, 'g');
+
+// Characters that render as nothing yet survive `.trim()`: format controls (zero-width space, soft
+// hyphen, word joiner, bidi marks), combining marks, the Hangul fillers and the blank braille
+// pattern. A push name made only of these would render as a bare "@".
+const INVISIBLE = /[\p{Cf}\p{M}\u115F\u1160\u3164\uFFA0\u2800]/gu;
function blankMentionName(name: string): boolean {
- return name.replace(HANGUL_FILLER, '').trim() === '';
+ return name.replace(INVISIBLE, '').trim() === '';
}
// The left boundary is the start of the text, whitespace, opening punctuation, or a format opener