Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(media): hand back a paced send's place on a proxy fetch failure

Yudhi Armyndharis4ad7dbabf8c7
diff --git a/src/common/media/load-remote-media.spec.ts b/src/common/media/load-remote-media.spec.ts
index fa6ead0eb..78e695360 100644
--- a/src/common/media/load-remote-media.spec.ts
+++ b/src/common/media/load-remote-media.spec.ts
@@ -2,7 +2,7 @@ import { fetch as undiciFetch } from 'undici';
import { loadRemoteMediaBuffer } from './load-remote-media';
import { BadRequestException, PayloadTooLargeException, ServiceUnavailableException } from '@nestjs/common';
import { SsrfBlockedError } from '../security/ssrf-guard';
-import { countsTowardSendBreaker } from '../../modules/message/send-pacing.service';
+import { countsTowardSendBreaker, sentNothing } 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', () => {
@@ -157,6 +157,8 @@ describe('loadRemoteMediaBuffer', () => {
const body = JSON.stringify((error as ServiceUnavailableException).getResponse());
expect(body).not.toMatch(/10\.0\.0\.5|proxy\.example|secret/);
expect(countsTowardSendBreaker(error)).toBe(false);
+ // Nothing reached WhatsApp, so a paced send hands its admission back.
+ expect(sentNothing(error)).toBe(true);
});
it('answers 503 for a timeout before any response', async () => {
@@ -164,6 +166,7 @@ describe('loadRemoteMediaBuffer', () => {
const error = await failureOf();
expect(error).toBeInstanceOf(ServiceUnavailableException);
expect(countsTowardSendBreaker(error)).toBe(false);
+ expect(sentNothing(error)).toBe(true);
});
it('keeps 400 once the target has answered', async () => {
diff --git a/src/common/media/load-remote-media.ts b/src/common/media/load-remote-media.ts
index 0fe2205e4..cd471d09f 100644
--- a/src/common/media/load-remote-media.ts
+++ b/src/common/media/load-remote-media.ts
@@ -1,9 +1,5 @@
-import {
- BadRequestException,
- HttpException,
- PayloadTooLargeException,
- ServiceUnavailableException,
-} from '@nestjs/common';
+import { BadRequestException, HttpException, PayloadTooLargeException } from '@nestjs/common';
+import { EngineNotSentError } from '../errors/engine-not-sent.error';
import { SsrfBlockedError, withSafeFetch } from '../security/ssrf-guard';
import { urlFetchProxy } from '../security/proxy-dispatcher';
import { createLogger } from '../services/logger.service';
@@ -46,7 +42,9 @@ function positiveIntFromEnv(name: string, fallback: number): number {
* instead: undici reports a proxy that refuses, cannot be resolved or fails its handshake with the
* same `fetch failed` as an unreachable target, so the two cannot be told apart, and blaming the
* caller's URL for an operator's proxy outage would send the client to fix a link that works. Once a
- * response has arrived the target was reached, and its failures stay 400/413.
+ * response has arrived the target was reached, and its failures stay 400/413. The 503 is an
+ * `EngineNotSentError`: the fetch runs before WhatsApp is asked, so a paced send gives its admission
+ * back.
*
* `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
@@ -109,7 +107,7 @@ export async function loadRemoteMediaBuffer(
const proxyHop = proxyUrl !== undefined && !responded;
if (name === 'TimeoutError' || name === 'AbortError') {
if (proxyHop) {
- throw new ServiceUnavailableException(`Media fetch through the session proxy timed out after ${timeoutMs} ms`);
+ throw new EngineNotSentError(`Media fetch through the session proxy timed out after ${timeoutMs} ms`);
}
throw new BadRequestException(`Media fetch timed out after ${timeoutMs} ms`);
}
@@ -120,7 +118,7 @@ export async function loadRemoteMediaBuffer(
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 });
- if (proxyHop) throw new ServiceUnavailableException('Media fetch through the session proxy failed');
+ if (proxyHop) throw new EngineNotSentError('Media fetch through the session proxy failed');
throw new BadRequestException('Media fetch failed');
}
throw error;