Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(media): name the url when a conversion url is invalid
Yudhi Armyndharis2dcca5ae6d43
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex d0f8823c8..c467b48ec 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -6764,7 +6764,7 @@ including playlists and manifests, is refused with `400`.{ "base64": "T2dnUwACAAAA...", "mimetype": "audio/ogg; codecs=opus", "bytes": 14970 }```-**Errors:** `400` neither field given, a `url` that answers non-2xx, times out or cannot be reached, or ffmpeg refused the input (its reason is included) · `401` missing/invalid `X-API-Key` · `403` key lacks OPERATOR role · `413` media above the size cap · `503` conversion is disabled, the ffmpeg binary is not runnable, the conversion queue is saturated, or a `url` fetch through the session's egress proxy failed before any response, the proxy or the target at fault+**Errors:** `400` neither field given, a `url` that is not an absolute http(s) URL or answers non-2xx, times out or cannot be reached, or ffmpeg refused the input (its reason is included) · `401` missing/invalid `X-API-Key` · `403` key lacks OPERATOR role · `413` media above the size cap · `503` conversion is disabled, the ffmpeg binary is not runnable, the conversion queue is saturated, or a `url` fetch through the session's egress proxy failed before any response, the proxy or the target at fault#### POST /api/sessions/:sessionId/media/convert/video@@ -6772,7 +6772,7 @@ Convert video into an MP4 every WhatsApp client accepts: baseline H.264 with AACfitted inside 1280x720, or 720x1280 for portrait (never upscaled), frame rate capped at 30 fps, index moved to the front so playback can start before the wholefile arrives.-**Errors:** `400` neither field given, a `url` that answers non-2xx, times out or cannot be reached, or ffmpeg refused the input (its reason is included) · `401` missing/invalid `X-API-Key` · `403` key lacks OPERATOR role · `413` media above the size cap · `503` conversion is disabled, the ffmpeg binary is not runnable, the conversion queue is saturated, or a `url` fetch through the session's egress proxy failed before any response, the proxy or the target at fault+**Errors:** `400` neither field given, a `url` that is not an absolute http(s) URL or answers non-2xx, times out or cannot be reached, or ffmpeg refused the input (its reason is included) · `401` missing/invalid `X-API-Key` · `403` key lacks OPERATOR role · `413` media above the size cap · `503` conversion is disabled, the ffmpeg binary is not runnable, the conversion queue is saturated, or a `url` fetch through the session's egress proxy failed before any response, the proxy or the target at fault**Response** `200`diff --git a/openapi.json b/openapi.jsonindex 860ec7985..c326052be 100644--- a/openapi.json+++ b/openapi.json@@ -15286,7 +15286,7 @@}},"400": {- "description": "Neither url nor base64 given, the url answers non-2xx, times out or cannot be reached, or ffmpeg refused the input.",+ "description": "Neither url nor base64 given, the url is not an absolute http(s) URL or answers non-2xx, times out or cannot be reached, or ffmpeg refused the input.","content": {"application/json": {"schema": {@@ -15378,7 +15378,7 @@}},"400": {- "description": "Neither url nor base64 given, the url answers non-2xx, times out or cannot be reached, or ffmpeg refused the input.",+ "description": "Neither url nor base64 given, the url is not an absolute http(s) URL or answers non-2xx, times out or cannot be reached, or ffmpeg refused the input.","content": {"application/json": {"schema": {diff --git a/src/modules/media/dto/convert-media.dto.spec.ts b/src/modules/media/dto/convert-media.dto.spec.tsnew file mode 100644index 000000000..b3a9c338c--- /dev/null+++ b/src/modules/media/dto/convert-media.dto.spec.ts@@ -0,0 +1,40 @@+import { plainToInstance } from 'class-transformer';+import { validate } from 'class-validator';+import { ConvertMediaDto } from './convert-media.dto';+import { GLOBAL_VALIDATION_OPTIONS } from '../../../config/app-validation';++// The production pipe's own options, so these cases cannot assert a contract the running app does not apply.+const validateDto = (obj: unknown) =>+ validate(+ plainToInstance(ConvertMediaDto, obj, GLOBAL_VALIDATION_OPTIONS.transformOptions),+ GLOBAL_VALIDATION_OPTIONS,+ );++describe('ConvertMediaDto url', () => {+ it('refuses a url that is not an absolute http(s) URL, naming the field instead of the SSRF guard', async () => {+ for (const url of ['example.com/a.mp3', 'ftp://example.com/a.mp3', 'file:///etc/passwd']) {+ const errors = await validateDto({ url });+ expect(errors.map(e => e.property)).toEqual(['url']);+ expect(Object.values(errors[0].constraints ?? {})).toContain('url must be an absolute http(s) URL');+ }+ });++ it('accepts an absolute http(s) url', async () => {+ for (const url of ['https://example.com/a.mp3', 'HTTP://example.com/a.mp3', 'http://media_server:8080/a.mp3']) {+ expect(await validateDto({ url })).toHaveLength(0);+ }+ });++ it('ignores a stale url next to base64, which wins', async () => {+ expect(await validateDto({ base64: 'AAAA', url: 'junk' })).toHaveLength(0);+ });++ it('checks the url when base64 is only a data-URI prefix', async () => {+ const errors = await validateDto({ base64: 'data:audio/mpeg;base64,', url: 'cdn/a.mp3' });+ expect(errors.map(e => e.property)).toContain('url');+ });++ it('still requires one of the two fields', async () => {+ expect((await validateDto({})).map(e => e.property).sort()).toEqual(['base64', 'url']);+ });+});diff --git a/src/modules/media/dto/convert-media.dto.ts b/src/modules/media/dto/convert-media.dto.tsindex bad966543..283ae9864 100644--- a/src/modules/media/dto/convert-media.dto.ts+++ b/src/modules/media/dto/convert-media.dto.ts@@ -1,5 +1,7 @@import { IsString, IsNotEmpty, ValidateIf } from 'class-validator';import { ApiPropertyOptional } from '@nestjs/swagger';+import { IsMediaUrl } from '../../../common/media/media-url';+import { stripBase64DataUri } from '../../message/media-cap.util';/*** Media to convert, in the same url-or-base64 shape every other media endpoint accepts.@@ -16,6 +18,8 @@ export class ConvertMediaDto {@ValidateIf((dto: ConvertMediaDto) => dto.base64 === undefined || dto.url !== undefined)@IsString()@IsNotEmpty()+ // Same rule as every send DTO: base64 wins when it holds data, so a url next to it is not fetched.+ @IsMediaUrl<ConvertMediaDto>({ ignoreWhen: o => !!stripBase64DataUri(o.base64) })url?: string;@ApiPropertyOptional({diff --git a/src/modules/media/media.controller.ts b/src/modules/media/media.controller.tsindex f66803fae..2ce1e5ee4 100644--- a/src/modules/media/media.controller.ts+++ b/src/modules/media/media.controller.ts@@ -54,8 +54,8 @@ export class MediaController {@ApiResponse({status: 400,description:- 'Neither url nor base64 given, the url answers non-2xx, times out or cannot be reached, or ffmpeg ' +- 'refused the input.',+ 'Neither url nor base64 given, the url is not an absolute http(s) URL or answers non-2xx, times out or ' ++ 'cannot be reached, or ffmpeg refused the input.',})@ApiResponse({ status: 413, description: 'The supplied media is above the media size cap.' })@ApiResponse({@@ -82,8 +82,8 @@ export class MediaController {@ApiResponse({status: 400,description:- 'Neither url nor base64 given, the url answers non-2xx, times out or cannot be reached, or ffmpeg ' +- 'refused the input.',+ 'Neither url nor base64 given, the url is not an absolute http(s) URL or answers non-2xx, times out or ' ++ 'cannot be reached, or ffmpeg refused the input.',})@ApiResponse({ status: 413, description: 'The supplied media is above the media size cap.' })@ApiResponse({