Skip to content

Commit a8091db

Browse files
fix(server): resolve symlinks before listing a picker directory
1 parent 1779d93 commit a8091db

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import * as NodeFSP from "node:fs/promises";
33
import * as NodeServices from "@effect/platform-node/NodeServices";
44
import { FileFinder } from "@ff-labs/fff-node";
5-
import * as NodeFSP from "node:fs/promises";
65
import { it, afterEach, describe, expect } from "@effect/vitest";
76
import * as Effect from "effect/Effect";
87
import * as FileSystem from "effect/FileSystem";
@@ -821,6 +820,32 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => {
821820
}),
822821
);
823822

823+
it.effect("does not read a directory selected through a network link", () =>
824+
Effect.gen(function* () {
825+
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
826+
const path = yield* Path.Path;
827+
const fs = yield* FileSystem.FileSystem;
828+
const cwd = yield* fs.realPath(yield* makeTempDir({ prefix: "t3code-picker-selected-" }));
829+
const target = yield* makeTempDir({ prefix: "t3code-picker-local-" });
830+
const link = path.join(cwd, "network");
831+
yield* Effect.tryPromise(() => NodeFSP.symlink(target, link, "junction"));
832+
const originalReadlink = NodeFSP.readlink;
833+
using _readlink = vi
834+
.spyOn(NodeFSP, "readlink")
835+
.mockImplementation((file, options) =>
836+
String(file) === link
837+
? Promise.resolve(String.raw`\\host\share`)
838+
: originalReadlink(file, options),
839+
);
840+
using readdir = vi.spyOn(NodeFSP, "readdir");
841+
const result = yield* workspaceEntries.browse({
842+
partialPath: yield* appendSeparator(link),
843+
});
844+
expect(result).toEqual({ parentPath: link, entries: [] });
845+
expect(readdir).not.toHaveBeenCalled();
846+
}),
847+
);
848+
824849
it.effect("omits direct and chained network links without statting their targets", () =>
825850
Effect.gen(function* () {
826851
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,14 @@ export const make = Effect.gen(function* () {
245245
const parentPath = endsWithSeparator ? resolvedInputPath : path.dirname(resolvedInputPath);
246246
const prefix = endsWithSeparator ? "" : path.basename(resolvedInputPath);
247247

248+
// A directly typed network path stays browsable; only links must not redirect to one.
249+
const readPath = isNetworkPath(parentPath)
250+
? parentPath
251+
: yield* resolveBrowseLink(parentPath).pipe(Effect.orElseSucceed(() => parentPath));
252+
if (readPath === undefined) return { parentPath, entries: [] };
253+
248254
const dirents = yield* Effect.tryPromise({
249-
try: () => NodeFSP.readdir(parentPath, { withFileTypes: true }),
255+
try: () => NodeFSP.readdir(readPath, { withFileTypes: true }),
250256
catch: (cause) =>
251257
new WorkspaceEntriesReadDirectoryError({
252258
cwd: input.cwd,

0 commit comments

Comments
 (0)