diff --git a/scripts/issue3329-artifact-safety.mjs b/scripts/issue3329-artifact-safety.mjs index 6aa60bd299..a1070626d6 100644 --- a/scripts/issue3329-artifact-safety.mjs +++ b/scripts/issue3329-artifact-safety.mjs @@ -1,26 +1,55 @@ +import { existsSync, realpathSync } from "node:fs"; import { homedir } from "node:os"; -import { isAbsolute, parse, relative, resolve, sep } from "node:path"; +import { basename, dirname, isAbsolute, parse, relative, resolve, sep } from "node:path"; -export function validateArtifactRoot(requestedPath, repoRoot) { - const repo = resolve(repoRoot); - const artifactSpace = resolve(repo, ".artifacts"); - const raw = String(requestedPath ?? ""); +function rejectTraversal(raw) { if (raw.split(/[\\/]+/).includes("..")) { throw new Error("Artifact root must not contain traversal segments"); } - const target = resolve(repo, raw); +} + +function isForbiddenRoot(target, repo, artifactSpace) { + return new Set([parse(target).root, resolve(homedir()), repo, artifactSpace]).has(target); +} + +function isDedicatedDescendant(target, artifactSpace) { const rel = relative(artifactSpace, target); - const forbidden = new Set([parse(target).root, resolve(homedir()), repo, artifactSpace]); + return rel !== "" && rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel); +} + +function canonicalizeTarget(target) { + const suffix = []; + let ancestor = target; + while (!existsSync(ancestor)) { + const parent = dirname(ancestor); + if (parent === ancestor) { + throw new Error("Artifact root has no canonical ancestor"); + } + suffix.unshift(basename(ancestor)); + ancestor = parent; + } + return resolve(realpathSync(ancestor), ...suffix); +} + +function rejectUnsafeTarget(target, repo, artifactSpace) { if ( - forbidden.has(target) || - rel === "" || - rel === ".." || - rel.startsWith(`..${sep}`) || - isAbsolute(rel) + isForbiddenRoot(target, repo, artifactSpace) || + !isDedicatedDescendant(artifactSpace, repo) || + !isDedicatedDescendant(target, artifactSpace) ) { throw new Error( "Artifact root must be a dedicated descendant of the repository artifact space", ); } - return target; +} + +export function validateArtifactRoot(requestedPath, repoRoot) { + const raw = String(requestedPath ?? ""); + rejectTraversal(raw); + const repo = realpathSync(resolve(repoRoot)); + const artifactSpace = canonicalizeTarget(resolve(repo, ".artifacts")); + const requestedTarget = resolve(repo, raw); + const target = canonicalizeTarget(requestedTarget); + rejectUnsafeTarget(target, repo, artifactSpace); + return requestedTarget; } diff --git a/scripts/issue3329-macos-real-pixel.test.mjs b/scripts/issue3329-macos-real-pixel.test.mjs index cbd5564c57..b87f539b2f 100644 --- a/scripts/issue3329-macos-real-pixel.test.mjs +++ b/scripts/issue3329-macos-real-pixel.test.mjs @@ -1,5 +1,13 @@ import assert from "node:assert/strict"; -import { mkdtempSync, readFileSync, realpathSync } from "node:fs"; +import { + mkdirSync, + mkdtempSync, + readFileSync, + realpathSync, + rmSync, + symlinkSync, + unlinkSync, +} from "node:fs"; import { homedir, tmpdir } from "node:os"; import { dirname, join, parse, resolve } from "node:path"; import test from "node:test"; @@ -36,6 +44,24 @@ test("cleanup rejects broad, traversing, home, and arbitrary external targets", } }); +test("cleanup rejects a descendant that escapes through a symlink", () => { + assert.ok(safetyModule, "artifact cleanup safety module must exist"); + const artifactSpace = join(repo, ".artifacts"); + const outside = mkdtempSync(join(tmpdir(), "hf-issue3329-symlink-external-")); + const escape = join(artifactSpace, `escape-${process.pid}-${Date.now()}`); + mkdirSync(artifactSpace, { recursive: true }); + symlinkSync(outside, escape, "dir"); + try { + assert.throws( + () => safetyModule.validateArtifactRoot(join(escape, "run"), repo), + /artifact root/i, + ); + } finally { + unlinkSync(escape); + rmSync(outside, { recursive: true }); + } +}); + test("ffprobe terminates option parsing before its controlled input path", () => { const source = readFileSync(join(scriptsDir, "issue3329-macos-real-pixel.mjs"), "utf8"); assert.match(