Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(baileys): decode character references in a link preview
Yudhi Armyndharisb90b3fca5b95
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 8e6094a27..a532e5a35 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -123,6 +123,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Read the duration of an outgoing Baileys audio file in a separate process and a voice note's waveform through ffmpeg, status voice notes included, so a crafted file can no longer abort or exhaust the gateway.- Bound `GET /api/search` to 1000 characters and 32 distinct words and run a repeated word once, so one request can no longer hold the event loop on SQLite for seconds.- Print the startup banner's API Docs URL only when Swagger is served, so a production boot no longer points at a `404`.+- Decode numeric and common named character references in a Baileys link preview's title and description, so `’` and `é` no longer reach the recipient as written.### Documentationdiff --git a/src/engine/adapters/safe-link-preview.spec.ts b/src/engine/adapters/safe-link-preview.spec.tsindex 4fcb3e899..291b5952c 100644--- a/src/engine/adapters/safe-link-preview.spec.ts+++ b/src/engine/adapters/safe-link-preview.spec.ts@@ -206,6 +206,39 @@ describe('generateSafeLinkPreview', () => {});});+ // WordPress and most CMSs write curly quotes, dashes and ellipses as numeric references, and older+ // pages write accents by name: left as written they reach the recipient as literal text.+ it('decodes numeric references, decimal and hexadecimal, and the common named ones', async () => {+ respondWith(+ '<meta property="og:title" content="Don’t panic – a guide & more">' ++ '<meta property="og:description" content="Café — 5 min read … © 2026">',+ );+ // Code points rather than the characters, so the expectation reads the same in any editor.+ const text = (...parts: Array<string | number>): string =>+ parts.map(part => (typeof part === 'number' ? String.fromCodePoint(part) : part)).join('');++ await expect(generateSafeLinkPreview('https://example.com')).resolves.toMatchObject({+ title: text('Don', 0x2019, 't panic ', 0x2013, ' a guide & more'),+ description: text('Caf', 0xe9, ' ', 0x2014, ' 5 min read ', 0x2026, ' ', 0xa9, ' 2026'),+ });+ });++ it('leaves a reference that names no character, or one it does not know, as written', async () => {+ respondWith('<meta property="og:title" content="� � � 	 – ¬anentity; &#x;">');++ await expect(generateSafeLinkPreview('https://example.com')).resolves.toMatchObject({+ title: '� � � 	 – ¬anentity; &#x;',+ });+ });++ it('does not decode a numeric reference twice', async () => {+ respondWith('<meta property="og:title" content="&#39; &#x41;">');++ await expect(generateSafeLinkPreview('https://example.com')).resolves.toMatchObject({+ title: '' A',+ });+ });+// Decoding the ampersand first would turn `&lt;` into `<`.it('does not double-decode', async () => {respondWith('<meta property="og:title" content="&lt;script&gt;">');diff --git a/src/engine/adapters/safe-link-preview.ts b/src/engine/adapters/safe-link-preview.tsindex 430831514..bb0244db7 100644--- a/src/engine/adapters/safe-link-preview.ts+++ b/src/engine/adapters/safe-link-preview.ts@@ -162,17 +162,52 @@ function attribute(attrs: string, name: 'property' | 'name' | 'content'): stringreturn (m?.[1] ?? m?.[2])?.trim();}-/** The handful of entities that actually show up in title/description text. */+/**+ * The Latin-1 named references (HTML 2), in code point order from U+00A0. `nbsp` is then replaced by a+ * plain space, as it always was here.+ */+const LATIN1_NAMES =+ 'nbsp iexcl cent pound curren yen brvbar sect uml copy ordf laquo not shy reg macr deg plusmn sup2 sup3 acute ' ++ 'micro para middot cedil sup1 ordm raquo frac14 frac12 frac34 iquest Agrave Aacute Acirc Atilde Auml Aring ' ++ 'AElig Ccedil Egrave Eacute Ecirc Euml Igrave Iacute Icirc Iuml ETH Ntilde Ograve Oacute Ocirc Otilde Ouml ' ++ 'times Oslash Ugrave Uacute Ucirc Uuml Yacute THORN szlig agrave aacute acirc atilde auml aring aelig ccedil ' ++ 'egrave eacute ecirc euml igrave iacute icirc iuml eth ntilde ograve oacute ocirc otilde ouml divide oslash ' ++ 'ugrave uacute ucirc uuml yacute thorn yuml';++/** Named references, as code points: the five markup ones, the typographic punctuation, and Latin-1. */+const NAMED_ENTITIES = new Map<string, number>([+ ...LATIN1_NAMES.split(' ').map((name, index): [string, number] => [name, 0xa0 + index]),+ ['nbsp', 0x20],+ ['quot', 0x22],+ ['amp', 0x26],+ ['apos', 0x27],+ ['lt', 0x3c],+ ['gt', 0x3e],+ ['ndash', 0x2013],+ ['mdash', 0x2014],+ ['lsquo', 0x2018],+ ['rsquo', 0x2019],+ ['ldquo', 0x201c],+ ['rdquo', 0x201d],+ ['bull', 0x2022],+ ['hellip', 0x2026],+ ['euro', 0x20ac],+ ['trade', 0x2122],+]);++/**+ * Decode character references in title/description text: decimal and hexadecimal ones generally, named+ * ones from {@link NAMED_ENTITIES}. One pass, so `&#39;` stays `'`. A reference that names no+ * character (a control, a surrogate, past U+10FFFF) or no known name is left as written.+ */function decodeEntities(value: string): string {- return (- value- .replace(/"/g, '"')- .replace(/�?39;/g, "'")- .replace(/'/g, "'")- .replace(/</g, '<')- .replace(/>/g, '>')- .replace(/ /g, ' ')- // Ampersand last: decoding it first would let `&lt;` become `<`.- .replace(/&/g, '&')+ return value.replace(+ /&(?:#(\d{1,7})|#[xX]([0-9a-fA-F]{1,6})|([A-Za-z][A-Za-z0-9]{1,31}));/g,+ (whole, decimal?: string, hex?: string, name?: string) => {+ const code = name ? NAMED_ENTITIES.get(name) : decimal ? Number(decimal) : parseInt(hex ?? '', 16);+ if (code === undefined || code < 0x20 || (code >= 0x7f && code <= 0x9f)) return whole;+ if (code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)) return whole;+ return String.fromCodePoint(code);+ },);}