Skip to content

Write HTML. Render video. Built for agents.

fix: create temp dirs with mkdtemp, not a name built from Date.now() (#3241)

Vance Ingallsde4062a93300
diff --git a/.github/workflows/catalog-previews.yml b/.github/workflows/catalog-previews.yml
index 9cf3a9c0b6..be19c2f2bf 100644
--- a/.github/workflows/catalog-previews.yml
+++ b/.github/workflows/catalog-previews.yml
@@ -15,9 +15,11 @@ on:
- "registry/blocks/**"
- "registry/components/**"
- "scripts/generate-catalog-previews.ts"
- # The containment module the renderer imports. Without it a change to
- # path-traversal defence alone never re-runs the job that exercises it.
+ # Modules the renderer imports. Without them a change to path-traversal
+ # defence or temp-directory allocation alone never re-runs the job that
+ # exercises it.
- "scripts/registry-target-paths.mjs"
+ - "scripts/catalog-preview-temp.ts"
- ".github/workflows/catalog-previews.yml"
concurrency:
@@ -87,7 +89,8 @@ jobs:
# timeline at body level (rendered directly). Getting that wrong is
# silent — the wrong-shaped block renders blank, not red.
RENDERER_CHANGED=$(git diff --name-only --diff-filter=ACMR "$BASE_SHA"...HEAD \
- -- scripts/generate-catalog-previews.ts scripts/registry-target-paths.mjs)
+ -- scripts/generate-catalog-previews.ts scripts/registry-target-paths.mjs \
+ scripts/catalog-preview-temp.ts)
if [ -n "$RENDERER_CHANGED" ]; then
CHANGED_ITEMS=$(printf '%s\n' $CHANGED_ITEMS \
code-snippet-visual-studio-dark code-snippet-apple-terminal-pro | sort -u)
diff --git a/package.json b/package.json
index 9298d06e3c..65969815e8 100644
--- a/package.json
+++ b/package.json
@@ -48,7 +48,7 @@
"player:perf": "bun run --filter @hyperframes/player perf",
"format:check": "oxfmt --check .",
"knip": "knip",
- "test:scripts": "node --import tsx --test scripts/check-tracked-artifacts.test.mjs scripts/check-no-main-deletions.test.mjs scripts/check-docs-snippet-motion.test.mjs scripts/registry-target-paths.test.mjs scripts/check-workspace-contracts.test.mjs scripts/check-package-cycles.test.mjs scripts/check-cli-process-ownership.test.mjs scripts/package-subpaths.test.mjs scripts/validate-release-channel.test.mjs scripts/publish-workflow.test.mjs scripts/install-workspace-dependencies.test.mjs scripts/draft-changelog.test.ts scripts/set-version.test.ts scripts/release-prepare.test.ts scripts/cli-options.test.ts scripts/changelog-weekly.test.ts scripts/claude-plugin-compression.test.ts scripts/catalog-payload-assets.test.ts scripts/studio-runtime-smoke.test.mjs scripts/verify-packed-manifests.test.mjs scripts/lint-skills.test.mjs packages/gcp-cloud-run/check-dockerfile-workspaces.test.mjs && vitest run scripts/catalog/",
+ "test:scripts": "node --import tsx --test scripts/check-tracked-artifacts.test.mjs scripts/check-no-main-deletions.test.mjs scripts/check-docs-snippet-motion.test.mjs scripts/registry-target-paths.test.mjs scripts/check-workspace-contracts.test.mjs scripts/check-package-cycles.test.mjs scripts/check-cli-process-ownership.test.mjs scripts/package-subpaths.test.mjs scripts/validate-release-channel.test.mjs scripts/publish-workflow.test.mjs scripts/install-workspace-dependencies.test.mjs scripts/draft-changelog.test.ts scripts/set-version.test.ts scripts/release-prepare.test.ts scripts/cli-options.test.ts scripts/changelog-weekly.test.ts scripts/claude-plugin-compression.test.ts scripts/catalog-payload-assets.test.ts scripts/catalog-preview-temp.test.ts scripts/studio-runtime-smoke.test.mjs scripts/verify-packed-manifests.test.mjs scripts/lint-skills.test.mjs packages/gcp-cloud-run/check-dockerfile-workspaces.test.mjs && vitest run scripts/catalog/",
"typecheck:scripts": "tsc --noEmit -p scripts/tsconfig.json",
"test:skills": "node --test 'skills/**/*.test.mjs'",
"generate:previews": "tsx scripts/generate-template-previews.ts",
diff --git a/packages/cli/src/whisper/normalize.test.ts b/packages/cli/src/whisper/normalize.test.ts
index 20d0252ded..8ee305a615 100644
--- a/packages/cli/src/whisper/normalize.test.ts
+++ b/packages/cli/src/whisper/normalize.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect, afterEach } from "vitest";
-import { writeFileSync, readFileSync, mkdirSync, rmSync } from "node:fs";
+import { writeFileSync, readFileSync, mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import {
@@ -14,8 +14,7 @@ import {
import { detectSpeechOnset } from "./transcribe.js";
function tmpFile(name: string, content: string): string {
- const dir = join(tmpdir(), `hf-normalize-test-${Date.now()}`);
- mkdirSync(dir, { recursive: true });
+ const dir = mkdtempSync(join(tmpdir(), "hf-normalize-test-"));
dirs.push(dir);
const path = join(dir, name);
writeFileSync(path, content);
@@ -478,8 +477,7 @@ describe("whisper-cpp zero-duration interpolation", () => {
describe("patchCaptionHtml", () => {
it("replaces const script = [] in HTML files", () => {
- const dir = join(tmpdir(), `hf-patch-test-${Date.now()}`);
- mkdirSync(dir, { recursive: true });
+ const dir = mkdtempSync(join(tmpdir(), "hf-patch-test-"));
dirs.push(dir);
const html = `<html><body><script>
@@ -501,8 +499,7 @@ describe("patchCaptionHtml", () => {
});
it("replaces const TRANSCRIPT = [] variant", () => {
- const dir = join(tmpdir(), `hf-patch-test-${Date.now()}`);
- mkdirSync(dir, { recursive: true });
+ const dir = mkdtempSync(join(tmpdir(), "hf-patch-test-"));
dirs.push(dir);
const html = `<script>const TRANSCRIPT = [];</script>`;
@@ -516,8 +513,7 @@ describe("patchCaptionHtml", () => {
});
it("does not modify HTML files without matching script patterns", () => {
- const dir = join(tmpdir(), `hf-patch-test-${Date.now()}`);
- mkdirSync(dir, { recursive: true });
+ const dir = mkdtempSync(join(tmpdir(), "hf-patch-test-"));
dirs.push(dir);
const html = `<html><body><script>console.log("hello");</script></body></html>`;
@@ -530,8 +526,7 @@ describe("patchCaptionHtml", () => {
});
it("skips empty word arrays", () => {
- const dir = join(tmpdir(), `hf-patch-test-${Date.now()}`);
- mkdirSync(dir, { recursive: true });
+ const dir = mkdtempSync(join(tmpdir(), "hf-patch-test-"));
dirs.push(dir);
const html = `<script>const script = [];</script>`;
@@ -572,9 +567,10 @@ describe("detectSpeechOnset", () => {
const amplitude = energyFn(t);
buf.writeInt16LE(Math.round(amplitude * 32767), 44 + i * 2);
}
- const path = join(tmpdir(), `hf-wav-test-${Date.now()}-${Math.floor(Math.random() * 1e6)}.wav`);
+ const dir = mkdtempSync(join(tmpdir(), "hf-wav-test-"));
+ dirs.push(dir);
+ const path = join(dir, "tone.wav");
writeFileSync(path, buf);
- dirs.push(path);
return path;
}
diff --git a/packages/producer/src/benchmark.ts b/packages/producer/src/benchmark.ts
index e13259f45f..68d59b90b9 100644
--- a/packages/producer/src/benchmark.ts
+++ b/packages/producer/src/benchmark.ts
@@ -25,6 +25,7 @@ import {
writeFileSync,
existsSync,
mkdirSync,
+ mkdtempSync,
cpSync,
rmSync,
} from "node:fs";
@@ -204,8 +205,7 @@ async function runBenchmark(): Promise<void> {
console.log(` Run ${r + 1}/${runs}...`);
// Copy src to temp dir for isolation
- const tmpRoot = join(tmpdir(), `benchmark-${fixture.id}-${Date.now()}`);
- mkdirSync(tmpRoot, { recursive: true });
+ const tmpRoot = mkdtempSync(join(tmpdir(), `benchmark-${fixture.id}-`));
cpSync(join(fixture.dir, "src"), join(tmpRoot, "src"), { recursive: true });
const projectDir = join(tmpRoot, "src");
diff --git a/packages/producer/src/transparency-test.ts b/packages/producer/src/transparency-test.ts
index 7cb87058c2..e686f53bd6 100644
--- a/packages/producer/src/transparency-test.ts
+++ b/packages/producer/src/transparency-test.ts
@@ -20,7 +20,7 @@
*/
import { strict as assert } from "node:assert";
-import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync } from "node:fs";
+import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
@@ -309,8 +309,7 @@ async function main(): Promise<void> {
if (!existsSync(SHADER_FIXTURE_SRC) || !existsSync(SHADER_GOLDEN)) {
throw new Error(`Shader fixture or golden missing: ${SHADER_FIXTURE_DIR}`);
}
- const workRoot = join(tmpdir(), `hf-transparency-${process.pid}-${Date.now()}`);
- mkdirSync(workRoot, { recursive: true });
+ const workRoot = mkdtempSync(join(tmpdir(), "hf-transparency-"));
const keepWork = process.env.KEEP_TEMP === "1";
console.log(`work dir: ${workRoot}${keepWork ? " (KEEP_TEMP=1)" : ""}`);
diff --git a/scripts/catalog-preview-temp.ts b/scripts/catalog-preview-temp.ts
index 8e7f555378..7bbafe26a1 100644
--- a/scripts/catalog-preview-temp.ts
+++ b/scripts/catalog-preview-temp.ts
@@ -2,7 +2,13 @@ import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
-/** Atomically allocate an owner-only preview directory under the OS temp root. */
+/**
+ * Atomically allocate an owner-only preview directory under the OS temp root.
+ *
+ * `mkdtemp` rather than a name built from `Date.now()`: it picks the random
+ * suffix and creates the directory 0700 in one syscall, so nothing can
+ * pre-create or symlink the path between choosing the name and making it.
+ */
export function createCatalogPreviewTempDir(itemName: string): string {
return mkdtempSync(join(tmpdir(), `hf-catalog-${itemName}-`));
}
diff --git a/scripts/generate-catalog-previews.ts b/scripts/generate-catalog-previews.ts
index 3b55f83cae..a49a882a85 100644
--- a/scripts/generate-catalog-previews.ts
+++ b/scripts/generate-catalog-previews.ts
@@ -31,8 +31,8 @@ import {
} from "node:fs";
import { execFileSync } from "node:child_process";
import { join, resolve, dirname } from "node:path";
-import { tmpdir } from "node:os";
import { fileURLToPath } from "node:url";
+import { createCatalogPreviewTempDir } from "./catalog-preview-temp.js";
// Import from source — bun workspace linking doesn't resolve for scripts outside packages/.
import {
captureFrame,
@@ -170,8 +170,7 @@ export async function prepareProjectDir(
item: CatalogItem,
options: PrepareOptions = {},
): Promise<string> {
- const tmpDir = join(tmpdir(), `hf-catalog-${item.name}-${Date.now()}`);
- mkdirSync(tmpDir, { recursive: true });
+ const tmpDir = createCatalogPreviewTempDir(item.name);
cpSync(item.sourceDir, tmpDir, { recursive: true });
mirrorRegistryTargets(tmpDir);
diff --git a/scripts/generate-template-previews.ts b/scripts/generate-template-previews.ts
index b4189109a9..a63642a892 100644
--- a/scripts/generate-template-previews.ts
+++ b/scripts/generate-template-previews.ts
@@ -21,6 +21,7 @@ import {
writeFileSync,
existsSync,
mkdirSync,
+ mkdtempSync,
cpSync,
rmSync,
} from "node:fs";
@@ -126,8 +127,7 @@ function discoverTemplates(only: string | null): string[] {
}
function prepareTemplateDir(templateId: string): string {
- const tmpDir = join(tmpdir(), `hf-preview-${templateId}-${Date.now()}`);
- mkdirSync(tmpDir, { recursive: true });
+ const tmpDir = mkdtempSync(join(tmpdir(), `hf-preview-${templateId}-`));
const src = resolveTemplateDir(templateId);
if (!src) throw new Error(`Template directory not found for "${templateId}"`);
cpSync(src, tmpDir, { recursive: true });