// The workspace: the one folder file tools may touch. // // Every path the model sends is untrusted. Before any file is opened, `resolve()`: // 1. rejects empty paths and NUL bytes, // 2. resolves the path against the workspace root and rejects anything outside it // (`../..`, absolute paths elsewhere), // 3. walks the path one component at a time: every component that exists and is a symlink // must resolve to a place inside the workspace. A dangling link is refused too, because // writing through it would create its target, wherever that is, // 4. rejects names that usually hold secrets or that programs execute from (.env files, keys, // .ssh, .git internals, git and tool config). import { lstat, realpath, stat } from 'node:fs/promises'; import path from 'node:path'; export class WorkspaceError extends Error {} const SECRET_NAMES: RegExp[] = [ /^\.env(\..*)?$/i, /^\.ssh$/i, /^\.git$/i, /^\.aws$/i, /^id_(rsa|ed25519|ecdsa|dsa)(\.pub)?$/i, /\.(pem|key|p12|pfx)$/i, /^\.npmrc$/i, /^\.netrc$/i, // Config that programs read and act on (git runs core.fsmonitor, hooks, and filters from it). /^\.gitconfig$/i, /^\.gitmodules$/i, /^\.config$/i, ]; export function isSecretName(name: string): boolean { return SECRET_NAMES.some((pattern) => pattern.test(name)); } export class Workspace { readonly root: string; constructor(root: string) { this.root = path.resolve(root); } /** Resolves a model-supplied path to an absolute path inside the workspace, or throws. */ async resolve(requested: string): Promise { if (!requested || requested.includes('\0')) throw new WorkspaceError('empty or invalid path'); const absolute = path.resolve(this.root, requested); if (!isInside(this.root, absolute)) { throw new WorkspaceError(`${requested} is outside the workspace`); } const relative = path.relative(this.root, absolute); for (const segment of relative.split(path.sep)) { if (segment && isSecretName(segment)) { throw new WorkspaceError(`${requested} looks like a secret or private file; not allowed`); } } // Symlinks: check every component that exists, from the root down. const realRoot = await realpath(this.root); let probe = this.root; for (const segment of relative.split(path.sep).filter(Boolean)) { probe = path.join(probe, segment); let info; try { info = await lstat(probe); } catch { break; // Does not exist yet: nothing further down can be a link. } if (!info.isSymbolicLink()) continue; let real: string; try { real = await realpath(probe); } catch { throw new WorkspaceError( `${requested} goes through a link that points nowhere; not allowed`, ); } if (!isInside(realRoot, real)) { throw new WorkspaceError(`${requested} leads outside the workspace through a link`); } } return absolute; } /** Path relative to the workspace root, with forward slashes, for display. */ display(absolute: string): string { return path.relative(this.root, absolute).split(path.sep).join('/') || '.'; } async exists(): Promise { try { return (await stat(this.root)).isDirectory(); } catch { return false; } } } function isInside(root: string, target: string): boolean { const relative = path.relative(root, target); return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative)); }