Skip to content

Commit 6039127

Browse files
committed
fix(settings): links resolve where they really live, and unreadable links fail the save
Signed-off-by: Yordis Prieto <[email protected]>
1 parent 7ab2a1d commit 6039127

2 files changed

Lines changed: 81 additions & 5 deletions

File tree

‎apps/server/src/atomicWrite.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { assert, it } from "@effect/vitest";
33
import * as Effect from "effect/Effect";
44
import * as Exit from "effect/Exit";
55
import * as FileSystem from "effect/FileSystem";
6+
import * as Layer from "effect/Layer";
67
import * as Path from "effect/Path";
8+
import * as PlatformError from "effect/PlatformError";
79

810
import { writeFileStringAtomically } from "./atomicWrite.ts";
911

@@ -64,6 +66,29 @@ it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
6466
}),
6567
);
6668

69+
it.effect("resolves a relative link through a symlinked parent directory", () =>
70+
Effect.gen(function* () {
71+
const fs = yield* FileSystem.FileSystem;
72+
const path = yield* Path.Path;
73+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
74+
const destination = path.join(root, "dotfiles", "config", "settings.json");
75+
const linkedState = path.join(root, "dotfiles", "state");
76+
const home = path.join(root, "home");
77+
const link = path.join(home, "state", "settings.json");
78+
yield* fs.makeDirectory(path.dirname(destination), { recursive: true });
79+
yield* fs.makeDirectory(linkedState, { recursive: true });
80+
yield* fs.makeDirectory(home, { recursive: true });
81+
yield* fs.symlink(linkedState, path.join(home, "state"));
82+
yield* fs.writeFileString(destination, "before");
83+
yield* fs.symlink("../config/settings.json", link);
84+
85+
yield* writeFileStringAtomically({ filePath: link, contents: "after" });
86+
87+
assert.strictEqual(yield* fs.readLink(link), "../config/settings.json");
88+
assert.strictEqual(yield* fs.readFileString(destination), "after");
89+
}),
90+
);
91+
6792
it.effect("creates a missing file and its directory", () =>
6893
Effect.gen(function* () {
6994
const fs = yield* FileSystem.FileSystem;
@@ -77,3 +102,38 @@ it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
77102
}),
78103
);
79104
});
105+
106+
it.effect("surfaces an unreadable link instead of writing over it", () =>
107+
Effect.gen(function* () {
108+
const readLinkFailure = PlatformError.systemError({
109+
_tag: "Unknown",
110+
module: "FileSystem",
111+
method: "readLink",
112+
pathOrDescriptor: "/home/settings.json",
113+
});
114+
115+
const result = yield* Effect.exit(
116+
writeFileStringAtomically({ filePath: "/home/settings.json", contents: "after" }),
117+
);
118+
119+
assert.deepStrictEqual(result, Exit.fail(readLinkFailure));
120+
}).pipe(
121+
Effect.provide(
122+
Layer.mergeAll(
123+
Path.layer,
124+
FileSystem.layerNoop({
125+
readLink: () =>
126+
Effect.fail(
127+
PlatformError.systemError({
128+
_tag: "Unknown",
129+
module: "FileSystem",
130+
method: "readLink",
131+
pathOrDescriptor: "/home/settings.json",
132+
}),
133+
),
134+
rename: () => Effect.die("an unreadable link must not be replaced"),
135+
}),
136+
),
137+
),
138+
),
139+
);

‎packages/shared/src/symlink.ts‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
11
import * as Effect from "effect/Effect";
22
import * as FileSystem from "effect/FileSystem";
3+
import * as Option from "effect/Option";
34
import * as Path from "effect/Path";
45
import * as PlatformError from "effect/PlatformError";
56

67
const MAX_SYMLINK_HOPS = 40;
78

9+
const isNotFound = (error: PlatformError.PlatformError) => error.reason._tag === "NotFound";
10+
11+
const isNotASymlink = (error: PlatformError.PlatformError) =>
12+
isNotFound(error) ||
13+
(error.cause instanceof Error && "code" in error.cause && error.cause.code === "EINVAL");
14+
815
/**
916
* Follows a chain of symlinks to the file it finally names, which may not exist
1017
* yet. Any path that is not a symlink resolves to itself. Atomic writers rename
11-
* onto this path so a linked file keeps its link. Fails on a cycle or an overly
12-
* long chain rather than handing back a link that a rename would replace.
18+
* onto this path so a linked file keeps its link. Fails on a cycle, an overly
19+
* long chain, or an unreadable link rather than handing back a link that a
20+
* rename would replace.
1321
*/
1422
export const resolveSymlinkTarget = (filePath: string) =>
1523
Effect.gen(function* () {
1624
const fs = yield* FileSystem.FileSystem;
1725
const path = yield* Path.Path;
1826
let current = path.resolve(filePath);
1927
for (let hop = 0; hop < MAX_SYMLINK_HOPS; hop++) {
20-
const link = yield* fs.readLink(current).pipe(Effect.option);
21-
if (link._tag === "None") {
28+
const link = yield* fs.readLink(current).pipe(
29+
Effect.map(Option.some),
30+
Effect.catchIf(isNotASymlink, () => Effect.succeedNone),
31+
);
32+
if (Option.isNone(link)) {
2233
return current;
2334
}
24-
current = path.resolve(path.dirname(current), link.value);
35+
// A relative target is relative to where the link really lives, which
36+
// differs from its lexical parent when that parent is itself a symlink.
37+
const linkDirectory = yield* fs
38+
.realPath(path.dirname(current))
39+
.pipe(Effect.catchIf(isNotFound, () => Effect.succeed(path.dirname(current))));
40+
current = path.resolve(linkDirectory, link.value);
2541
}
2642
return yield* PlatformError.systemError({
2743
_tag: "Unknown",

0 commit comments

Comments
 (0)