Skip to content

Commit 6588b84

Browse files
t3dotggclaude
andcommitted
Handle root seed-home containment
Co-Authored-By: Claude <[email protected]>
1 parent 6800ee3 commit 6588b84

2 files changed

Lines changed: 61 additions & 20 deletions

File tree

‎scripts/dev-seed-cli.test.ts‎

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@ import * as NodeFS from "node:fs";
55
import * as NodeOS from "node:os";
66
import * as NodePath from "node:path";
77

8+
const runDevSeed = (input: {
9+
readonly root: string;
10+
readonly targetBaseDir: string;
11+
readonly t3Home: string;
12+
}) =>
13+
NodeChildProcess.spawnSync(
14+
process.execPath,
15+
[
16+
NodePath.join(import.meta.dirname, "dev-seed.ts"),
17+
"--from",
18+
NodePath.join(input.root, "source"),
19+
"--to",
20+
input.targetBaseDir,
21+
],
22+
{
23+
encoding: "utf8",
24+
env: { ...process.env, NO_COLOR: "1", T3CODE_HOME: input.t3Home },
25+
},
26+
);
27+
28+
const assertSharedHomeRefusal = (result: ReturnType<typeof runDevSeed>) => {
29+
assert.notEqual(result.status, 0);
30+
assert.include(
31+
`${result.stdout}${result.stderr}`,
32+
"that database lives in the shared T3 Code home",
33+
);
34+
};
35+
836
it("refuses to seed a target inside T3CODE_HOME through a symlink", () => {
937
const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-dev-seed-cli-"));
1038
try {
@@ -16,26 +44,9 @@ it("refuses to seed a target inside T3CODE_HOME through a symlink", () => {
1644
NodeFS.writeFileSync(NodePath.join(sharedStateDir, "state.sqlite"), "must stay untouched");
1745
NodeFS.symlinkSync(sharedStateDir, NodePath.join(targetBaseDir, "userdata"), "dir");
1846

19-
const result = NodeChildProcess.spawnSync(
20-
process.execPath,
21-
[
22-
NodePath.join(import.meta.dirname, "dev-seed.ts"),
23-
"--from",
24-
NodePath.join(root, "source"),
25-
"--to",
26-
targetBaseDir,
27-
],
28-
{
29-
encoding: "utf8",
30-
env: { ...process.env, NO_COLOR: "1", T3CODE_HOME: sharedHome },
31-
},
32-
);
47+
const result = runDevSeed({ root, targetBaseDir, t3Home: sharedHome });
3348

34-
assert.notEqual(result.status, 0);
35-
assert.include(
36-
`${result.stdout}${result.stderr}`,
37-
"that database lives in the shared T3 Code home",
38-
);
49+
assertSharedHomeRefusal(result);
3950
assert.equal(
4051
NodeFS.readFileSync(NodePath.join(sharedStateDir, "state.sqlite"), "utf8"),
4152
"must stay untouched",
@@ -44,3 +55,25 @@ it("refuses to seed a target inside T3CODE_HOME through a symlink", () => {
4455
NodeFS.rmSync(root, { recursive: true, force: true });
4556
}
4657
});
58+
59+
it("refuses targets when T3CODE_HOME resolves to the filesystem root", () => {
60+
const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-dev-seed-root-cli-"));
61+
try {
62+
const sharedHome = NodePath.join(root, "shared");
63+
const targetBaseDir = NodePath.join(root, "target");
64+
const targetStateDir = NodePath.join(targetBaseDir, "userdata");
65+
NodeFS.symlinkSync(NodePath.parse(root).root, sharedHome, "dir");
66+
NodeFS.mkdirSync(targetStateDir, { recursive: true });
67+
NodeFS.writeFileSync(NodePath.join(targetStateDir, "state.sqlite"), "must stay untouched");
68+
69+
const result = runDevSeed({ root, targetBaseDir, t3Home: sharedHome });
70+
71+
assertSharedHomeRefusal(result);
72+
assert.equal(
73+
NodeFS.readFileSync(NodePath.join(targetStateDir, "state.sqlite"), "utf8"),
74+
"must stay untouched",
75+
);
76+
} finally {
77+
NodeFS.rmSync(root, { recursive: true, force: true });
78+
}
79+
});

‎scripts/dev-seed.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ const resolveSharedHome = (path: Path.Path): string => {
3838
);
3939
};
4040

41+
const isPathWithin = (path: Path.Path, parent: string, candidate: string): boolean => {
42+
const relative = path.relative(parent, candidate);
43+
return (
44+
relative === "" ||
45+
(relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative))
46+
);
47+
};
48+
4149
class DevSeedTargetError extends Schema.TaggedErrorClass<DevSeedTargetError>()(
4250
"DevSeedTargetError",
4351
{
@@ -283,7 +291,7 @@ const devSeedCli = Command.make("dev-seed", {
283291
canonicalDbPath(sourceDbPath),
284292
fileSystem.realPath(sharedHome).pipe(Effect.orElseSucceed(() => sharedHome)),
285293
]);
286-
if (canonicalTargetDb.startsWith(`${canonicalShared}${path.sep}`)) {
294+
if (isPathWithin(path, canonicalShared, canonicalTargetDb)) {
287295
return yield* new DevSeedTargetError({ reason: "shared-home", detail: canonicalTargetDb });
288296
}
289297
// After the shared-home refusal: that one must come first because the

0 commit comments

Comments
 (0)