Skip to content

Commit a01081d

Browse files
committed
fix(server): inspect symlink chains before following targets
1 parent 45ec7ea commit a01081d

3 files changed

Lines changed: 90 additions & 9 deletions

File tree

‎apps/server/src/workspace/WorkspaceEntries.test.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// @effect-diagnostics nodeBuiltinImport:off
2+
import * as NodeFSP from "node:fs/promises";
3+
24
import * as NodeServices from "@effect/platform-node/NodeServices";
35
import { it, afterEach, describe, expect } from "@effect/vitest";
46
import * as Effect from "effect/Effect";
@@ -17,7 +19,14 @@ import * as WorkspacePaths from "./WorkspacePaths.ts";
1719

1820
vi.mock("node:fs/promises", async (importOriginal) => {
1921
const actual = await importOriginal<typeof import("node:fs/promises")>();
20-
return { ...actual, readdir: vi.fn(actual.readdir) };
22+
return {
23+
...actual,
24+
readdir: vi.fn(actual.readdir),
25+
stat: vi.fn(actual.stat),
26+
readlink: vi.fn(actual.readlink),
27+
realpath: vi.fn(actual.realpath),
28+
lstat: vi.fn(actual.lstat),
29+
};
2130
});
2231

2332
const layerTest = Layer.empty.pipe(
@@ -94,6 +103,44 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
94103
vi.restoreAllMocks();
95104
});
96105

106+
// quarantine: #19681 requires host support for creating symlinks.
107+
it.effect.skipIf(!symlinksSupported)(
108+
"never follows network targets while classifying links",
109+
() =>
110+
Effect.gen(function* () {
111+
const cwd = yield* makeTempDir();
112+
const fs = yield* FileSystem.FileSystem;
113+
const path = yield* Path.Path;
114+
yield* fs.makeDirectory(path.join(cwd, "storage"));
115+
yield* fs.symlink("storage", path.join(cwd, "network"));
116+
yield* fs.symlink("network", path.join(cwd, "chain"));
117+
yield* fs.symlink("network/child", path.join(cwd, "nested"));
118+
yield* fs.symlink("storage", path.join(cwd, "safe"));
119+
const actual = yield* Effect.promise(() =>
120+
vi.importActual<typeof NodeFSP>("node:fs/promises"),
121+
);
122+
using readlink = vi
123+
.spyOn(NodeFSP, "readlink")
124+
.mockImplementation(async (target, options) => {
125+
if (String(target).endsWith(`${path.sep}network`))
126+
return String.raw`\\attacker.test\share`;
127+
return actual.readlink(target, options);
128+
});
129+
using stat = vi.spyOn(NodeFSP, "stat");
130+
using realpath = vi.spyOn(NodeFSP, "realpath");
131+
using lstat = vi.spyOn(NodeFSP, "lstat");
132+
const entries = yield* WorkspaceEntries.WorkspaceEntries;
133+
const browse = yield* entries.browse({ partialPath: yield* appendSeparator(cwd) });
134+
expect(browse.entries.map((entry) => entry.name)).toEqual(["safe", "storage"]);
135+
const listed = yield* entries.list({ cwd, directoryPath: "" });
136+
expect(listed.entries.map((entry) => entry.path).toSorted()).toEqual(["safe", "storage"]);
137+
expect(stat.mock.calls).toEqual([]);
138+
for (const calls of [realpath.mock.calls, lstat.mock.calls, readlink.mock.calls]) {
139+
expect(calls.some(([target]) => /^[\\/]{2}/.test(String(target)))).toBe(false);
140+
}
141+
}),
142+
);
143+
97144
describe("list", () => {
98145
it.effect("lists immediate children including ignored and empty directories", () =>
99146
Effect.gen(function* () {
@@ -236,6 +283,7 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
236283
}),
237284
);
238285

286+
// quarantine: #19681 requires host support for creating symlinks.
239287
it.effect.skipIf(!symlinksSupported)("lists accessible directory and file links", () =>
240288
Effect.gen(function* () {
241289
const cwd = yield* makeTempDir();
@@ -264,6 +312,7 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
264312
}),
265313
);
266314

315+
// quarantine: #19681 requires host support for creating symlinks.
267316
it.effect.skipIf(!symlinksSupported)(
268317
"omits broken links, git links, and directory cycles",
269318
() =>
@@ -618,6 +667,7 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
618667
});
619668

620669
describe("browse", () => {
670+
// quarantine: #19681 requires host support for creating symlinks.
621671
it.effect.skipIf(!symlinksSupported)("offers linked directories when choosing a project", () =>
622672
Effect.gen(function* () {
623673
const cwd = yield* makeTempDir();

‎apps/server/src/workspace/WorkspaceEntries.ts‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,40 @@ export const make = Effect.gen(function* () {
200200
},
201201
);
202202

203-
// Per-entry filesystem calls share the browse/list span.
203+
// Inspect each link before following it: stat/realpath can authenticate to a UNC host on Windows.
204204
const entryType = Effect.fnUntraced(function* (parent: string, entry: NodeFS.Dirent) {
205-
if (!entry.isSymbolicLink()) return entry;
205+
const absolutePath = path.join(parent, entry.name);
206+
if (!entry.isSymbolicLink()) return { type: entry, realPath: absolutePath };
206207
return yield* Effect.tryPromise({
207-
try: () => NodeFSP.stat(path.join(parent, entry.name)),
208+
try: async () => {
209+
let current = path.parse(absolutePath).root;
210+
const splitSegments = (value: string) => value.split(path.sep === "\\" ? /[\\/]/ : "/");
211+
const segments = splitSegments(absolutePath.slice(current.length));
212+
let links = 0;
213+
while (segments.length > 0) {
214+
const segment = segments.shift()!;
215+
if (segment === "" || segment === ".") continue;
216+
if (segment === "..") {
217+
current = path.dirname(current);
218+
continue;
219+
}
220+
const candidate = path.join(current, segment);
221+
const type = await NodeFSP.lstat(candidate);
222+
if (type.isSymbolicLink()) {
223+
if (++links > 40) return null;
224+
const target = await NodeFSP.readlink(candidate);
225+
if (isNetworkPath(target)) return null;
226+
if (path.isAbsolute(target)) current = path.parse(target).root;
227+
const relativeTarget = path.isAbsolute(target) ? target.slice(current.length) : target;
228+
segments.unshift(...splitSegments(relativeTarget));
229+
continue;
230+
}
231+
current = candidate;
232+
if (segments.length === 0) return { type, realPath: current };
233+
if (!type.isDirectory()) return null;
234+
}
235+
return { type: await NodeFSP.lstat(current), realPath: current };
236+
},
208237
catch: () => null,
209238
}).pipe(Effect.orElseSucceed(() => null));
210239
});
@@ -245,8 +274,8 @@ export const make = Effect.gen(function* () {
245274
const entries = yield* Effect.forEach(
246275
candidates,
247276
Effect.fnUntraced(function* (entry) {
248-
const type = yield* entryType(parentPath, entry);
249-
return type?.isDirectory()
277+
const target = yield* entryType(parentPath, entry);
278+
return target?.type.isDirectory()
250279
? [{ name: entry.name, fullPath: path.join(parentPath, entry.name) }]
251280
: [];
252281
}),
@@ -355,12 +384,13 @@ export const make = Effect.gen(function* () {
355384
children,
356385
Effect.fnUntraced(function* (child) {
357386
if (child.name === ".git") return [];
358-
const type = yield* entryType(directory, child);
359-
if (!type || (!type.isDirectory() && !type.isFile())) return [];
387+
const resolved = yield* entryType(directory, child);
388+
if (!resolved) return [];
389+
const { type, realPath: real } = resolved;
390+
if (!type.isDirectory() && !type.isFile()) return [];
360391
if (child.isSymbolicLink()) {
361392
const allowed = yield* Effect.tryPromise({
362393
try: async () => {
363-
const real = await NodeFSP.realpath(path.join(directory, child.name));
364394
if (real.split(path.sep).includes(".git")) return false;
365395
if (isNetworkPath(real) && !KnownWorkspaces.isWithinRoot(path, real, root))
366396
return false;

‎apps/server/src/workspace/WorkspaceFileSystem.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
320320
}),
321321
);
322322

323+
// quarantine: #19681 requires host support for creating symlinks.
323324
it.effect.skipIf(!symlinksSupported)(
324325
"reads accessible file and directory links outside the workspace root",
325326
() =>

0 commit comments

Comments
 (0)