Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): scan link-preview HTML in linear time

Yudhi Armyndharis6b3d326f55b6
diff --git a/src/engine/adapters/safe-link-preview.spec.ts b/src/engine/adapters/safe-link-preview.spec.ts
index 75232d33b..c6fd8eae5 100644
--- a/src/engine/adapters/safe-link-preview.spec.ts
+++ b/src/engine/adapters/safe-link-preview.spec.ts
@@ -169,6 +169,31 @@ describe('generateSafeLinkPreview', () => {
title: '<script>',
});
});
+
+ it('reads content written before property, and a ">" inside a value', async () => {
+ respondWith('<meta content="Shop > Shoes" property="og:title"/><meta name="description" content="Size 42 > 41">');
+
+ await expect(generateSafeLinkPreview('https://example.com')).resolves.toMatchObject({
+ title: 'Shop > Shoes',
+ description: 'Size 42 > 41',
+ });
+ });
+ });
+
+ // The scan runs on the event loop, where the fetch timeout cannot interrupt it. A page of unclosed
+ // tags made the old attribute patterns backtrack from every tag to the end of the body.
+ describe('a hostile page cannot stall the process', () => {
+ it.each([
+ ['unclosed <meta tags', '<meta '],
+ ['unclosed <title tags', '<title'],
+ ])('scans a body of %s in linear time', async (_label, unit) => {
+ respondWith(unit.repeat((128 * 1024) / unit.length));
+
+ const started = Date.now();
+ await expect(generateSafeLinkPreview('https://example.com')).resolves.toBeUndefined();
+
+ expect(Date.now() - started).toBeLessThan(500);
+ });
});
describe('when there is nothing worth showing', () => {
diff --git a/src/engine/adapters/safe-link-preview.ts b/src/engine/adapters/safe-link-preview.ts
index ccac5795e..4bcc41a9b 100644
--- a/src/engine/adapters/safe-link-preview.ts
+++ b/src/engine/adapters/safe-link-preview.ts
@@ -67,16 +67,17 @@ export async function generateSafeLinkPreview(
if (!type.includes('html') && !type.includes('xml')) return undefined;
const html = await readCapped(response, maxBytes);
- const title = firstMatch(html, [
- /<meta[^>]+property=["']og:title["'][^>]+content=["']([^"']*)["']/i,
- /<meta[^>]+content=["']([^"']*)["'][^>]+property=["']og:title["']/i,
- /<title[^>]*>([^<]*)<\/title>/i,
- ]);
- const description = firstMatch(html, [
- /<meta[^>]+property=["']og:description["'][^>]+content=["']([^"']*)["']/i,
- /<meta[^>]+content=["']([^"']*)["'][^>]+property=["']og:description["']/i,
- /<meta[^>]+name=["']description["'][^>]+content=["']([^"']*)["']/i,
- ]);
+ // One pass over the <meta> tags, each read only up to the next '<', then one attribute at a
+ // time. Matching attribute pairs across the whole body backtracks from every '<meta' to the
+ // end of the text: a crafted page of unclosed tags held the event loop for minutes.
+ const metas = new Map<string, string>();
+ for (const [, attrs] of html.matchAll(/<meta\b([^<]*)/gi)) {
+ const key = (attribute(attrs, 'property') ?? attribute(attrs, 'name'))?.toLowerCase();
+ const content = attribute(attrs, 'content');
+ if (key && content && !metas.has(key)) metas.set(key, content);
+ }
+ const title = metas.get('og:title') ?? /<title\b[^<>]*>([^<]*)<\/title>/i.exec(html)?.[1]?.trim();
+ const description = metas.get('og:description') ?? metas.get('description');
// A page with neither says nothing the raw link does not, so there is no point attaching a
// preview at all.
@@ -151,12 +152,9 @@ async function readCapped(response: Response, maxBytes: number): Promise<string>
return out;
}
-function firstMatch(html: string, patterns: RegExp[]): string | undefined {
- for (const pattern of patterns) {
- const value = pattern.exec(html)?.[1]?.trim();
- if (value) return value;
- }
- return undefined;
+/** A quoted attribute's trimmed value from one tag's attribute text. */
+function attribute(attrs: string, name: 'property' | 'name' | 'content'): string | undefined {
+ return new RegExp(`\\b${name}=["']([^"']*)["']`, 'i').exec(attrs)?.[1]?.trim();
}
/** The handful of entities that actually show up in title/description text. */