Skip to content

Commit 8dfd61c

Browse files
committed
fix(server): inspect intermediate project picker symlinks
1 parent 67e2b39 commit 8dfd61c

2 files changed

Lines changed: 137 additions & 19 deletions

File tree

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

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,10 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
621621
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
622622
const path = yield* Path.Path;
623623
const cwd = yield* makeTempDir({ prefix: "t3code-picker-link-errors-" });
624-
const target = yield* makeTempDir({ prefix: "t3code-picker-link-target-" });
624+
const fs = yield* FileSystem.FileSystem;
625+
const target = yield* fs.realPath(
626+
yield* makeTempDir({ prefix: "t3code-picker-link-target-" }),
627+
);
625628
yield* writeTextFile(cwd, "plain-file.txt");
626629
yield* writeTextFile(cwd, "ordinary/package.json", "{}");
627630
for (const name of [
@@ -631,24 +634,25 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
631634
"linked-loop",
632635
"linked-denied",
633636
]) {
637+
yield* fs.makeDirectory(path.join(target, name));
634638
yield* Effect.tryPromise(() =>
635-
NodeFSP.symlink(target, path.join(cwd, name), "junction"),
639+
NodeFSP.symlink(path.join(target, name), path.join(cwd, name), "junction"),
636640
);
637641
}
638642
const originalStat = NodeFSP.stat;
639643
const fileStat = yield* Effect.tryPromise(() =>
640644
originalStat(path.join(cwd, "plain-file.txt")),
641645
);
642646
const failures = new Map([
643-
[path.join(cwd, "linked-broken"), "ENOENT"],
644-
[path.join(cwd, "linked-loop"), "ELOOP"],
645-
[path.join(cwd, "linked-denied"), "EACCES"],
647+
[path.join(target, "linked-broken"), "ENOENT"],
648+
[path.join(target, "linked-loop"), "ELOOP"],
649+
[path.join(target, "linked-denied"), "EACCES"],
646650
]);
647651
// Inject target states so this also runs on Windows without file-symlink privileges.
648652
using _stat = vi.spyOn(NodeFSP, "stat").mockImplementation((file, options) => {
649653
const code = failures.get(String(file));
650654
if (code) return Promise.reject(Object.assign(new Error(code), { code }));
651-
if (String(file) === path.join(cwd, "linked-file")) return Promise.resolve(fileStat);
655+
if (String(file) === path.join(target, "linked-file")) return Promise.resolve(fileStat);
652656
return originalStat(file, options);
653657
});
654658
const result = yield* workspaceEntries.browse({
@@ -665,7 +669,8 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
665669
Effect.gen(function* () {
666670
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
667671
const path = yield* Path.Path;
668-
const cwd = yield* makeTempDir({ prefix: "t3code-picker-network-" });
672+
const fs = yield* FileSystem.FileSystem;
673+
const cwd = yield* fs.realPath(yield* makeTempDir({ prefix: "t3code-picker-network-" }));
669674
const target = yield* makeTempDir({ prefix: "t3code-picker-local-" });
670675
const direct = path.join(cwd, "network");
671676
const chained = path.join(cwd, "chained");
@@ -696,6 +701,96 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
696701
}),
697702
);
698703

704+
it.effect(
705+
"rejects a network redirect in an intermediate target component before probing it",
706+
() =>
707+
Effect.gen(function* () {
708+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
709+
const path = yield* Path.Path;
710+
const fs = yield* FileSystem.FileSystem;
711+
const root = yield* fs.realPath(
712+
yield* makeTempDir({ prefix: "t3code-picker-component-" }),
713+
);
714+
const cwd = path.join(root, "projects");
715+
const target = path.join(root, "target");
716+
yield* fs.makeDirectory(cwd);
717+
yield* fs.makeDirectory(path.join(target, "project"), { recursive: true });
718+
const redirect = path.join(root, "redirect");
719+
const localRedirect = path.join(root, "local-redirect");
720+
for (const link of [redirect, localRedirect]) {
721+
yield* Effect.tryPromise(() => NodeFSP.symlink(target, link, "junction"));
722+
}
723+
const blocked = path.join(cwd, "blocked");
724+
const local = path.join(cwd, "local");
725+
const unsafeTarget = path.join(redirect, "project");
726+
yield* Effect.tryPromise(() => NodeFSP.symlink(unsafeTarget, blocked, "junction"));
727+
yield* Effect.tryPromise(() =>
728+
NodeFSP.symlink(path.join(localRedirect, "project"), local, "junction"),
729+
);
730+
const originalReadlink = NodeFSP.readlink;
731+
const networkTarget = String.raw`\\host\share`;
732+
using _readlink = vi
733+
.spyOn(NodeFSP, "readlink")
734+
.mockImplementation((file, options) =>
735+
String(file) === redirect
736+
? Promise.resolve(networkTarget)
737+
: originalReadlink(file, options),
738+
);
739+
using stat = vi.spyOn(NodeFSP, "stat");
740+
using lstat = vi.spyOn(NodeFSP, "lstat");
741+
const result = yield* workspaceEntries.browse({
742+
partialPath: yield* appendSeparator(cwd),
743+
});
744+
expect(result.entries).toEqual([{ name: "local", fullPath: local }]);
745+
expect(lstat.mock.calls.map(([file]) => String(file))).not.toContain(unsafeTarget);
746+
expect(lstat.mock.calls.map(([file]) => String(file))).not.toContain(networkTarget);
747+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(blocked);
748+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(local);
749+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(unsafeTarget);
750+
expect(stat.mock.calls.map(([file]) => String(file))).toContain(
751+
path.join(target, "project"),
752+
);
753+
}),
754+
);
755+
756+
it.effect("resolves parent components after local links and bounds cyclic links", () =>
757+
Effect.gen(function* () {
758+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
759+
const path = yield* Path.Path;
760+
const fs = yield* FileSystem.FileSystem;
761+
const root = yield* fs.realPath(yield* makeTempDir({ prefix: "t3code-picker-relative-" }));
762+
const cwd = path.join(root, "projects");
763+
const target = path.join(root, "physical", "sibling");
764+
const via = path.join(root, "via");
765+
yield* fs.makeDirectory(cwd);
766+
yield* fs.makeDirectory(target, { recursive: true });
767+
yield* fs.makeDirectory(path.join(root, "physical", "child"));
768+
yield* Effect.tryPromise(() =>
769+
NodeFSP.symlink(path.join(root, "physical", "child"), via, "junction"),
770+
);
771+
const relative = path.join(cwd, "relative");
772+
const cycle = path.join(cwd, "cycle");
773+
for (const link of [relative, cycle]) {
774+
yield* Effect.tryPromise(() => NodeFSP.symlink(target, link, "junction"));
775+
}
776+
const originalReadlink = NodeFSP.readlink;
777+
// Junction fixtures avoid Windows symlink privileges; inject relative and cyclic targets.
778+
using readlink = vi.spyOn(NodeFSP, "readlink").mockImplementation((file, options) => {
779+
if (String(file) === relative)
780+
return Promise.resolve(["..", "via", "..", "sibling"].join(path.sep));
781+
if (String(file) === cycle) return Promise.resolve(cycle);
782+
return originalReadlink(file, options);
783+
});
784+
using stat = vi.spyOn(NodeFSP, "stat");
785+
const result = yield* workspaceEntries.browse({ partialPath: yield* appendSeparator(cwd) });
786+
expect(result.entries).toEqual([{ name: "relative", fullPath: relative }]);
787+
expect(stat.mock.calls.map(([file]) => String(file))).toContain(target);
788+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(relative);
789+
expect(stat.mock.calls.map(([file]) => String(file))).not.toContain(cycle);
790+
expect(readlink.mock.calls.filter(([file]) => String(file) === cycle)).toHaveLength(40);
791+
}),
792+
);
793+
699794
it.effect("shows dot directories in directory mode and hidden-prefix mode", () =>
700795
Effect.gen(function* () {
701796
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;

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

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,41 @@ export const make = Effect.gen(function* () {
198198
},
199199
);
200200

201-
const canFollowBrowseLink = Effect.fn("WorkspaceEntries.canFollowBrowseLink")(function* (
201+
const resolveBrowseLink = Effect.fn("WorkspaceEntries.resolveBrowseLink")(function* (
202202
fullPath: string,
203203
) {
204-
let currentPath = fullPath;
205-
for (let hop = 0; hop < 40; hop++) {
206-
const target = yield* Effect.tryPromise(() => NodeFSP.readlink(currentPath));
207-
// Inspect link targets before following them: UNC paths can trigger Windows NTLM sign-in.
208-
if (isNetworkPath(target)) return false;
209-
currentPath = path.resolve(path.dirname(currentPath), target);
210-
const stats = yield* Effect.tryPromise(() => NodeFSP.lstat(currentPath));
211-
if (!stats.isSymbolicLink()) return true;
204+
if (isNetworkPath(fullPath)) return undefined;
205+
const separators = path.sep === "\\" ? /[\\/]+/ : /\/+/;
206+
let currentPath = path.parse(fullPath).root;
207+
const remaining = fullPath.slice(currentPath.length).split(separators);
208+
let hops = 0;
209+
while (remaining.length > 0) {
210+
const component = remaining.shift()!;
211+
if (component === "" || component === ".") continue;
212+
if (component === "..") {
213+
currentPath = path.dirname(currentPath);
214+
continue;
215+
}
216+
const nextPath = path.join(currentPath, component);
217+
const stats = yield* Effect.tryPromise(() => NodeFSP.lstat(nextPath));
218+
if (!stats.isSymbolicLink()) {
219+
if (remaining.length > 0 && !stats.isDirectory()) return undefined;
220+
currentPath = nextPath;
221+
continue;
222+
}
223+
if (++hops > 40) return undefined;
224+
const target = yield* Effect.tryPromise(() => NodeFSP.readlink(nextPath));
225+
// Inspect every link before following it: UNC paths can trigger Windows NTLM sign-in.
226+
if (isNetworkPath(target)) return undefined;
227+
const root = path.parse(target).root;
228+
if (root) {
229+
if (!path.isAbsolute(target)) return undefined;
230+
currentPath = path.parse(path.resolve(currentPath, root)).root;
231+
}
232+
// Expand before processing '..' so it applies to the resolved directory.
233+
remaining.unshift(...target.slice(root.length).split(separators));
212234
}
213-
return false;
235+
return currentPath;
214236
});
215237

216238
const browse: WorkspaceEntries["Service"]["browse"] = Effect.fn("WorkspaceEntries.browse")(
@@ -253,8 +275,9 @@ export const make = Effect.gen(function* () {
253275
const entry = { name: dirent.name, fullPath: path.join(parentPath, dirent.name) };
254276
if (dirent.isDirectory()) return Effect.succeed(entry);
255277
return Effect.gen(function* () {
256-
if (!(yield* canFollowBrowseLink(entry.fullPath))) return undefined;
257-
const stats = yield* Effect.tryPromise(() => NodeFSP.stat(entry.fullPath));
278+
const target = yield* resolveBrowseLink(entry.fullPath);
279+
if (target === undefined) return undefined;
280+
const stats = yield* Effect.tryPromise(() => NodeFSP.stat(target));
258281
return stats.isDirectory() ? entry : undefined;
259282
}).pipe(Effect.orElseSucceed(() => undefined));
260283
},

0 commit comments

Comments
 (0)