mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(skills): locate.mjs reads the system font in place, not a shared /tmp copy (#8042)
locate.mjs copied a system font to the fixed path /tmp/locate-font.ttf and trusted any file already there. Another local user could plant that path first: a crafted font would mislabel the grid strips, a dangling link would make the copy write through it, and an unreadable file would stop every run. Pass the system font file to drawtext directly, quoted so a path with a space parses. With no known font present, drawtext uses its default font.
This commit is contained in:
@@ -6,8 +6,8 @@
|
||||
"files": 3
|
||||
},
|
||||
"hyperframes": {
|
||||
"hash": "90b4e96c091ebe16",
|
||||
"files": 80
|
||||
"hash": "5e1305c67d48155a",
|
||||
"files": 81
|
||||
},
|
||||
"hyperframes-cli": {
|
||||
"hash": "8c7ba18bfc4e86c1",
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
*/
|
||||
import "./lib/windows-exe-search.mjs";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { existsSync, mkdirSync, copyFileSync } from "node:fs";
|
||||
import { existsSync, mkdirSync } from "node:fs";
|
||||
|
||||
const N1 = 9,
|
||||
PAD1 = 0.4; // stage 1: strips per axis, padding in STRIP units
|
||||
@@ -54,31 +54,25 @@ function probe(img) {
|
||||
const [w, h] = out.split(",").map(Number);
|
||||
return { w, h };
|
||||
}
|
||||
function font() {
|
||||
const dst = "/tmp/locate-font.ttf";
|
||||
if (!existsSync(dst)) {
|
||||
for (const c of [
|
||||
"/System/Library/Fonts/Supplemental/Arial Bold.ttf",
|
||||
"/System/Library/Fonts/Supplemental/Arial.ttf",
|
||||
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
|
||||
]) {
|
||||
if (existsSync(c)) {
|
||||
copyFileSync(c, dst);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
const FONT_FILES = [
|
||||
"/System/Library/Fonts/Supplemental/Arial Bold.ttf",
|
||||
"/System/Library/Fonts/Supplemental/Arial.ttf",
|
||||
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
|
||||
];
|
||||
// ffmpeg reads the system font in place; with none found, drawtext falls back to its default font.
|
||||
function fontOption() {
|
||||
const file = FONT_FILES.find((candidate) => existsSync(candidate));
|
||||
return file ? `fontfile='${file}':` : "";
|
||||
}
|
||||
function gridFilters(w, h, n, axis, fz) {
|
||||
const F = font(),
|
||||
const F = fontOption(),
|
||||
fs = [];
|
||||
if (axis === "v" || axis === "both") {
|
||||
for (let i = 1; i < n; i++)
|
||||
fs.push(`drawbox=x=${Math.round((i * w) / n)}:y=0:w=2:h=${h}:color=${GREEN}@0.8:t=fill`);
|
||||
for (let i = 0; i < n; i++)
|
||||
fs.push(
|
||||
`drawtext=fontfile=${F}:text='${i + 1}':x=${Math.round(((i + 0.5) * w) / n - fz * 0.3)}:y=6:fontsize=${fz}:fontcolor=black:box=1:boxcolor=white@0.95:boxborderw=5`,
|
||||
`drawtext=${F}text='${i + 1}':x=${Math.round(((i + 0.5) * w) / n - fz * 0.3)}:y=6:fontsize=${fz}:fontcolor=black:box=1:boxcolor=white@0.95:boxborderw=5`,
|
||||
);
|
||||
}
|
||||
if (axis === "h" || axis === "both") {
|
||||
@@ -86,7 +80,7 @@ function gridFilters(w, h, n, axis, fz) {
|
||||
fs.push(`drawbox=x=0:y=${Math.round((j * h) / n)}:w=${w}:h=2:color=${BLUE}@0.8:t=fill`);
|
||||
for (let j = 0; j < n; j++)
|
||||
fs.push(
|
||||
`drawtext=fontfile=${F}:text='${j + 1}':x=6:y=${Math.round(((j + 0.5) * h) / n - fz * 0.5)}:fontsize=${fz}:fontcolor=black:box=1:boxcolor=white@0.95:boxborderw=5`,
|
||||
`drawtext=${F}text='${j + 1}':x=6:y=${Math.round(((j + 0.5) * h) / n - fz * 0.5)}:fontsize=${fz}:fontcolor=black:box=1:boxcolor=white@0.95:boxborderw=5`,
|
||||
);
|
||||
}
|
||||
return fs;
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { execFileSync, spawnSync } from "node:child_process";
|
||||
import { existsSync, lstatSync, mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const LOCATE = fileURLToPath(new URL("./locate.mjs", import.meta.url));
|
||||
const SHARED_FONT_PATH = "/tmp/locate-font.ttf";
|
||||
|
||||
function hasDrawtext() {
|
||||
const filters = spawnSync("ffmpeg", ["-hide_banner", "-filters"], { encoding: "utf8" });
|
||||
return filters.status === 0 && /\bdrawtext\b/.test(filters.stdout);
|
||||
}
|
||||
|
||||
function sharedFontState() {
|
||||
const stat = lstatSync(SHARED_FONT_PATH, { throwIfNoEntry: false, bigint: true });
|
||||
return stat ? `${stat.ino}:${stat.mtimeNs}` : null;
|
||||
}
|
||||
|
||||
test(
|
||||
"overlay draws its grids without reading or writing a font at a shared /tmp path",
|
||||
{ skip: !hasDrawtext() && "ffmpeg without the drawtext filter" },
|
||||
() => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "locate-test-"));
|
||||
try {
|
||||
const image = join(dir, "in.png");
|
||||
execFileSync("ffmpeg", [
|
||||
"-v",
|
||||
"error",
|
||||
"-f",
|
||||
"lavfi",
|
||||
"-i",
|
||||
"color=c=gray:s=320x180",
|
||||
"-frames:v",
|
||||
"1",
|
||||
image,
|
||||
]);
|
||||
const before = sharedFontState();
|
||||
execFileSync(process.execPath, [LOCATE, "overlay", image, "--out", join(dir, "grid")], {
|
||||
stdio: ["ignore", "ignore", "inherit"],
|
||||
});
|
||||
assert.ok(existsSync(join(dir, "grid", "gv.png")));
|
||||
assert.ok(existsSync(join(dir, "grid", "gh.png")));
|
||||
assert.equal(sharedFontState(), before);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user