fix(core): read downloaded skill files without external_directory approval

This commit is contained in:
Adam
2026-11-06 14:40:56 -06:00
Unverified
parent e05e2e4461
commit 22690b28e9
4 changed files with 169 additions and 2 deletions
+4 -1
View File
@@ -7,6 +7,7 @@ import { Context, Effect, Layer, Types } from "effect"
import { Skill } from "@opencode/schema/skill"
import { Bus } from "./bus.js"
import { Permission } from "./permission.js"
import { SkillDiscovery } from "./skill/discovery.js"
import { State } from "./state.js"
export const DirectorySource = Skill.DirectorySource
@@ -56,6 +57,8 @@ export const toModelOutput = (skill: Info, files: ReadonlyArray<string>) => {
].join("\n")
}
const hidden = new Set(["SKILL.md", SkillDiscovery.versionFile])
export const prepare = Effect.fn("Skill.prepare")(function* (
fs: FSUtil.Interface,
skill: Info,
@@ -83,7 +86,7 @@ export const prepare = Effect.fn("Skill.prepare")(function* (
dot: true,
ignore: FSUtil.ignoreGlobs,
}))
.filter((file) => path.basename(file) !== "SKILL.md")
.filter((file) => !hidden.has(path.basename(file)))
.toSorted()
.slice(0, 10)
: []
+65 -1
View File
@@ -1,9 +1,10 @@
export * as ReadTool from "./read.js"
import type { Context } from "@opencode/plugin/effect/plugin"
import { basename, dirname, join } from "path"
import { basename, dirname, join, parse } from "path"
import { ToolFailure } from "@opencode/ai/effect"
import { Effect, Schema } from "effect"
import { ChildProcessSpawner } from "effect/process"
import { FSUtil } from "@opencode/util/fs-util"
import { Location } from "../../location.js"
import { FileAccess } from "../../file-access.js"
@@ -91,6 +92,32 @@ export const Plugin = {
return { output, path: input.path }
}
const skillFile = yield* activeSkillFile(input.path)
if (skillFile) {
yield* permission.assert({
action: "skill",
resources: [skillFile.skill.id],
save: [skillFile.skill.id],
...Permission.invocation(context),
})
yield* permission.assert({
action: "read",
resources: [skillFile.absolute.replaceAll("\\", "/")],
save: ["*"],
...Permission.invocation(context),
})
const output = yield* ReadToolFileSystem.read(skillFiles, skillFile.target, skillFile.absolute, {
offset: input.offset,
limit: input.limit,
}).pipe(
Effect.catchIf(
(error) => error instanceof Environment.NotFound,
() => Effect.fail(new ToolFailure({ message: `File not found: ${input.path}` })),
),
)
return { output, path: input.path }
}
const read = (target: FileAccess.Target) =>
reader.read(target.absolute, target.resource, {
offset: input.offset,
@@ -174,6 +201,43 @@ export const Plugin = {
)
.pipe(Effect.orDie)
// A skill's supporting files sit beside its SKILL.md. A skill from a URL source is cached on this host, outside
// the project and, for a session in a remote workspace, outside its environment too. So a file under an active
// skill's directory outside the location reads from this host under the `skill` and `read` permissions, not
// `external_directory`.
const activeSkillFile = Effect.fn("ReadTool.activeSkillFile")(function* (file: string) {
const absolute = AbsolutePath.make(FileAccess.resolvePath(location.directory, file))
const project = location.project.directory
if (
FSUtil.contains(location.directory, absolute) ||
(project !== parse(project).root && FSUtil.contains(project, absolute))
)
return
const skill = (yield* skills.list()).find(
(item) =>
!Skill.isUri(item.path) &&
basename(item.path) === "SKILL.md" &&
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
return { skill, absolute, target }
})
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"))),
)
const skillFiles: Environment.Files = {
...host,
list: (directory) =>
host
.list(directory)
.pipe(Effect.map((entries) => entries.filter((entry) => entry.name !== SkillDiscovery.versionFile))),
}
const alternatePath = Effect.fn("ReadTool.alternatePath")(function* (absolute: string) {
const canonical = (name: string) =>
name
+16
View File
@@ -270,4 +270,20 @@ describe("Skill", () => {
expect(yield* skill.files(Skill.ID.make("review"))).toBeUndefined()
}),
)
it.effect("lists a skill's files without SKILL.md or a download's version marker", () =>
Effect.gen(function* () {
const fs = yield* FSUtil.Service
const skill = Skill.Info.make({ ...info("deploy", "Deploy"), path: AbsolutePath.make("/cache/deploy/SKILL.md") })
const prepared = yield* Skill.prepare(
{
...fs,
scan: () =>
Effect.succeed(["/cache/deploy/SKILL.md", "/cache/deploy/.opencode-version", "/cache/deploy/guide.md"]),
},
skill,
)
expect(prepared.output).toBe(Skill.toModelOutput(skill, ["/cache/deploy/guide.md"]))
}),
)
})
+84
View File
@@ -1,4 +1,5 @@
import { beforeEach, describe, expect } from "bun:test"
import fs from "fs/promises"
import path from "path"
import { Effect, Exit, Layer, Result } from "effect"
import { Config } from "@opencode/core/config"
@@ -15,6 +16,7 @@ import { AbsolutePath, RelativePath } from "@opencode/core/schema"
import { Global } from "@opencode/util/global"
import { FileAccess } from "@opencode/core/file-access"
import { location } from "./fixture/location"
import { tmpdir } from "./fixture/tmpdir"
import { Tool } from "@opencode/core/tool"
import { ReadTool } from "@opencode/core/tool/plugin/read"
import { ReadToolFileSystem } from "@opencode/core/tool/read-filesystem"
@@ -1177,4 +1179,86 @@ describe("ReadTool", () => {
)
}),
)
it.effect("reads an external skill's supporting files from this host under skill and read permissions", () =>
Effect.gen(function* () {
const skills = yield* Skill.Service
const registry = yield* Tool.Service
const tmp = yield* Effect.acquireDisposable(Effect.promise(() => tmpdir()))
const root = path.join(tmp.path, "cache", "deploy")
const png = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=",
"base64",
)
yield* Effect.promise(async () => {
await fs.mkdir(path.join(root, "references"), { recursive: true })
await fs.writeFile(path.join(root, "SKILL.md"), "# Deploy")
await fs.writeFile(path.join(root, "references", "guide.md"), "Line 1\nLine 2")
await fs.writeFile(path.join(root, "logo.png"), png)
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"))
})
yield* skills.transform((editor) =>
editor.add(
Skill.Info.make({
id: Skill.ID.make("deploy"),
name: Skill.Name.make("Deploy"),
path: AbsolutePath.make(path.join(root, "SKILL.md")),
content: "# Deploy",
}),
),
)
const read = (file: string) =>
executeTool(registry, {
sessionID,
...toolIdentity,
call: { type: "tool-call", id: `call-${file}`, name: "read", input: { path: file } },
})
const guide = path.join(root, "references", "guide.md")
expect(yield* read(guide)).toMatchObject({
status: "completed",
content: [{ type: "text", text: `Read file ${guide}, lines 1-2\n1: Line 1\n2: Line 2` }],
})
expect(yield* read(path.join(root, "logo.png"))).toMatchObject({
status: "completed",
output: { type: "file", encoding: "base64", mime: "image/png" },
})
expect(yield* read(root)).toMatchObject({
status: "completed",
content: [
{
type: "text",
text: `Read directory ${root}, entries 1-4\nreferences${path.sep}\nescape.md\nlogo.png\nSKILL.md`,
},
],
})
// The session's environment never served these reads, and no external_directory approval was asked.
expect(readCalls).toEqual([])
expect(assertions.map((item) => item.action)).toEqual(["skill", "read", "skill", "read", "skill", "read"])
expect(assertions.slice(0, 2)).toMatchObject([
{ sessionID, action: "skill", resources: ["deploy"], save: ["deploy"] },
{ 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")]) {
assertions.length = 0
yield* read(file)
expect(assertions[0]).toMatchObject({ action: "external_directory" })
}
deniedResource = "deploy"
expect(yield* read(guide)).toEqual({
status: "error",
error: { type: "permission.rejected", message: "Permission denied: skill" },
})
deniedResource = guide.replaceAll("\\", "/")
expect(yield* read(guide)).toEqual({
status: "error",
error: { type: "permission.rejected", message: "Permission denied: read" },
})
}),
)
})