Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(media): answer 400/413 when a media URL cannot be fetched

Yudhi Armyndharis70616070d92d
diff --git a/src/common/media/load-remote-media.spec.ts b/src/common/media/load-remote-media.spec.ts
index 6bec2dbc9..fb0258717 100644
--- a/src/common/media/load-remote-media.spec.ts
+++ b/src/common/media/load-remote-media.spec.ts
@@ -1,6 +1,8 @@
import { fetch as undiciFetch } from 'undici';
import { loadRemoteMediaBuffer } from './load-remote-media';
+import { BadRequestException, PayloadTooLargeException } from '@nestjs/common';
import { SsrfBlockedError } from '../security/ssrf-guard';
+import { countsTowardSendBreaker } from '../../modules/message/send-pacing.service';
// Media download goes through undici's fetch (via the SSRF-pinning helper); mock it, not global fetch.
jest.mock('undici', () => {
@@ -55,4 +57,81 @@ describe('loadRemoteMediaBuffer', () => {
(undiciFetch as jest.Mock).mockResolvedValue(fakeResponse([1, 2, 3], { 'content-type': 'image/png' }));
await expect(loadRemoteMediaBuffer('http://8.8.8.8/x.png', undefined)).rejects.toThrow(/exceeds/i);
});
+
+ /**
+ * A URL that cannot be fetched is a fault in the caller's input. As a plain Error it left every
+ * send path as a 500 and counted toward the send-pacing breaker, so a client with a dead link
+ * could trip the breaker on a healthy session.
+ */
+ describe('maps a failed fetch to a client error', () => {
+ const failureOf = async (): Promise<unknown> =>
+ loadRemoteMediaBuffer('http://8.8.8.8/x.png', undefined).then(
+ () => undefined,
+ (error: unknown) => error,
+ );
+
+ it('answers 400 for a non-2xx response', async () => {
+ (undiciFetch as jest.Mock).mockResolvedValue({ ...fakeResponse([], {}), ok: false, status: 404 });
+ const error = await failureOf();
+ expect(error).toBeInstanceOf(BadRequestException);
+ expect((error as Error).message).toBe('Media fetch failed with status 404');
+ expect(countsTowardSendBreaker(error)).toBe(false);
+ });
+
+ it('answers 413 for a declared or streamed size over the cap', async () => {
+ process.env.MEDIA_DOWNLOAD_MAX_BYTES = '2';
+ (undiciFetch as jest.Mock).mockResolvedValue(fakeResponse([1], { 'content-length': '3' }));
+ const declared = await failureOf();
+ expect(declared).toBeInstanceOf(PayloadTooLargeException);
+ expect(countsTowardSendBreaker(declared)).toBe(false);
+
+ (undiciFetch as jest.Mock).mockResolvedValue(fakeResponse([1, 2, 3], {}));
+ expect(await failureOf()).toBeInstanceOf(PayloadTooLargeException);
+ });
+
+ it('answers 400 for a response without a body', async () => {
+ (undiciFetch as jest.Mock).mockResolvedValue({ ...fakeResponse([], {}), body: null });
+ expect(await failureOf()).toBeInstanceOf(BadRequestException);
+ });
+
+ it('answers 400 for a timeout', async () => {
+ (undiciFetch as jest.Mock).mockRejectedValue(new DOMException('aborted', 'TimeoutError'));
+ const error = await failureOf();
+ expect(error).toBeInstanceOf(BadRequestException);
+ expect((error as Error).message).toMatch(/^Media fetch timed out after \d+ ms$/);
+ });
+
+ it('answers 400 for a failed connection without echoing its cause', async () => {
+ (undiciFetch as jest.Mock).mockRejectedValue(
+ new TypeError('fetch failed', { cause: new Error('connect ECONNREFUSED 10.0.0.5:443') }),
+ );
+ const error = await failureOf();
+ expect(error).toBeInstanceOf(BadRequestException);
+ expect(JSON.stringify((error as BadRequestException).getResponse())).not.toContain('10.0.0.5');
+ expect(countsTowardSendBreaker(error)).toBe(false);
+ });
+
+ it('answers 400 for a body cut off mid-read', async () => {
+ const reader = { read: () => Promise.reject(new TypeError('terminated')), cancel: () => Promise.resolve() };
+ const response = fakeResponse([], {});
+ (undiciFetch as jest.Mock).mockResolvedValue({
+ ...response,
+ body: { ...response.body, getReader: () => reader },
+ });
+ expect(await failureOf()).toBeInstanceOf(BadRequestException);
+ });
+
+ // A malformed proxy URL or a fault in this code is a server fault: it stays a 500.
+ it('leaves any other TypeError unchanged', async () => {
+ const invalid = new TypeError('Invalid URL');
+ (undiciFetch as jest.Mock).mockRejectedValue(invalid);
+ expect(await failureOf()).toBe(invalid);
+ });
+
+ it('leaves any other error unchanged', async () => {
+ const boom = new Error('Unsupported proxy protocol: gopher:');
+ (undiciFetch as jest.Mock).mockRejectedValue(boom);
+ expect(await failureOf()).toBe(boom);
+ });
+ });
});
diff --git a/src/common/media/load-remote-media.ts b/src/common/media/load-remote-media.ts
index 2fdac30fa..59ced29b8 100644
--- a/src/common/media/load-remote-media.ts
+++ b/src/common/media/load-remote-media.ts
@@ -1,5 +1,9 @@
-import { withSafeFetch } from '../security/ssrf-guard';
+import { BadRequestException, HttpException, PayloadTooLargeException } from '@nestjs/common';
+import { SsrfBlockedError, withSafeFetch } from '../security/ssrf-guard';
import { urlFetchProxy } from '../security/proxy-dispatcher';
+import { createLogger } from '../services/logger.service';
+
+const logger = createLogger('RemoteMedia');
/** Default cap on a server-side media download: 50 MiB (overridable via MEDIA_DOWNLOAD_MAX_BYTES). */
const DEFAULT_MEDIA_MAX_BYTES = 50 * 1024 * 1024;
@@ -26,6 +30,12 @@ function positiveIntFromEnv(name: string, fallback: number): number {
*
* Engine-neutral: returns raw bytes + the response content-type, so any engine adapter can use it.
*
+ * A URL that cannot be fetched is the caller's input, not an engine fault: a non-2xx answer, a
+ * missing body, a timeout or a failed connection throw `BadRequestException`, and a body over the
+ * cap throws `PayloadTooLargeException`. As plain Errors they left every send path as a 500 and
+ * counted toward the send-pacing breaker. `SsrfBlockedError` is rethrown unchanged; each caller maps
+ * it to its own generic message.
+ *
* `sessionProxyUrl` is the egress proxy of the session the fetch is attributed to, or undefined for
* a direct one. It is required rather than optional so a new call site cannot leave a proxied
* session's fetch going direct by omission; `urlFetchProxy` applies the operator's opt-out.
@@ -46,17 +56,17 @@ export async function loadRemoteMediaBuffer(
{ signal: AbortSignal.timeout(timeoutMs) },
async response => {
if (!response.ok) {
- throw new Error(`Media fetch failed with status ${response.status}`);
+ throw new BadRequestException(`Media fetch failed with status ${response.status}`);
}
const declaredLength = Number(response.headers.get('content-length') ?? '');
if (Number.isFinite(declaredLength) && declaredLength > maxBytes) {
- throw new Error(`Media exceeds the ${maxBytes}-byte limit`);
+ throw new PayloadTooLargeException(`Media exceeds the ${maxBytes}-byte limit`);
}
const reader = response.body?.getReader();
if (!reader) {
- throw new Error('Media response has no body');
+ throw new BadRequestException('Media response has no body');
}
const chunks: Buffer[] = [];
@@ -69,7 +79,7 @@ export async function loadRemoteMediaBuffer(
total += value.byteLength;
if (total > maxBytes) {
await reader.cancel();
- throw new Error(`Media exceeds the ${maxBytes}-byte limit`);
+ throw new PayloadTooLargeException(`Media exceeds the ${maxBytes}-byte limit`);
}
chunks.push(Buffer.from(value));
}
@@ -78,5 +88,22 @@ export async function loadRemoteMediaBuffer(
return { data: Buffer.concat(chunks), mimetype };
},
{ proxyUrl },
- );
+ ).catch((error: unknown) => {
+ if (error instanceof HttpException || error instanceof SsrfBlockedError) throw error;
+ // Matched by name: the abort reason is a DOMException, which need not share this realm's Error.
+ const name = (error as { name?: unknown } | null)?.name;
+ if (name === 'TimeoutError' || name === 'AbortError') {
+ throw new BadRequestException(`Media fetch timed out after ${timeoutMs} ms`);
+ }
+ // undici reports a failed connection as TypeError('fetch failed') and a body cut off mid-read
+ // as TypeError('terminated'). Their cause can name the resolved address or the proxy, so it is
+ // logged here and only a fixed message reaches the caller. Any other TypeError (a bad proxy URL,
+ // a fault in this code) is a server fault and passes through.
+ if (error instanceof TypeError && (error.message === 'fetch failed' || error.message === 'terminated')) {
+ const cause: unknown = error.cause;
+ logger.warn('Media fetch failed', { cause: cause instanceof Error ? cause.message : error.message });
+ throw new BadRequestException('Media fetch failed');
+ }
+ throw error;
+ });
}
diff --git a/src/modules/media/media-conversion.service.spec.ts b/src/modules/media/media-conversion.service.spec.ts
index 12f01a845..e40601bb5 100644
--- a/src/modules/media/media-conversion.service.spec.ts
+++ b/src/modules/media/media-conversion.service.spec.ts
@@ -204,6 +204,20 @@ describe('MediaConversionService', () => {
});
});
+ // A fault in the fetch that is not about the caller's URL is the server's, and its message can
+ // name the session proxy; it must not come back to the client as a 400.
+ it('does not report a server-side fetch fault as a bad URL', async () => {
+ const fault = new Error('Unsupported proxy protocol: gopher://user:[email protected]');
+ jest.spyOn(loadRemoteMedia, 'loadRemoteMediaBuffer').mockRejectedValue(fault);
+ const service = makeService(config());
+
+ const failure: unknown = await service
+ .convertToVoice(SESSION, { url: 'https://example.com/note.m4a' })
+ .catch((error: unknown) => error);
+
+ expect(failure).toBe(fault);
+ });
+
it('decodes base64, stripping a data: prefix', async () => {
const service = makeService(config());
diff --git a/src/modules/media/media-conversion.service.ts b/src/modules/media/media-conversion.service.ts
index 973f47ec9..10a3d0711 100644
--- a/src/modules/media/media-conversion.service.ts
+++ b/src/modules/media/media-conversion.service.ts
@@ -1,4 +1,4 @@
-import { BadRequestException, HttpException, Injectable, ServiceUnavailableException } from '@nestjs/common';
+import { BadRequestException, Injectable, ServiceUnavailableException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectRepository } from '@nestjs/typeorm';
import { isUUID } from 'class-validator';
@@ -147,15 +147,15 @@ export class MediaConversionService {
const { data } = await loadRemoteMediaBuffer(dto.url, proxyUrl);
return data;
} catch (error) {
- // The fetch layer throws plain Errors (bad status, over the byte cap) and SsrfBlockedError,
- // none of them HttpExceptions — unmapped they leave as a 500 for what is squarely a bad
- // input, while the send path answers 400 for the very same URL. An SSRF block is reported
- // generically: its raw message names the resolved internal address.
+ // The fetch layer already answers 400/413 for a bad status, a timeout, a failed connection
+ // or a body over the cap, exactly as on the send path. An SSRF block is reported
+ // generically: its raw message names the resolved internal address. Anything else (a
+ // malformed session proxy, say) is a server fault and stays a 500, as it does on a send,
+ // rather than a 400 carrying a message that can name the proxy.
if (error instanceof SsrfBlockedError) {
throw new BadRequestException(SSRF_BLOCKED_CLIENT_MESSAGE);
}
- if (error instanceof HttpException) throw error;
- throw new BadRequestException(error instanceof Error ? error.message : String(error));
+ throw error;
}
}
throw new BadRequestException('Either url or base64 must be provided');