Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): keep quotes inside link-preview meta values

Yudhi Armyndharisfe6c21cf9878
diff --git a/src/engine/adapters/safe-link-preview.spec.ts b/src/engine/adapters/safe-link-preview.spec.ts
index c6fd8eae5..cbcedf6e4 100644
--- a/src/engine/adapters/safe-link-preview.spec.ts
+++ b/src/engine/adapters/safe-link-preview.spec.ts
@@ -178,6 +178,15 @@ describe('generateSafeLinkPreview', () => {
description: 'Size 42 > 41',
});
});
+
+ it('keeps a quote of the other kind inside a value', async () => {
+ respondWith(`<meta property="og:title" content="Don't Panic"><meta name='description' content='say "hi"'>`);
+
+ await expect(generateSafeLinkPreview('https://example.com')).resolves.toMatchObject({
+ title: "Don't Panic",
+ description: 'say "hi"',
+ });
+ });
});
// The scan runs on the event loop, where the fetch timeout cannot interrupt it. A page of unclosed
diff --git a/src/engine/adapters/safe-link-preview.ts b/src/engine/adapters/safe-link-preview.ts
index 4bcc41a9b..45c57e90d 100644
--- a/src/engine/adapters/safe-link-preview.ts
+++ b/src/engine/adapters/safe-link-preview.ts
@@ -152,9 +152,13 @@ async function readCapped(response: Response, maxBytes: number): Promise<string>
return out;
}
-/** A quoted attribute's trimmed value from one tag's attribute text. */
+/**
+ * A quoted attribute's trimmed value from one tag's attribute text. The value ends at the quote that
+ * opened it, so an apostrophe inside a double-quoted title stays part of it.
+ */
function attribute(attrs: string, name: 'property' | 'name' | 'content'): string | undefined {
- return new RegExp(`\\b${name}=["']([^"']*)["']`, 'i').exec(attrs)?.[1]?.trim();
+ const m = new RegExp(`\\b${name}=(?:"([^"]*)"|'([^']*)')`, 'i').exec(attrs);
+ return (m?.[1] ?? m?.[2])?.trim();
}
/** The handful of entities that actually show up in title/description text. */