Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(media): stop ffmpeg writing past the output size cap
Yudhi Armyndharisdb230e56e8fc
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex a7de3c06f..d984a55c6 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -6602,6 +6602,7 @@ file arrives.**Size note.** Both endpoints return the converted media inline, so the response is bounded by thesame `MEDIA_CONVERSION_MAX_OUTPUT_BYTES` cap (default 50 MiB) — and a client posting it onward isstill bound by `BODY_SIZE_LIMIT` (default 25 MiB) on that next request.+ffmpeg stops writing once its output passes that cap, and such a conversion fails with `400`.### 6.4.16 Automation rules (autoreply)diff --git a/src/modules/media/ffmpeg-run.spec.ts b/src/modules/media/ffmpeg-run.spec.tsindex 8e4cac8fa..c7bd02c3d 100644--- a/src/modules/media/ffmpeg-run.spec.ts+++ b/src/modules/media/ffmpeg-run.spec.ts@@ -41,6 +41,8 @@ case "$mode" inhang) sleep 30 ;;empty) : > "$out" ;;big) head -c 4096 /dev/zero > "$out" ;;+ argv) printf '%s ' "$@" > "$out" ;;+ capped) head -c 1025 /dev/zero > "$out" ;;*) printf 'converted-output' > "$out" ;;esac`,@@ -111,6 +113,20 @@ esac).rejects.toThrow(/4096 bytes, above the 1000 byte limit/);});+ // The cap has to reach the process, or the output grows unbounded until ffmpeg exits.+ it('hands ffmpeg the size cap', async () => {+ const argv = (await runFfmpeg(Buffer.from('input'), 'bin', 'ogg', mode('argv'), options())).toString();++ expect(argv).toContain('-fs 1025 ');+ });++ // Where ffmpeg stops at -fs it exits 0 with a cut-off file, which must never be returned as a result.+ it('refuses the cut-off file ffmpeg leaves when it stops at the cap', async () => {+ await expect(+ runFfmpeg(Buffer.from('input'), 'bin', 'ogg', mode('capped'), options({ maxOutputBytes: 1024 })),+ ).rejects.toThrow(/1025 bytes, above the 1024 byte limit/);+ });+// An empty file is a silent failure: it would otherwise be returned as a valid zero-byte result.it('treats an empty result as a failure', async () => {await expect(runFfmpeg(Buffer.from('input'), 'bin', 'ogg', mode('empty'), options())).rejects.toThrow(diff --git a/src/modules/media/ffmpeg.spec.ts b/src/modules/media/ffmpeg.spec.tsindex 8f050dd9f..ed6120673 100644--- a/src/modules/media/ffmpeg.spec.ts+++ b/src/modules/media/ffmpeg.spec.ts@@ -71,7 +71,22 @@ describe('ffmpeg encoder arguments', () => {* every conversion would still succeed — which is exactly why it is pinned here.*/describe('ffmpeg invocation shape', () => {- const args = buildFfmpegArgs('/tmp/openwa-convert-x/in.bin', '/tmp/openwa-convert-x/out.ogg', ['-c:a', 'libopus']);+ const args = buildFfmpegArgs(+ '/tmp/openwa-convert-x/in.bin',+ '/tmp/openwa-convert-x/out.ogg',+ ['-c:a', 'libopus'],+ 1000,+ );++ // Without it the size cap is only checked after ffmpeg exits, and until then the output can grow+ // without bound in the temp directory. It must be an output option: before -i it would apply to the+ // input, after the output path ffmpeg ignores it.+ it('caps the output size one byte above the limit, as an output option', () => {+ const at = args.indexOf('-fs');+ expect(args[at + 1]).toBe('1001');+ expect(at).toBeGreaterThan(args.indexOf('libopus'));+ expect(at).toBe(args.length - 3);+ });it('confines ffmpeg to the file protocol', () => {expect(args[args.indexOf('-protocol_whitelist') + 1]).toBe('file');diff --git a/src/modules/media/ffmpeg.ts b/src/modules/media/ffmpeg.tsindex 7f07c0322..7d0799332 100644--- a/src/modules/media/ffmpeg.ts+++ b/src/modules/media/ffmpeg.ts@@ -89,8 +89,18 @@ const BASE_ARGS = [* shape — restricted protocols, no stdin, and the fact that the only paths present are ones this* process chose — can be asserted without running a process.*/-export function buildFfmpegArgs(inputPath: string, outputPath: string, encodeArgs: string[]): string[] {- return [...BASE_ARGS, '-i', inputPath, ...encodeArgs, outputPath];+export function buildFfmpegArgs(+ inputPath: string,+ outputPath: string,+ encodeArgs: string[],+ maxOutputBytes: number,+): string[] {+ // `-fs` makes ffmpeg stop writing once the output reaches the limit, so a conversion cannot fill+ // the temp directory (a RAM-backed tmpfs in the compose files) before the size check after exit.+ // It is an output option, so it sits right before the output path. The limit is one byte above+ // the cap: a cut-off file is then always over the cap and rejected, and a complete one at the cap+ // still passes.+ return [...BASE_ARGS, '-i', inputPath, ...encodeArgs, '-fs', String(maxOutputBytes + 1), outputPath];}/**@@ -157,10 +167,11 @@ export async function runFfmpeg(const outputPath = join(dir, `out.${outputExtension}`);try {await writeFile(inputPath, input);- await execute(buildFfmpegArgs(inputPath, outputPath, encodeArgs), options);+ await execute(buildFfmpegArgs(inputPath, outputPath, encodeArgs, options.maxOutputBytes), options);// Check the size on disk before reading, so an unexpectedly large result is refused instead of- // being pulled into memory first.+ // being pulled into memory first. `-fs` stops ffmpeg at one byte over the cap and ffmpeg then+ // exits 0, so this check is also what rejects that cut-off file.const { size } = await stat(outputPath);if (size > options.maxOutputBytes) {throw new FfmpegConversionError(