mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
* fix(cli): staging folders beside a project path no longer fail past 260 characters on Windows mkdtemp does not get the long-path prefix that mkdir does, so every staging folder made beside a project or output path failed once its full path passed 260 characters. They now come from one helper, createStagingDir in @hyperframes/core/atomic-file, and a scan test fails on a new mkdtemp beside a path. * fix(cli): give mkdtemp the long-path form instead of replacing it CodeQL treats mkdtemp as the safe way to make a folder under the system temp dir and flagged every file written into a folder from the mkdir-based helper. createStagingDir now calls mkdtemp with the long-path form of the path on Windows, so both hold. * fix(cli): leave non-Windows paths alone when making a staging folder The long-path form is now chosen by the shape of the path, not process.platform, so tests that pretend to be Windows keep working and the comment share does not rise. * fix(core): a staging folder keeps a Windows path that already names its namespace * fix(core): a staging folder keeps a Windows device path as the caller gave it * fix(core): the staging folder helper's comment fits the line limit
102 lines
4.0 KiB
TypeScript
102 lines
4.0 KiB
TypeScript
import { mkdirSync, realpathSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
import { CaptureDirRefusedError } from "./captureErrors.js";
|
|
import { removeTreeBestEffort } from "@hyperframes/core/remove-tree";
|
|
import { createStagingDir } from "@hyperframes/core/atomic-file";
|
|
|
|
type WriteOptions = Parameters<typeof writeFileSync>[2];
|
|
|
|
// Writes are staged in a private directory next to the target and renamed over
|
|
// it, the same publication pattern as the producer's font cache (#3669).
|
|
// `rename` replaces the directory entry itself, so a symlink or hard link
|
|
// pre-planted at the target is swapped out instead of written through, while
|
|
// intentional overwrites (the deadline partial bundle rewriting files from the
|
|
// in-progress attempt) keep working. The staging directory is created fresh by
|
|
// this call, so nothing in it can be pre-planted and cleanup only ever removes
|
|
// what this call made.
|
|
function publishCaptureFileSync(
|
|
path: string,
|
|
data: string | NodeJS.ArrayBufferView,
|
|
options: Exclude<WriteOptions, string>,
|
|
): void {
|
|
const stagingDir = createStagingDir(dirname(path), ".capture-");
|
|
try {
|
|
const staged = join(stagingDir, basename(path));
|
|
writeCaptureFileSync(staged, data, { ...options, flag: "wx" });
|
|
renameSync(staged, path);
|
|
} finally {
|
|
removeTreeBestEffort(stagingDir);
|
|
}
|
|
}
|
|
|
|
// A caller that passes its own flag keeps those semantics. Exclusive create
|
|
// (`wx`) is already safe on its own: O_EXCL refuses anything at the target,
|
|
// symlinks included.
|
|
export function writeCaptureFileSync(
|
|
path: string,
|
|
data: string | NodeJS.ArrayBufferView,
|
|
options?: WriteOptions,
|
|
): void {
|
|
const normalized = typeof options === "string" ? { encoding: options } : options;
|
|
if (normalized?.flag === undefined || normalized.flag === "w") {
|
|
return publishCaptureFileSync(path, data, normalized);
|
|
}
|
|
writeFileSync(path, data, { ...normalized, mode: 0o600 });
|
|
}
|
|
|
|
function hasCode(error: unknown, ...codes: string[]): boolean {
|
|
return (
|
|
error instanceof Error &&
|
|
"code" in error &&
|
|
typeof error.code === "string" &&
|
|
codes.includes(error.code)
|
|
);
|
|
}
|
|
|
|
// The JS `realpathSync`, deliberately not `realpathSync.native`: native returns the on-disk case
|
|
// on macOS/Windows, so an existing `Assets/` asked for as `assets` would be falsely refused. The
|
|
// JS walk only rewrites the components that are links, which is exactly what is being checked.
|
|
function resolvedPath(path: string): string | undefined {
|
|
try {
|
|
return realpathSync(path);
|
|
} catch (error) {
|
|
// A dangling or looping link resolves nowhere: refuse it like any other escaping link.
|
|
if (hasCode(error, "ENOENT", "ELOOP")) return undefined;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// Creates `dir` under the trusted capture root, refusing any directory whose real path leaves it (#4304).
|
|
// Checked on creation, not on every write: a link swapped in later by a racing process is out of scope.
|
|
export function ensureCaptureDirSync(root: string, dir: string): string {
|
|
const rootPath = resolve(root);
|
|
const target = resolve(dir);
|
|
const rel = relative(rootPath, target);
|
|
if (rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
|
|
throw new CaptureDirRefusedError(
|
|
`Refusing to create ${target}: it is outside the capture directory ${rootPath}`,
|
|
);
|
|
}
|
|
mkdirSync(rootPath, { recursive: true });
|
|
let current = rootPath;
|
|
let expected = realpathSync(rootPath);
|
|
for (const segment of rel.split(sep).filter(Boolean)) {
|
|
current = join(current, segment);
|
|
expected = join(expected, segment);
|
|
try {
|
|
mkdirSync(current);
|
|
} catch (error) {
|
|
if (!hasCode(error, "EEXIST")) throw error;
|
|
}
|
|
if (resolvedPath(current) !== expected) {
|
|
throw new CaptureDirRefusedError(
|
|
`Refusing to write into ${current}: it resolves outside the capture directory ${rootPath}`,
|
|
);
|
|
}
|
|
if (!statSync(current).isDirectory()) {
|
|
throw new CaptureDirRefusedError(`Refusing to write into ${current}: it is not a directory`);
|
|
}
|
|
}
|
|
return target;
|
|
}
|