Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(media): keep the gateway environment out of ffmpeg

Yudhi Armyndharise4ffbb5c2f52
diff --git a/src/modules/media/ffmpeg-run.spec.ts b/src/modules/media/ffmpeg-run.spec.ts
index 8d9b4c737..cd860a89d 100644
--- a/src/modules/media/ffmpeg-run.spec.ts
+++ b/src/modules/media/ffmpeg-run.spec.ts
@@ -79,6 +79,24 @@ esac
const convertDirs = async (): Promise<string[]> =>
(await readdir(tmpdir())).filter(name => name.startsWith('openwa-convert-'));
+ // ffmpeg decodes bytes a caller chose, on every Baileys voice note too: it gets no secret of the gateway's.
+ // Read from the spawn options: Jest sandboxes process.env, so what the child printed could not tell.
+ it('does not hand the gateway environment to ffmpeg', async () => {
+ const spawned = jest.mocked(spawn);
+ spawned.mockClear();
+ process.env.OWA_PROBE_SECRET = 'hunter2';
+ try {
+ await runFfmpeg(Buffer.from('input'), 'bin', 'ogg', [], options());
+ } finally {
+ delete process.env.OWA_PROBE_SECRET;
+ }
+
+ const env = (spawned.mock.calls[0][2] as { env?: Record<string, string> }).env;
+ expect(env).toBeDefined();
+ expect(env).not.toHaveProperty('OWA_PROBE_SECRET');
+ expect(env).toHaveProperty('PATH');
+ });
+
it('returns the bytes the process produced', async () => {
const output = await runFfmpeg(Buffer.from('input'), 'bin', 'ogg', [], options());
diff --git a/src/modules/media/ffmpeg.ts b/src/modules/media/ffmpeg.ts
index b9ddea05e..36915b6c5 100644
--- a/src/modules/media/ffmpeg.ts
+++ b/src/modules/media/ffmpeg.ts
@@ -233,6 +233,20 @@ export function killRunningConversions(): void {
}
process.on('exit', killRunningConversions);
+/**
+ * What ffmpeg sees of the gateway's environment. It parses bytes a caller chose and needs no secret, so
+ * the database and Redis passwords and the API key pepper stay out of it. What remains is what finding
+ * a binary, its libraries and a temp directory take.
+ */
+function ffmpegEnv(): NodeJS.ProcessEnv {
+ const env: NodeJS.ProcessEnv = {};
+ for (const name of ['PATH', 'LD_LIBRARY_PATH', 'DYLD_LIBRARY_PATH', 'TMPDIR', 'LANG', 'LC_ALL']) {
+ const value = process.env[name];
+ if (value !== undefined) env[name] = value;
+ }
+ return env;
+}
+
/** Spawn ffmpeg and resolve when it exits 0, else reject with whatever it wrote to stderr. */
function execute(args: string[], options: FfmpegRunOptions): Promise<void> {
return new Promise((resolve, reject) => {
@@ -240,7 +254,11 @@ function execute(args: string[], options: FfmpegRunOptions): Promise<void> {
// filename with a space or a quote is data rather than syntax.
// `detached` puts the child at the head of its own process group, so the timeout can kill
// everything under it (see below).
- const child = spawn(options.ffmpegPath, args, { stdio: ['ignore', 'ignore', 'pipe'], detached: true });
+ const child = spawn(options.ffmpegPath, args, {
+ stdio: ['ignore', 'ignore', 'pipe'],
+ detached: true,
+ env: ffmpegEnv(),
+ });
const pid = child.pid;
if (pid !== undefined) runningGroups.add(pid);