mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-17 15:24:19 +00:00
test: reject symlink artifact escapes
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user