|
| 1 | +import * as os from "os" |
| 2 | +import * as path from "path" |
| 3 | +import * as fs from "fs/promises" |
| 4 | + |
| 5 | +import * as vscode from "vscode" |
| 6 | + |
| 7 | +import type { Task } from "../../task/Task" |
| 8 | +import { authorizeRead, authorizeWrite } from "../WorkspaceFileAccess" |
| 9 | + |
| 10 | +vi.mock("vscode", () => ({ |
| 11 | + workspace: { workspaceFolders: [] as { uri: { fsPath: string } }[] }, |
| 12 | +})) |
| 13 | + |
| 14 | +// Real symlinks in a real temp directory (no fs mocking, per #389/#390). Some scenarios can't be |
| 15 | +// reproduced everywhere: symlink creation needs privileges on Windows, and chmod-based EACCES is |
| 16 | +// meaningless as root. Such cases are skipped at runtime rather than mocked. |
| 17 | +const isWindows = process.platform === "win32" |
| 18 | +const isRoot = typeof process.getuid === "function" && process.getuid() === 0 |
| 19 | + |
| 20 | +/** Lowercase on case-insensitive filesystems, matching the resolver's own normalization. */ |
| 21 | +const expectCase = (p: string) => (process.platform === "darwin" || process.platform === "win32" ? p.toLowerCase() : p) |
| 22 | + |
| 23 | +/** Minimal Task stub exposing only what WorkspaceFileAccess reads. */ |
| 24 | +function makeTask(opts: { allow?: boolean; providerGone?: boolean; getStateThrows?: boolean } = {}): Task { |
| 25 | + const provider = { |
| 26 | + getState: async () => { |
| 27 | + if (opts.getStateThrows) { |
| 28 | + throw new Error("provider torn down") |
| 29 | + } |
| 30 | + return { allowSymlinksOutsideWorkspace: opts.allow } |
| 31 | + }, |
| 32 | + } |
| 33 | + return { |
| 34 | + providerRef: { deref: () => (opts.providerGone ? undefined : provider) }, |
| 35 | + } as unknown as Task |
| 36 | +} |
| 37 | + |
| 38 | +function setWorkspaceFolders(...paths: string[]) { |
| 39 | + ;(vscode.workspace as any).workspaceFolders = paths.map((p) => ({ uri: { fsPath: p } })) |
| 40 | +} |
| 41 | + |
| 42 | +describe("WorkspaceFileAccess", () => { |
| 43 | + let tmpRoot: string |
| 44 | + let workspace: string |
| 45 | + let outside: string |
| 46 | + let symlinksSupported = false |
| 47 | + |
| 48 | + beforeEach(async () => { |
| 49 | + // realpath the temp root so comparisons aren't tripped up by /var -> /private/var (macOS). |
| 50 | + tmpRoot = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), "zoo-wfa-"))) |
| 51 | + workspace = path.join(tmpRoot, "workspace") |
| 52 | + outside = path.join(tmpRoot, "outside") |
| 53 | + await fs.mkdir(workspace, { recursive: true }) |
| 54 | + await fs.mkdir(outside, { recursive: true }) |
| 55 | + setWorkspaceFolders(workspace) |
| 56 | + |
| 57 | + const probeTarget = path.join(tmpRoot, "probe-target") |
| 58 | + const probeLink = path.join(tmpRoot, "probe-link") |
| 59 | + await fs.writeFile(probeTarget, "probe") |
| 60 | + try { |
| 61 | + await fs.symlink(probeTarget, probeLink) |
| 62 | + symlinksSupported = true |
| 63 | + } catch { |
| 64 | + symlinksSupported = false |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + afterEach(async () => { |
| 69 | + await fs.chmod(path.join(workspace, "restricted"), 0o755).catch(() => {}) |
| 70 | + await fs.rm(tmpRoot, { recursive: true, force: true }).catch(() => {}) |
| 71 | + ;(vscode.workspace as any).workspaceFolders = [] |
| 72 | + }) |
| 73 | + |
| 74 | + it("authorizes a real file inside the workspace and returns its canonical path", async () => { |
| 75 | + const file = path.join(workspace, "file.txt") |
| 76 | + await fs.writeFile(file, "x") |
| 77 | + |
| 78 | + const result = await authorizeRead({ task: makeTask(), requestedPath: file, source: "read_file" }) |
| 79 | + |
| 80 | + expect(result.ok).toBe(true) |
| 81 | + if (result.ok) { |
| 82 | + expect(result.resolvedPath).toBe(expectCase(await fs.realpath(file))) |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + it("denies a symlink inside the workspace that escapes it (symlink_escapes_workspace)", async () => { |
| 87 | + if (!symlinksSupported) return |
| 88 | + const secret = path.join(outside, "secret.txt") |
| 89 | + await fs.writeFile(secret, "x") |
| 90 | + const link = path.join(workspace, "link.txt") |
| 91 | + await fs.symlink(secret, link) |
| 92 | + |
| 93 | + const result = await authorizeRead({ task: makeTask(), requestedPath: link, source: "read_file" }) |
| 94 | + |
| 95 | + expect(result).toMatchObject({ ok: false, reason: "symlink_escapes_workspace" }) |
| 96 | + }) |
| 97 | + |
| 98 | + it("allows an escaping symlink when allowSymlinksOutsideWorkspace is true", async () => { |
| 99 | + if (!symlinksSupported) return |
| 100 | + const secret = path.join(outside, "secret.txt") |
| 101 | + await fs.writeFile(secret, "x") |
| 102 | + const link = path.join(workspace, "link.txt") |
| 103 | + await fs.symlink(secret, link) |
| 104 | + |
| 105 | + const result = await authorizeRead({ task: makeTask({ allow: true }), requestedPath: link, source: "read_file" }) |
| 106 | + |
| 107 | + expect(result.ok).toBe(true) |
| 108 | + if (result.ok) { |
| 109 | + expect(result.resolvedPath).toBe(expectCase(await fs.realpath(secret))) |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + it("denies a path that is plainly outside the workspace (outside_workspace)", async () => { |
| 114 | + const file = path.join(outside, "file.txt") |
| 115 | + await fs.writeFile(file, "x") |
| 116 | + |
| 117 | + const result = await authorizeRead({ task: makeTask(), requestedPath: file, source: "read_file" }) |
| 118 | + |
| 119 | + expect(result).toMatchObject({ ok: false, reason: "outside_workspace" }) |
| 120 | + }) |
| 121 | + |
| 122 | + it("returns permission_denied when the path cannot be resolved due to EACCES", async () => { |
| 123 | + if (isWindows || isRoot) return |
| 124 | + const restricted = path.join(workspace, "restricted") |
| 125 | + await fs.mkdir(restricted) |
| 126 | + const target = path.join(restricted, "file.txt") |
| 127 | + await fs.writeFile(target, "x") |
| 128 | + await fs.chmod(restricted, 0o000) |
| 129 | + |
| 130 | + const result = await authorizeRead({ task: makeTask(), requestedPath: target, source: "read_file" }) |
| 131 | + |
| 132 | + expect(result).toMatchObject({ ok: false, reason: "permission_denied" }) |
| 133 | + }) |
| 134 | + |
| 135 | + it("authorizes a not-yet-created file under a symlinked ancestor that stays inside the workspace", async () => { |
| 136 | + if (!symlinksSupported) return |
| 137 | + const realDir = path.join(workspace, "real-dir") |
| 138 | + await fs.mkdir(realDir) |
| 139 | + const linkDir = path.join(workspace, "link-dir") |
| 140 | + await fs.symlink(realDir, linkDir) |
| 141 | + const newFile = path.join(linkDir, "not-created-yet.txt") |
| 142 | + |
| 143 | + const result = await authorizeWrite({ task: makeTask(), requestedPath: newFile, source: "write_to_file" }) |
| 144 | + |
| 145 | + expect(result.ok).toBe(true) |
| 146 | + if (result.ok) { |
| 147 | + expect(result.resolvedPath).toBe(expectCase(path.join(await fs.realpath(realDir), "not-created-yet.txt"))) |
| 148 | + } |
| 149 | + }) |
| 150 | + |
| 151 | + it("fails closed when the provider has been torn down (deref returns undefined)", async () => { |
| 152 | + if (!symlinksSupported) return |
| 153 | + const secret = path.join(outside, "secret.txt") |
| 154 | + await fs.writeFile(secret, "x") |
| 155 | + const link = path.join(workspace, "link.txt") |
| 156 | + await fs.symlink(secret, link) |
| 157 | + |
| 158 | + // providerGone => allowSymlinksOutsideWorkspace defaults to false => symlink is followed and blocked. |
| 159 | + const result = await authorizeRead({ |
| 160 | + task: makeTask({ providerGone: true }), |
| 161 | + requestedPath: link, |
| 162 | + source: "read_file", |
| 163 | + }) |
| 164 | + |
| 165 | + expect(result).toMatchObject({ ok: false, reason: "symlink_escapes_workspace" }) |
| 166 | + }) |
| 167 | + |
| 168 | + it("fails closed when reading provider state throws", async () => { |
| 169 | + if (!symlinksSupported) return |
| 170 | + const secret = path.join(outside, "secret.txt") |
| 171 | + await fs.writeFile(secret, "x") |
| 172 | + const link = path.join(workspace, "link.txt") |
| 173 | + await fs.symlink(secret, link) |
| 174 | + |
| 175 | + const result = await authorizeRead({ |
| 176 | + task: makeTask({ getStateThrows: true }), |
| 177 | + requestedPath: link, |
| 178 | + source: "read_file", |
| 179 | + }) |
| 180 | + |
| 181 | + expect(result).toMatchObject({ ok: false, reason: "symlink_escapes_workspace" }) |
| 182 | + }) |
| 183 | + |
| 184 | + it("fails closed when no workspace folder is open", async () => { |
| 185 | + ;(vscode.workspace as any).workspaceFolders = [] |
| 186 | + const file = path.join(workspace, "file.txt") |
| 187 | + await fs.writeFile(file, "x") |
| 188 | + |
| 189 | + const result = await authorizeWrite({ task: makeTask(), requestedPath: file, source: "write_to_file" }) |
| 190 | + |
| 191 | + expect(result).toMatchObject({ ok: false, reason: "outside_workspace" }) |
| 192 | + }) |
| 193 | +}) |
0 commit comments