Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(media): keep the temp directory out of a conversion's 400 reason

Yudhi Armyndharis844a9c7fc051
diff --git a/src/modules/media/ffmpeg-run.spec.ts b/src/modules/media/ffmpeg-run.spec.ts
index b09e4589f..9a24a649c 100644
--- a/src/modules/media/ffmpeg-run.spec.ts
+++ b/src/modules/media/ffmpeg-run.spec.ts
@@ -41,10 +41,13 @@ if [ "$1" = "-version" ]; then echo "ffmpeg version stub"; exit 0; fi
mode=ok
for a in "$@"; do
case "$a" in MODE=*) mode="\${a#MODE=}" ;; esac
+ [ "$prev" = "-i" ] && in="$a"
+ prev="$a"
out="$a"
done
case "$mode" in
fail) echo "Invalid data found when processing input" >&2; exit 1 ;;
+ badinput) echo "$in: Invalid data found when processing input" >&2; exit 1 ;;
hang) sleep 10 ;;
orphan) sleep 10 & echo $! > "$0.pid"; wait ;;
empty) : > "$out" ;;
@@ -106,6 +109,14 @@ esac
});
});
+ // ffmpeg names its input by the path it was handed, which is this process's own temp directory and
+ // nothing the caller supplied, so it must not reach the reason the caller is shown.
+ it('keeps its temp directory out of the reason it reports', async () => {
+ await expect(runFfmpeg(Buffer.from('input'), 'bin', 'ogg', mode('badinput'), options())).rejects.toMatchObject({
+ detail: 'in.bin: Invalid data found when processing input',
+ });
+ });
+
// A codec stuck in a loop is the case this defends against, so the kill has to be unconditional.
it('kills a process that overruns its timeout, rather than waiting on it', async () => {
const startedAt = Date.now();
diff --git a/src/modules/media/ffmpeg.ts b/src/modules/media/ffmpeg.ts
index 88a2f6e23..a8c0f14da 100644
--- a/src/modules/media/ffmpeg.ts
+++ b/src/modules/media/ffmpeg.ts
@@ -1,7 +1,7 @@
import { spawn } from 'node:child_process';
import { mkdtemp, readFile, rm, stat, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
-import { join } from 'node:path';
+import { join, sep } from 'node:path';
/** A conversion that ffmpeg refused, timed out on, or produced too much output for. */
export class FfmpegConversionError extends Error {
@@ -170,7 +170,16 @@ export async function runFfmpeg(
const outputPath = join(dir, `out.${outputExtension}`);
try {
await writeFile(inputPath, input);
- await execute(buildFfmpegArgs(inputPath, outputPath, encodeArgs, options.maxOutputBytes), options);
+ try {
+ await execute(buildFfmpegArgs(inputPath, outputPath, encodeArgs, options.maxOutputBytes), options);
+ } catch (error) {
+ // ffmpeg names its input and output by the paths it was given. Those are this process's own temp
+ // files, so the directory is dropped and the reason names only `in.<ext>` or `out.<ext>`.
+ if (error instanceof FfmpegConversionError && error.detail) {
+ throw new FfmpegConversionError(error.message, error.detail.replaceAll(dir + sep, ''));
+ }
+ throw error;
+ }
// Check the size on disk before reading, so an unexpectedly large result is refused instead of
// being pulled into memory first. `-fs` stops ffmpeg at one byte over the cap and ffmpeg then
diff --git a/src/modules/media/media-conversion.service.ts b/src/modules/media/media-conversion.service.ts
index e5914e4db..cf5d788bd 100644
--- a/src/modules/media/media-conversion.service.ts
+++ b/src/modules/media/media-conversion.service.ts
@@ -96,7 +96,7 @@ export class MediaConversionService {
if (error instanceof FfmpegConversionError) {
// ffmpeg's stderr is about the caller's own bytes, so returning it is what makes a rejection
// actionable. It never names a path the caller did not supply: the only paths in the command
- // are the temp files this process created.
+ // are the temp files this process created, and runFfmpeg strips their directory.
this.logger.warn('Media conversion failed', { reason: error.message, detail: error.detail });
throw new BadRequestException(error.detail ? `${error.message}: ${error.detail}` : error.message);
}