Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): render a very long message body without links

Yudhi Armyndharis6ca5d0716cfb
diff --git a/dashboard/src/components/chats/MessageBody.test.ts b/dashboard/src/components/chats/MessageBody.test.ts
index 7f1107917..7c8a1ad8e 100644
--- a/dashboard/src/components/chats/MessageBody.test.ts
+++ b/dashboard/src/components/chats/MessageBody.test.ts
@@ -89,3 +89,26 @@ test('a backtick in a push name cannot pair with one in the body and hand the na
rtl.cleanup();
}
});
+
+test('a body past the link-scanning bound renders as plain text without being scanned', () => {
+ // Link detection grows with the square of an unbroken run of domain-like characters, so a long
+ // body is left unlinked instead of freezing the thread for seconds.
+ const body = `${'a.'.repeat(32_500)} https://example.com/x`;
+ const started = performance.now();
+ const { container } = rtl.render(createElement(MessageBody, { text: body }));
+ const elapsed = performance.now() - started;
+ assert.ok(elapsed < 1000, `rendering ${body.length} chars took ${Math.round(elapsed)} ms`);
+ assert.equal(container.querySelectorAll('a').length, 0);
+ assert.equal(container.textContent, body);
+});
+
+test('a body at the link-scanning bound is still linked', () => {
+ const url = 'https://example.com/x';
+ const body = `${url} ${'word '.repeat(1000)}`.slice(0, 4096);
+ assert.equal(body.length, 4096);
+ const { container } = rtl.render(createElement(MessageBody, { text: body }));
+ assert.equal(container.querySelector('a')?.getAttribute('href'), url);
+ rtl.cleanup();
+ const { container: past } = rtl.render(createElement(MessageBody, { text: `${body}x` }));
+ assert.equal(past.querySelectorAll('a').length, 0);
+});
diff --git a/dashboard/src/components/chats/MessageBody.tsx b/dashboard/src/components/chats/MessageBody.tsx
index ad31bb237..8a5571fab 100644
--- a/dashboard/src/components/chats/MessageBody.tsx
+++ b/dashboard/src/components/chats/MessageBody.tsx
@@ -22,6 +22,12 @@ const linkifyOptions = {
},
};
+/**
+ * Longest body handed to Linkify. Its scan of an unbroken run of domain-like characters grows with
+ * the square of the run, so a longer body renders without links instead of freezing the thread.
+ */
+const MAX_LINKED_LENGTH = 4096;
+
function renderNode(node: MessageNode, key: number): ReactNode {
switch (node.type) {
case 'text':
@@ -48,9 +54,8 @@ function renderNode(node: MessageNode, key: number): ReactNode {
function MessageBodyBase({ text, className, enableLinks = true }: Props) {
const nodes = parseMessageBody(text);
const rendered = <>{nodes.map(renderNode)}</>;
- return (
- <div className={className}>{enableLinks ? <Linkify options={linkifyOptions}>{rendered}</Linkify> : rendered}</div>
- );
+ const linked = enableLinks && text.length <= MAX_LINKED_LENGTH;
+ return <div className={className}>{linked ? <Linkify options={linkifyOptions}>{rendered}</Linkify> : rendered}</div>;
}
export default memo(MessageBodyBase);