Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(media): restrict conversion input to single-file containers
Yudhi Armyndharis557ddcdddbf1
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 1059c9d7b..3eeb7f971 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -6497,7 +6497,10 @@ tuned for speech. Post the returned `base64` to `send-audio` with `ptt: true`.| base64 | string | Inline bytes. Takes precedence when both are given |Exactly one of `url` / `base64` is required. No `mimetype` is accepted: the input format is-identified from the bytes.+identified from the bytes. Both conversion routes accept single-file media inputs only: MP4, M4A,+3GP and MOV, Matroska and WebM, Ogg, MP3, WAV and W64, AAC, AC-3 and E-AC-3, FLAC, WavPack, AU,+AMR, AVI, MPEG-PS and MPEG-TS, ASF, FLV, CAF, AIFF, GIF, and raw H.264 and HEVC. Anything else,+including playlists and manifests, is refused with `400`.**Response** `200`diff --git a/src/modules/media/ffmpeg.spec.ts b/src/modules/media/ffmpeg.spec.tsindex bf583ed57..8f050dd9f 100644--- a/src/modules/media/ffmpeg.spec.ts+++ b/src/modules/media/ffmpeg.spec.ts@@ -77,6 +77,22 @@ describe('ffmpeg invocation shape', () => {expect(args[args.indexOf('-protocol_whitelist') + 1]).toBe('file');});+ // The file protocol still reaches local files, and playlist or manifest demuxers open the files+ // their input names. Only single-file containers may decode the input, and the whitelist has to be+ // an input option, so it must precede -i.+ it('restricts the input demuxers to single-file media containers', () => {+ const at = args.indexOf('-format_whitelist');+ expect(at).toBeGreaterThan(-1);+ expect(at).toBeLessThan(args.indexOf('-i'));+ const formats = args[at + 1].split(',');+ expect(formats).toEqual(+ expect.arrayContaining(['mov', 'matroska', 'ogg', 'mp3', 'wav', 'gif', 'mpeg', 'ac3', 'eac3']),+ );+ for (const referencing of ['hls', 'dash', 'concat', 'image2', 'segment', 'tee', 'lavfi']) {+ expect(formats).not.toContain(referencing);+ }+ });+it('never waits on stdin, and never prompts about an existing output', () => {expect(args).toContain('-nostdin');expect(args).toContain('-y');diff --git a/src/modules/media/ffmpeg.ts b/src/modules/media/ffmpeg.tsindex cc13092c7..7f07c0322 100644--- a/src/modules/media/ffmpeg.ts+++ b/src/modules/media/ffmpeg.ts@@ -24,19 +24,65 @@ export interface FfmpegRunOptions {maxOutputBytes: number;}+/**+ * Input demuxers conversion accepts, by ffmpeg name. A name matches any demuxer that registers it,+ * so `mov` covers mp4, m4a and 3gp, `matroska` covers webm, and `mpeg` is MPEG-PS (.mpg, .vob).+ * Every one of them reads its input and nothing else.+ */+export const INPUT_FORMATS = [+ 'mov',+ 'matroska',+ 'ogg',+ 'mp3',+ 'wav',+ 'w64',+ 'aac',+ 'ac3',+ 'eac3',+ 'flac',+ 'wv',+ 'au',+ 'amr',+ 'avi',+ 'mpeg',+ 'mpegts',+ 'asf',+ 'flv',+ 'caf',+ 'aiff',+ 'gif',+ 'h264',+ 'hevc',+].join(',');+/*** The arguments every conversion starts with, before any codec choice.*- * `-protocol_whitelist file` is the load-bearing one. ffmpeg treats its input as a URL, and given an- * `http://` one it will happily make the request — verified against this project's own image, where- * it reached for the link-local metadata address. The input here is always a file this process just- * wrote, so restricting ffmpeg to the file protocol costs nothing and removes the whole class:- * neither the input path nor anything a crafted container might reference can become a request.+ * `-protocol_whitelist file` is the load-bearing one for the network. ffmpeg treats its input as a URL,+ * and given an `http://` one it will happily make the request (verified against this project's own+ * image, where it reached for the link-local metadata address). The input here is always a file this+ * process just wrote, so restricting ffmpeg to the file protocol costs nothing, and neither the input+ * path nor anything a crafted container might reference can become a network request.+ *+ * The file protocol still reaches every local file, though, and some demuxers (playlists, manifests,+ * concatenation scripts) open the files their input names. `-format_whitelist` restricts the input+ * demuxers to the single-file media containers conversion supports, so the input is decoded as+ * itself and never as a pointer to something else on disk.** `-nostdin` stops a prompt (an existing output file, a missing codec) from blocking forever on a* stdin nobody is attached to, and `-y` means there is nothing to prompt about in the first place.*/-const BASE_ARGS = ['-hide_banner', '-nostdin', '-loglevel', 'error', '-y', '-protocol_whitelist', 'file'] as const;+const BASE_ARGS = [+ '-hide_banner',+ '-nostdin',+ '-loglevel',+ 'error',+ '-y',+ '-protocol_whitelist',+ 'file',+ '-format_whitelist',+ INPUT_FORMATS,+] as const;/*** The full argument list for one conversion. Split out from the spawn so the security-relevant