From 30d363ed91098ffe941e75339f09b3fdd9325398 Mon Sep 17 00:00:00 2001 From: Adam <2363879+adamdotdevin@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:21:03 -0600 Subject: [PATCH] fix(core): check read rules on the real path of a skill file --- packages/core/src/tool/plugin/read.ts | 34 +++++++++++---- packages/core/test/tool-read.test.ts | 62 +++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 11 deletions(-) diff --git a/packages/core/src/tool/plugin/read.ts b/packages/core/src/tool/plugin/read.ts index 95ee920b508..df82dbea376 100644 --- a/packages/core/src/tool/plugin/read.ts +++ b/packages/core/src/tool/plugin/read.ts @@ -100,9 +100,13 @@ export const Plugin = { save: [skillFile.skill.id], ...Permission.invocation(context), }) + // Read rules apply to the file opened as well as the path asked for, so a link inside the skill + // can't reach `.env` past a rule that denies it. yield* permission.assert({ action: "read", - resources: [skillFile.absolute.replaceAll("\\", "/")], + resources: Array.from( + new Set([skillFile.absolute, skillFile.target].map((item) => item.replaceAll("\\", "/"))), + ), save: ["*"], ...Permission.invocation(context), }) @@ -220,12 +224,23 @@ export const Plugin = { FSUtil.contains(dirname(item.path), absolute), ) if (!skill) return - // Resolve links so a symlink inside the skill can't reach a file outside it. - const target = AbsolutePath.make(yield* fs.resolve(absolute)) - if (!FSUtil.contains(yield* fs.resolve(dirname(skill.path)), target)) return - if (basename(target) === SkillDiscovery.versionFile) return + // Compare real on-disk paths, with their real casing, so a link or a differently cased name inside the skill + // can't reach a file outside it or the version marker. + const target = AbsolutePath.make(yield* realPath(absolute)) + if (!FSUtil.contains(yield* realPath(dirname(skill.path)), target)) return + if (isVersionFile(target)) return return { skill, absolute, target } }) + // A missing file has no real path, so resolve its nearest existing ancestor and keep the rest as written. + const realPath = (file: string): Effect.Effect => + fs.realPath(file).pipe( + Effect.catch(() => { + const parent = dirname(file) + return parent === file + ? Effect.succeed(file) + : realPath(parent).pipe(Effect.map((real) => join(real, basename(file)))) + }), + ) const host = Environment.makeFiles( // Reading never spawns a process, so these host files need no spawner. Environment.makeLocalDriver(ChildProcessSpawner.make(() => Effect.die("host skill files never spawn"))), @@ -233,9 +248,7 @@ export const Plugin = { const skillFiles: Environment.Files = { ...host, list: (directory) => - host - .list(directory) - .pipe(Effect.map((entries) => entries.filter((entry) => entry.name !== SkillDiscovery.versionFile))), + host.list(directory).pipe(Effect.map((entries) => entries.filter((entry) => !isVersionFile(entry.name)))), } const alternatePath = Effect.fn("ReadTool.alternatePath")(function* (absolute: string) { @@ -308,6 +321,11 @@ export const Plugin = { }), } +// Case-insensitive file systems open the marker under any casing. +function isVersionFile(file: string) { + return basename(file).toLowerCase() === SkillDiscovery.versionFile +} + function parseSkillUri(value: string) { const trimmed = value.slice("skill://".length).replace(/\/+$/, "") const slash = trimmed.indexOf("/") diff --git a/packages/core/test/tool-read.test.ts b/packages/core/test/tool-read.test.ts index 04f7cc67ab3..4540f3700b7 100644 --- a/packages/core/test/tool-read.test.ts +++ b/packages/core/test/tool-read.test.ts @@ -1198,6 +1198,8 @@ describe("ReadTool", () => { await fs.writeFile(path.join(root, ".opencode-version"), "1") await fs.writeFile(path.join(tmp.path, "secret.md"), "secret") await fs.symlink(path.join(tmp.path, "secret.md"), path.join(root, "escape.md")) + await fs.writeFile(path.join(root, ".env"), "TOKEN=secret") + await fs.symlink(path.join(root, ".env"), path.join(root, "notes.md")) }) yield* skills.transform((editor) => editor.add( @@ -1230,7 +1232,7 @@ describe("ReadTool", () => { content: [ { type: "text", - text: `Read directory ${root}, entries 1-4\nreferences${path.sep}\nescape.md\nlogo.png\nSKILL.md`, + text: `Read directory ${root}, entries 1-6\nreferences${path.sep}\n.env\nescape.md\nlogo.png\nnotes.md\nSKILL.md`, }, ], }) @@ -1242,8 +1244,12 @@ describe("ReadTool", () => { { sessionID, action: "read", resources: [guide.replaceAll("\\", "/")], save: ["*"] }, ]) - // The version marker and a link out of the skill get no skill exemption. - for (const file of [path.join(root, ".opencode-version"), path.join(root, "escape.md")]) { + // The version marker, under any casing, and a link out of the skill get no skill exemption. + for (const file of [ + path.join(root, ".opencode-version"), + path.join(root, ".OPENCODE-VERSION"), + path.join(root, "escape.md"), + ]) { assertions.length = 0 yield* read(file) expect(assertions[0]).toMatchObject({ action: "external_directory" }) @@ -1259,6 +1265,56 @@ describe("ReadTool", () => { status: "error", error: { type: "permission.rejected", message: "Permission denied: read" }, }) + + // Read rules see the file actually opened: a link inside the skill, or another casing of the name on a + // case-insensitive disk, can't reach `.env` past a rule that denies it. + const real = yield* Effect.promise(() => fs.realpath(root)) + deniedResource = path.join(real, ".env").replaceAll("\\", "/") + const insensitive = yield* Effect.promise(() => + fs.stat(path.join(root, ".ENV")).then( + () => true, + () => false, + ), + ) + for (const file of [path.join(root, "notes.md"), ...(insensitive ? [path.join(root, ".ENV")] : [])]) + expect(yield* read(file)).toEqual({ + status: "error", + error: { type: "permission.rejected", message: "Permission denied: read" }, + }) + }), + ) + + it.effect("keeps the skill exemption for a missing file in a skill whose directory is a link", () => + Effect.gen(function* () { + const skills = yield* Skill.Service + const registry = yield* Tool.Service + const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir())) + const linked = path.join(tmp.path, "skills", "lint") + yield* Effect.promise(async () => { + await fs.mkdir(path.join(tmp.path, "dotfiles", "lint"), { recursive: true }) + await fs.mkdir(path.dirname(linked), { recursive: true }) + await fs.writeFile(path.join(tmp.path, "dotfiles", "lint", "SKILL.md"), "# Lint") + await fs.symlink(path.join(tmp.path, "dotfiles", "lint"), linked) + }) + yield* skills.transform((editor) => + editor.add( + Skill.Info.make({ + id: Skill.ID.make("lint"), + name: Skill.Name.make("Lint"), + path: AbsolutePath.make(path.join(linked, "SKILL.md")), + content: "# Lint", + }), + ), + ) + const missing = path.join(linked, "missing.md") + expect( + yield* executeTool(registry, { + sessionID, + ...toolIdentity, + call: { type: "tool-call", id: "call-missing", name: "read", input: { path: missing } }, + }), + ).toEqual({ status: "error", error: { type: "tool.execution", message: `File not found: ${missing}` } }) + expect(assertions.map((item) => item.action)).toEqual(["skill", "read"]) }), ) })