Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(media): give ffmpeg HOME, a temp directory and SystemRoot

Yudhi Armyndharisefd967ce9649
diff --git a/src/modules/media/ffmpeg-run.spec.ts b/src/modules/media/ffmpeg-run.spec.ts
index cd860a89d..e137265db 100644
--- a/src/modules/media/ffmpeg-run.spec.ts
+++ b/src/modules/media/ffmpeg-run.spec.ts
@@ -97,6 +97,26 @@ esac
expect(env).toHaveProperty('PATH');
});
+ // An FFMPEG_PATH wrapper script, or a Windows host, needs a home, a temp directory or the system root.
+ it('hands ffmpeg the variables a wrapper script and a Windows process load with', async () => {
+ const spawned = jest.mocked(spawn);
+ spawned.mockClear();
+ const names = ['HOME', 'TEMP', 'TMP', 'SystemRoot'];
+ const saved = names.map(name => process.env[name]);
+ names.forEach(name => (process.env[name] = `value-of-${name}`));
+ try {
+ await runFfmpeg(Buffer.from('input'), 'bin', 'ogg', [], options());
+ } finally {
+ names.forEach((name, i) => {
+ if (saved[i] === undefined) delete process.env[name];
+ else process.env[name] = saved[i];
+ });
+ }
+
+ const env = (spawned.mock.calls[0][2] as { env?: Record<string, string> }).env;
+ for (const name of names) expect(env?.[name]).toBe(`value-of-${name}`);
+ });
+
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 36915b6c5..3ab4c175a 100644
--- a/src/modules/media/ffmpeg.ts
+++ b/src/modules/media/ffmpeg.ts
@@ -236,11 +236,23 @@ 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.
+ * a binary, its libraries, a home and a temp directory take, and the system root a Windows process
+ * cannot load its libraries without.
*/
function ffmpegEnv(): NodeJS.ProcessEnv {
const env: NodeJS.ProcessEnv = {};
- for (const name of ['PATH', 'LD_LIBRARY_PATH', 'DYLD_LIBRARY_PATH', 'TMPDIR', 'LANG', 'LC_ALL']) {
+ for (const name of [
+ 'PATH',
+ 'LD_LIBRARY_PATH',
+ 'DYLD_LIBRARY_PATH',
+ 'HOME',
+ 'TMPDIR',
+ 'TEMP',
+ 'TMP',
+ 'LANG',
+ 'LC_ALL',
+ 'SystemRoot',
+ ]) {
const value = process.env[name];
if (value !== undefined) env[name] = value;
}