Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(media): require an executable when resolving ffmpeg

Etienne Lescot2f24b7057a8f
diff --git a/electron/media/audioPeaks.test.ts b/electron/media/audioPeaks.test.ts
index fb20b2677..ba7a46679 100644
--- a/electron/media/audioPeaks.test.ts
+++ b/electron/media/audioPeaks.test.ts
@@ -1,5 +1,6 @@
// @vitest-environment node
-import { existsSync } from "node:fs";
+import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { ffmpegCandidates, peakBlockCount, resolveFfmpeg } from "./audioPeaks";
@@ -52,6 +53,68 @@ describe("ffmpeg resolution", () => {
it("returns null rather than throwing when nothing is staged", () => {
expect(resolveFfmpeg(path.join(ROOT, "does", "not", "exist"))).toBeNull();
});
+
+ /**
+ * The shape that slipped through. A Linux dev checkout can have
+ * `electron/native/bin/<tag>/ffmpeg` as a DIRECTORY of shared libraries
+ * rather than the binary; `existsSync` accepted it, resolution stopped
+ * there, and the failure only surfaced later as `spawn … EACCES`.
+ */
+ it("skips a candidate that is a directory rather than the binary", () => {
+ const here = mkdtempSync(path.join(tmpdir(), "openscreen-ffmpeg-"));
+ const tag = `${process.platform}-${process.arch}`;
+ const name = process.platform === "win32" ? "ffmpeg-shared.exe" : "ffmpeg";
+ const staged = path.join(here, "electron", "native", "bin", tag, name);
+ try {
+ // A directory sitting exactly where the executable is looked for.
+ mkdirSync(staged, { recursive: true });
+ writeFileSync(path.join(staged, "libavcodec.so.62"), "");
+
+ expect(resolveFfmpeg(here)).toBeNull();
+ } finally {
+ rmSync(here, { recursive: true, force: true });
+ }
+ });
+
+ it("accepts a candidate that is an executable file", () => {
+ const here = mkdtempSync(path.join(tmpdir(), "openscreen-ffmpeg-"));
+ const tag = `${process.platform}-${process.arch}`;
+ const name = process.platform === "win32" ? "ffmpeg-shared.exe" : "ffmpeg";
+ const staged = path.join(here, "electron", "native", "bin", tag, name);
+ try {
+ mkdirSync(path.dirname(staged), { recursive: true });
+ writeFileSync(staged, "", { mode: 0o755 });
+
+ expect(resolveFfmpeg(here)).toBe(staged);
+ } finally {
+ rmSync(here, { recursive: true, force: true });
+ }
+ });
+
+ // Non-executable files are the other half of the predicate, and the check is
+ // only meaningful where the OS enforces the bit.
+ it.runIf(process.platform !== "win32")(
+ "skips a candidate that is a file but not executable",
+ () => {
+ const here = mkdtempSync(path.join(tmpdir(), "openscreen-ffmpeg-"));
+ const staged = path.join(
+ here,
+ "electron",
+ "native",
+ "bin",
+ `${process.platform}-${process.arch}`,
+ "ffmpeg",
+ );
+ try {
+ mkdirSync(path.dirname(staged), { recursive: true });
+ writeFileSync(staged, "", { mode: 0o644 });
+
+ expect(resolveFfmpeg(here)).toBeNull();
+ } finally {
+ rmSync(here, { recursive: true, force: true });
+ }
+ },
+ );
});
// Only runs where the binary is actually staged; skipped elsewhere rather than
diff --git a/electron/media/audioPeaks.ts b/electron/media/audioPeaks.ts
index 056cab54e..762f1a0fa 100644
--- a/electron/media/audioPeaks.ts
+++ b/electron/media/audioPeaks.ts
@@ -1,6 +1,6 @@
import { spawn } from "node:child_process";
import { createHash } from "node:crypto";
-import { existsSync } from "node:fs";
+import { accessSync, constants as fsConstants, statSync } from "node:fs";
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
import path from "node:path";
import { app } from "electron";
@@ -98,10 +98,42 @@ export function ffmpegCandidates(here: string = process.cwd()): string[] {
let cachedFfmpeg: string | null | undefined;
-/** First candidate that exists, or null when none does (callers fall back). */
+/**
+ * Whether a candidate is something that can actually be run.
+ *
+ * EXISTENCE IS NOT ENOUGH, and the difference is not academic. `existsSync` was
+ * the test here, and it answers true for a DIRECTORY: on a Linux dev machine
+ * `electron/native/bin/<tag>/ffmpeg` is a folder holding the shared libraries
+ * (`libavcodec.so.62` and friends) rather than the binary, so resolution picked
+ * the folder, every later candidate was skipped, and the failure surfaced much
+ * later as `spawn … EACCES` — a message that blames permissions rather than
+ * saying the wrong candidate was chosen.
+ *
+ * Every failure mode is swallowed on purpose. A candidate that is missing,
+ * unreadable, or not executable is simply not this one; throwing out of
+ * resolution would let a single bad path deny a later, working one.
+ */
+function isExecutableFile(candidate: string): boolean {
+ try {
+ if (!statSync(candidate).isFile()) {
+ return false;
+ }
+ // A no-op for the caller on Windows, where X_OK is not enforced — but the
+ // `isFile` check above is what matters there anyway.
+ accessSync(candidate, fsConstants.X_OK);
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+/**
+ * First candidate that is an executable file, or null when none is (callers
+ * fall back).
+ */
export function resolveFfmpeg(here?: string): string | null {
if (cachedFfmpeg !== undefined && here === undefined) return cachedFfmpeg;
- const found = ffmpegCandidates(here).find((p) => existsSync(p)) ?? null;
+ const found = ffmpegCandidates(here).find(isExecutableFile) ?? null;
if (here === undefined) cachedFfmpeg = found;
return found;
}