Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): follow redirects when building a link preview

Yudhi Armyndharisb9ae2260796c
diff --git a/src/engine/adapters/safe-link-preview.spec.ts b/src/engine/adapters/safe-link-preview.spec.ts
index cbcedf6e4..4fcb3e899 100644
--- a/src/engine/adapters/safe-link-preview.spec.ts
+++ b/src/engine/adapters/safe-link-preview.spec.ts
@@ -1,3 +1,5 @@
+import { createServer, type Server } from 'node:http';
+import type { AddressInfo } from 'node:net';
import { generateSafeLinkPreview } from './safe-link-preview';
import * as ssrfGuard from '../../common/security/ssrf-guard';
@@ -44,7 +46,10 @@ describe('generateSafeLinkPreview', () => {
await generateSafeLinkPreview('https://example.com/a', { sessionProxyUrl: 'socks5://proxy.invalid:1080' });
- expect((withSafeFetch.mock.calls[0] as unknown[])[3]).toEqual({ proxyUrl: 'socks5://proxy.invalid:1080' });
+ expect((withSafeFetch.mock.calls[0] as unknown[])[3]).toEqual({
+ proxyUrl: 'socks5://proxy.invalid:1080',
+ followRedirects: true,
+ });
});
it('leaves an unproxied session fetching direct', async () => {
@@ -52,7 +57,7 @@ describe('generateSafeLinkPreview', () => {
await generateSafeLinkPreview('https://example.com/a');
- expect((withSafeFetch.mock.calls[0] as unknown[])[3]).toEqual({ proxyUrl: undefined });
+ expect((withSafeFetch.mock.calls[0] as unknown[])[3]).toEqual({ proxyUrl: undefined, followRedirects: true });
});
it('fetches direct when the operator switches the session-proxy URL fetch off', async () => {
@@ -64,7 +69,7 @@ describe('generateSafeLinkPreview', () => {
delete process.env.SESSION_PROXY_URL_FETCH;
}
- expect((withSafeFetch.mock.calls[0] as unknown[])[3]).toEqual({ proxyUrl: undefined });
+ expect((withSafeFetch.mock.calls[0] as unknown[])[3]).toEqual({ proxyUrl: undefined, followRedirects: true });
});
// The guard rejects a blocked destination by throwing. That must surface as "no preview", never
@@ -108,6 +113,46 @@ describe('generateSafeLinkPreview', () => {
});
});
+ // A bare domain that answers with its www host, an http link moved to https and every short link
+ // arrive as a redirect. Each hop is checked by the guard again before it is fetched.
+ describe('follows a redirect through the guard', () => {
+ let server: Server;
+ let origin: string;
+ const allowed = process.env.SSRF_ALLOWED_HOSTS;
+
+ beforeAll(async () => {
+ server = createServer((req, res) => {
+ if (req.url === '/short') res.writeHead(301, { location: '/page' }).end();
+ // Reachable but not allowlisted: the same server under a hostname the guard refuses, so a hop
+ // followed without the guard would land on the page and produce a preview.
+ else if (req.url === '/internal')
+ res.writeHead(302, { location: `http://localhost:${(server.address() as AddressInfo).port}/page` }).end();
+ else res.writeHead(200, { 'content-type': 'text/html' }).end('<title>Landing</title>');
+ });
+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
+ origin = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
+ process.env.SSRF_ALLOWED_HOSTS = '127.0.0.1';
+ });
+
+ afterAll(async () => {
+ if (allowed === undefined) delete process.env.SSRF_ALLOWED_HOSTS;
+ else process.env.SSRF_ALLOWED_HOSTS = allowed;
+ await new Promise(resolve => server.close(resolve));
+ });
+
+ it('previews the page a redirect lands on, under the URL that was sent', async () => {
+ await expect(generateSafeLinkPreview(`${origin}/short`)).resolves.toEqual({
+ 'matched-text': `${origin}/short`,
+ 'canonical-url': `${origin}/short`,
+ title: 'Landing',
+ });
+ });
+
+ it('returns nothing when a hop points at a blocked address', async () => {
+ await expect(generateSafeLinkPreview(`${origin}/internal`)).resolves.toBeUndefined();
+ });
+ });
+
describe('what it extracts', () => {
it('prefers Open Graph metadata', async () => {
respondWith(
diff --git a/src/engine/adapters/safe-link-preview.ts b/src/engine/adapters/safe-link-preview.ts
index 45c57e90d..971247cd6 100644
--- a/src/engine/adapters/safe-link-preview.ts
+++ b/src/engine/adapters/safe-link-preview.ts
@@ -93,8 +93,9 @@ export async function generateSafeLinkPreview(
};
},
// The fetched URL comes from the message text, so it is caller-supplied the same way a media
- // URL is: on a proxied session it leaves through the session proxy (#1626).
- { proxyUrl: urlFetchProxy(opts.sessionProxyUrl) },
+ // URL is: on a proxied session it leaves through the session proxy (#1626). A bare domain, an
+ // http link and a short link all answer with a redirect; the guard vets every hop it follows.
+ { proxyUrl: urlFetchProxy(opts.sessionProxyUrl), followRedirects: true },
);
} catch {
// Blocked destination, DNS failure, timeout, malformed response — all the same to a caller who