1- // @effect -diagnostics nodeBuiltinImport:off - FileSystem cannot create a FIFO.
1+ // @effect -diagnostics nodeBuiltinImport:off - FileSystem cannot create a FIFO, and reads spy on native realpath .
22import * as NodeChildProcess from "node:child_process" ;
3+ import * as NodeFSP from "node:fs/promises" ;
34
5+ import * as NodePath from "@effect/platform-node/NodePath" ;
46import * as NodeServices from "@effect/platform-node/NodeServices" ;
57import { it , describe , expect } from "@effect/vitest" ;
68import * as Effect from "effect/Effect" ;
@@ -17,6 +19,12 @@ import * as WorkspaceFileSystem from "./WorkspaceFileSystem.ts";
1719import * as WorkspacePaths from "./WorkspacePaths.ts" ;
1820import * as HostProcess from "@t3tools/shared/HostProcess" ;
1921import { symlinksSupported } from "@t3tools/shared/testing/symlinks" ;
22+ import { vi } from "vite-plus/test" ;
23+
24+ vi . mock ( "node:fs/promises" , async ( importOriginal ) => {
25+ const actual = await importOriginal < typeof NodeFSP > ( ) ;
26+ return { ...actual , realpath : vi . fn ( actual . realpath ) } ;
27+ } ) ;
2028
2129// Temp folders count as known workspaces unless named as unknown.
2230const UNKNOWN_WORKSPACE_PREFIX = "t3code-unknown-workspace-" ;
@@ -123,6 +131,35 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
123131 ] ) ;
124132 } ) ,
125133 ) ;
134+
135+ // A file chip for `file://attacker.test/s/a.ts` in an agent's message sent
136+ // the Windows user's NTLM credentials to attacker.test as it rendered.
137+ it . effect ( "never stats a network path" , ( ) =>
138+ Effect . gen ( function * ( ) {
139+ const fs = yield * FileSystem . FileSystem ;
140+ const statted : Array < string > = [ ] ;
141+ const service = yield * WorkspaceFileSystem . make . pipe (
142+ Effect . provideService (
143+ FileSystem . FileSystem ,
144+ FileSystem . FileSystem . of ( {
145+ ...fs ,
146+ stat : ( requestedPath ) => {
147+ statted . push ( requestedPath ) ;
148+ return fs . stat ( requestedPath ) ;
149+ } ,
150+ } ) ,
151+ ) ,
152+ Effect . provide ( layerKnownWorkspacesTest ) ,
153+ ) ;
154+ const paths = [
155+ "\\\\attacker.test\\s\\a.ts" ,
156+ "//attacker.test/s/a.ts" ,
157+ "/\\attacker.test\\s" ,
158+ ] ;
159+ expect ( ( yield * service . getMetadata ( { paths } ) ) . entries ) . toEqual ( [ null , null , null ] ) ;
160+ expect ( statted ) . toEqual ( [ ] ) ;
161+ } ) ,
162+ ) ;
126163 } ) ;
127164
128165 describe ( "readFile" , ( ) => {
@@ -199,6 +236,68 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
199236 } ) ,
200237 ) ;
201238
239+ it . effect ( "refuses a network path outside the workspace" , ( ) =>
240+ Effect . gen ( function * ( ) {
241+ const workspaceFileSystem = yield * WorkspaceFileSystem . WorkspaceFileSystem ;
242+
243+ const error = yield * workspaceFileSystem
244+ . readFile ( { cwd : "//nas.test/share/project" , relativePath : "//attacker.test/s/a.md" } )
245+ . pipe ( Effect . flip ) ;
246+
247+ expect ( error ) . toBeInstanceOf ( WorkspacePaths . WorkspacePathOutsideRootError ) ;
248+ } ) ,
249+ ) ;
250+
251+ // This resolves inside the project, but Windows reads `\\?\` paths as
252+ // written, so realpath on the raw string signed in to attacker.test.
253+ it . effect ( "reads the network path it checked" , ( ) =>
254+ Effect . gen ( function * ( ) {
255+ const workspaceFileSystem = yield * WorkspaceFileSystem . make . pipe (
256+ Effect . provide ( Layer . merge ( layerKnownWorkspacesTest , NodePath . layerWin32 ) ) ,
257+ ) ;
258+ // Rejects without touching the filesystem, so a Windows runner never looks the share up.
259+ const realpath = vi
260+ . mocked ( NodeFSP . realpath )
261+ . mockClear ( )
262+ . mockRejectedValueOnce ( Object . assign ( new Error ( "ENOENT" ) , { code : "ENOENT" } ) ) ;
263+
264+ yield * workspaceFileSystem
265+ . readFile ( {
266+ cwd : "\\\\?\\UNC\\nas.test\\share\\project" ,
267+ relativePath : "\\\\?\\UNC\\attacker.test\\s\\..\\..\\nas.test\\share\\project\\a.md" ,
268+ } )
269+ . pipe ( Effect . exit ) ;
270+
271+ expect ( realpath . mock . calls . map ( ( [ target ] ) => target ) ) . toEqual ( [
272+ "\\\\?\\UNC\\nas.test\\share\\project\\a.md" ,
273+ ] ) ;
274+ } ) ,
275+ ) ;
276+
277+ // Resolving `..` before realpath would read beside the link instead of its target.
278+ // Windows collapses `..` before the filesystem sees a path, so it has no such reads.
279+ it . effect . skipIf ( ! symlinksSupported || HostProcess . Platform . defaultValue ( ) === "win32" ) (
280+ "reads `..` after a link from the link's target" ,
281+ ( ) =>
282+ Effect . gen ( function * ( ) {
283+ const workspaceFileSystem = yield * WorkspaceFileSystem . WorkspaceFileSystem ;
284+ const fs = yield * FileSystem . FileSystem ;
285+ const path = yield * Path . Path ;
286+ const root = yield * makeTempDir ;
287+ yield * writeTextFile ( root , "target/parent.md" , "beside the target\n" ) ;
288+ yield * writeTextFile ( root , "parent.md" , "beside the link\n" ) ;
289+ yield * fs . makeDirectory ( path . join ( root , "target" , "child" ) ) ;
290+ yield * fs . symlink ( path . join ( root , "target" , "child" ) , path . join ( root , "link" ) ) ;
291+
292+ const result = yield * workspaceFileSystem . readFile ( {
293+ cwd : root ,
294+ relativePath : `${ root } /link/../parent.md` ,
295+ } ) ;
296+
297+ expect ( result . contents ) . toBe ( "beside the target\n" ) ;
298+ } ) ,
299+ ) ;
300+
202301 it . effect . skipIf ( ! symlinksSupported ) (
203302 "rejects symlinks that resolve outside the workspace root" ,
204303 ( ) =>
0 commit comments