Skip to content

Commit 58e2ec1

Browse files
committed
fix(scripts): make registry path containment filesystem-aware
Miguel's second P1 on #2975, and he is right that my first fix only closed half of it. `resolve()` and `relative()` are string operations and do not follow links. Registry items are copied in recursively with symlinks preserved, so a PR shipping `escape -> /tmp/outside` and declaring `target: "escape/pwned.txt"` passed the lexical check, `mkdirSync` followed the link, and `cpSync` wrote outside the project. Reproduced before fixing: the old predicate returned one allowed copy and the file appeared outside the project. Both directions were exposed — a symlinked `path` reads a runner file in just as readily. Containment is now filesystem-aware. No existing component of a candidate may be a symlink, and the candidate's real location — resolved through its deepest existing ancestor — has to sit under the project's own real path. A symlink is refused rather than followed, even one pointing back inside the project: nothing in the registry needs one, and following it would mean trusting the target not to change between the check and the copy. The tests are real fixtures now instead of string cases, because a purely lexical suite is exactly what stayed green through the bypass. Twelve of them, covering a symlinked target directory, a symlinked source file, a deeper path through a symlinked component, an inward-pointing symlink, plus the lexical and absolute cases from before.
1 parent 7d898b7 commit 58e2ec1

2 files changed

Lines changed: 141 additions & 63 deletions

File tree

scripts/registry-target-paths.mjs

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,84 @@
66
* `registry/components/**`, so a contributor's own manifest reaches the preview
77
* renderer, and the job then uploads `docs/images/catalog/` as an artifact.
88
*
9-
* `join()` happily walks out of its first argument, so a `files[].path` of
10-
* `../../../../etc/passwd` reads an arbitrary runner file into the project, and
11-
* a `files[].target` of the same shape writes an arbitrary runner path. Both
12-
* sides have to be resolved and checked, not just the one that looks like input.
9+
* Two escapes, and the second is why this is not a one-line check:
10+
*
11+
* Lexical — `join()` walks out of its first argument, so `files[].path` of
12+
* `../../../../etc/passwd` reads an arbitrary runner file into the project and
13+
* `files[].target` of the same shape writes an arbitrary runner path.
14+
*
15+
* Symbolic — `resolve()` and `relative()` are pure string operations and do
16+
* not follow links. The registry item is copied in recursively with symlinks
17+
* preserved, so a PR shipping `escape -> /tmp/outside` and declaring
18+
* `target: "escape/pwned.txt"` passes any lexical test; `mkdirSync` then
19+
* follows the link and `cpSync` writes outside the project.
20+
*
21+
* So containment is filesystem-aware: no existing component of a candidate may
22+
* be a symlink, and the candidate's real location — resolved through its
23+
* deepest existing ancestor — must sit under the project's own real path.
24+
* Both fields are checked, not just the one that looks like input.
1325
*/
1426

15-
import { isAbsolute, relative, resolve } from "node:path";
27+
import { existsSync, lstatSync, realpathSync } from "node:fs";
28+
import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
29+
30+
/** True when `candidate` is `root` itself or lexically beneath it. */
31+
function isBeneath(root, candidate) {
32+
const step = relative(root, candidate);
33+
return step === "" || (!step.startsWith(`..${sep}`) && step !== ".." && !isAbsolute(step));
34+
}
35+
36+
/** Every directory from `root` down to `candidate`, inclusive. */
37+
function componentsUnder(root, candidate) {
38+
const chain = [];
39+
for (let current = candidate; current !== root && isBeneath(root, current);) {
40+
chain.push(current);
41+
const parent = dirname(current);
42+
if (parent === current) break;
43+
current = parent;
44+
}
45+
return chain;
46+
}
47+
48+
/** `candidate` with symlinks resolved as far as the filesystem knows it. */
49+
function realLocation(candidate) {
50+
const existing = componentsUnder("", candidate).find((part) => existsSync(part));
51+
if (!existing) return candidate;
52+
return resolve(realpathSync(existing), relative(existing, candidate));
53+
}
1654

17-
/** True when `candidate` resolves to `root` itself or something beneath it. */
55+
/**
56+
* True when `candidate` really lands inside `root`.
57+
*
58+
* Lexical containment first, then a refusal of any existing component that is a
59+
* symlink, then a real-path check — a link is rejected outright rather than
60+
* followed, so a link pointing back inside the project is still refused. That
61+
* is deliberate: nothing in the registry needs one, and allowing it would mean
62+
* trusting the link target not to change between the check and the copy.
63+
*/
1864
export function isContainedIn(root, candidate) {
19-
const step = relative(resolve(root), resolve(root, candidate));
20-
return step === "" || (!step.startsWith("..") && !isAbsolute(step));
65+
const realRoot = realpathSync(resolve(root));
66+
const absolute = resolve(realRoot, candidate);
67+
if (!isBeneath(realRoot, absolute)) return false;
68+
if (componentsUnder(realRoot, absolute).some(isSymlink)) return false;
69+
return isBeneath(realRoot, realLocation(absolute));
70+
}
71+
72+
function isSymlink(target) {
73+
return (
74+
existsSync(dirname(target)) && lstatSync(target, { throwIfNoEntry: false })?.isSymbolicLink()
75+
);
2176
}
2277

2378
/**
2479
* The `[from, to]` pairs safe to copy, dropping any that escape `projectDir`.
2580
*
26-
* `exists` is injected so the containment rule can be tested without a fixture
27-
* tree — the traversal decision must not depend on whether the target happens
28-
* to be present on the runner.
81+
* `exists` is injected so a caller can test the containment rule without a
82+
* fixture tree — the traversal decision must not depend on whether the target
83+
* happens to be present on the runner.
2984
*/
3085
export function resolveContainedCopies(projectDir, files, exists) {
31-
const root = resolve(projectDir);
86+
const root = realpathSync(resolve(projectDir));
3287
return (files ?? [])
3388
.filter((file) => file?.path && file?.target)
3489
.filter((file) => isContainedIn(root, file.path) && isContainedIn(root, file.target))
Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,99 @@
11
import { strict as assert } from "node:assert";
2-
import { test } from "node:test";
2+
import {
3+
existsSync,
4+
mkdirSync,
5+
mkdtempSync,
6+
realpathSync,
7+
rmSync,
8+
symlinkSync,
9+
writeFileSync,
10+
} from "node:fs";
11+
import { tmpdir } from "node:os";
12+
import { join, resolve } from "node:path";
13+
import { after, before, test } from "node:test";
314

415
import { isContainedIn, resolveContainedCopies } from "./registry-target-paths.mjs";
516

6-
const ROOT = "/tmp/hf-catalog-demo";
7-
const always = () => true;
17+
// Real fixtures rather than string cases: the second escape this guards is a
18+
// symlink, which only exists on a filesystem. A purely lexical test suite is
19+
// exactly what stayed green through the first version of this check.
20+
let sandbox;
21+
let project;
822

9-
test("ordinary manifest entries are copied", () => {
10-
const copies = resolveContainedCopies(
11-
ROOT,
12-
[{ path: "demo.html", target: "compositions/demo.html" }],
13-
always,
14-
);
15-
assert.deepEqual(copies, [[`${ROOT}/demo.html`, `${ROOT}/compositions/demo.html`]]);
23+
before(() => {
24+
sandbox = mkdtempSync(join(tmpdir(), "hf-registry-paths-"));
25+
project = join(sandbox, "project");
26+
mkdirSync(join(project, "nested"), { recursive: true });
27+
mkdirSync(join(sandbox, "outside"), { recursive: true });
28+
writeFileSync(join(project, "demo.html"), "<html>\n");
29+
writeFileSync(join(sandbox, "secret.txt"), "runner secret\n");
30+
symlinkSync(join(sandbox, "outside"), join(project, "escape"));
31+
symlinkSync(join(sandbox, "secret.txt"), join(project, "leak.txt"));
32+
symlinkSync(join(project, "nested"), join(project, "inward"));
1633
});
1734

18-
// Both fields are attacker-controlled: catalog-previews.yml runs on
19-
// pull_request for any registry change, so the manifest arrives from the PR.
35+
after(() => rmSync(sandbox, { recursive: true, force: true }));
36+
37+
const allow = (files) => resolveContainedCopies(project, files, existsSync);
38+
39+
test("an ordinary manifest entry is copied", () => {
40+
// Compared against the real path: the helper resolves the project root, which
41+
// matters on macOS where the temp directory is itself a symlink.
42+
const real = realpathSync(project);
43+
assert.deepEqual(allow([{ path: "demo.html", target: "compositions/demo.html" }]), [
44+
[resolve(real, "demo.html"), resolve(real, "compositions/demo.html")],
45+
]);
46+
});
47+
48+
test("traversal that returns inside the project is allowed", () => {
49+
assert.equal(allow([{ path: "nested/../demo.html", target: "out/demo.html" }]).length, 1);
50+
});
51+
52+
// Lexical escapes.
2053

2154
test("a traversing path cannot read outside the project", () => {
22-
const copies = resolveContainedCopies(
23-
ROOT,
24-
[{ path: "../../../../etc/passwd", target: "leak.txt" }],
25-
always,
26-
);
27-
assert.deepEqual(copies, []);
55+
assert.deepEqual(allow([{ path: "../secret.txt", target: "stolen.txt" }]), []);
2856
});
2957

3058
test("a traversing target cannot write outside the project", () => {
31-
const copies = resolveContainedCopies(
32-
ROOT,
33-
[{ path: "demo.html", target: "../../../../home/runner/.bashrc" }],
34-
always,
35-
);
36-
assert.deepEqual(copies, []);
59+
assert.deepEqual(allow([{ path: "demo.html", target: "../pwned.txt" }]), []);
3760
});
3861

3962
test("an absolute path or target is refused on either side", () => {
40-
assert.deepEqual(
41-
resolveContainedCopies(ROOT, [{ path: "/etc/passwd", target: "leak.txt" }], always),
42-
[],
43-
);
44-
assert.deepEqual(
45-
resolveContainedCopies(ROOT, [{ path: "demo.html", target: "/etc/cron.d/x" }], always),
46-
[],
47-
);
63+
assert.deepEqual(allow([{ path: "/etc/passwd", target: "stolen.txt" }]), []);
64+
assert.deepEqual(allow([{ path: "demo.html", target: "/tmp/pwned.txt" }]), []);
4865
});
4966

50-
test("traversal that returns inside the project is allowed", () => {
51-
const copies = resolveContainedCopies(
52-
ROOT,
53-
[{ path: "nested/../demo.html", target: "out/demo.html" }],
54-
always,
55-
);
56-
assert.deepEqual(copies, [[`${ROOT}/demo.html`, `${ROOT}/out/demo.html`]]);
67+
test("a sibling directory sharing the project's prefix is still outside", () => {
68+
assert.equal(isContainedIn(project, "../project-evil/x"), false);
5769
});
5870

59-
test("a sibling directory sharing the project's prefix is still outside", () => {
60-
assert.equal(isContainedIn(ROOT, "../hf-catalog-demo-evil/x"), false);
71+
// Symbolic escapes. resolve()/relative() do not follow links, so every case
72+
// below passed the first, lexical-only version of this check.
73+
74+
test("a symlinked target directory cannot be written through", () => {
75+
assert.deepEqual(allow([{ path: "demo.html", target: "escape/pwned.txt" }]), []);
76+
});
77+
78+
test("a symlinked source file cannot be read through", () => {
79+
assert.deepEqual(allow([{ path: "leak.txt", target: "stolen.txt" }]), []);
6180
});
6281

63-
test("containment does not depend on the file existing", () => {
64-
assert.equal(isContainedIn(ROOT, "../../etc/passwd"), false);
65-
assert.deepEqual(
66-
resolveContainedCopies(ROOT, [{ path: "../../etc/passwd", target: "x" }], () => true),
67-
[],
68-
);
82+
test("a symlink is refused even when it points back inside the project", () => {
83+
// Rejected rather than followed: nothing in the registry needs a symlink, and
84+
// allowing one means trusting its target not to change before the copy.
85+
assert.deepEqual(allow([{ path: "demo.html", target: "inward/a.txt" }]), []);
86+
});
87+
88+
test("a deeper path through a symlinked component is refused", () => {
89+
assert.deepEqual(allow([{ path: "demo.html", target: "escape/a/b/c.txt" }]), []);
6990
});
7091

7192
test("incomplete entries are skipped rather than resolved", () => {
72-
assert.deepEqual(
73-
resolveContainedCopies(ROOT, [{ path: "demo.html" }, { target: "x" }, {}], always),
74-
[],
75-
);
93+
assert.deepEqual(allow([{ path: "demo.html" }, { target: "x" }, {}]), []);
94+
});
95+
96+
test("containment does not depend on the candidate existing", () => {
97+
assert.equal(isContainedIn(project, "../../etc/passwd"), false);
98+
assert.equal(isContainedIn(project, "not-created-yet/file.txt"), true);
7699
});

0 commit comments

Comments
 (0)