mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
236 lines
10 KiB
TypeScript
236 lines
10 KiB
TypeScript
import { describe, expect } from "bun:test"
|
|
import { Document, Event, Info, type Entry } from "@opencode/schema/config"
|
|
import { ConfigPolicy } from "@opencode/schema/config/policy"
|
|
import { Permission } from "@opencode/schema/permission"
|
|
import { Config } from "@opencode/core/config"
|
|
import { Integration } from "@opencode/core/integration"
|
|
import { ConfigPolicyPlugin } from "@opencode/core/config/plugin/policy"
|
|
import { Bus } from "@opencode/core/bus"
|
|
import { Plugin } from "@opencode/core/plugin"
|
|
import { PluginHooks } from "@opencode/core/plugin/hooks"
|
|
import { PluginHost } from "@opencode/core/plugin/host"
|
|
import { Provider } from "@opencode/core/provider"
|
|
import { Session } from "@opencode/core/session"
|
|
import { Effect, Schema } from "effect"
|
|
import { testEffect } from "../lib/effect"
|
|
import { organizationPolicy } from "../fixture/policy"
|
|
import { PluginTestLayer } from "../plugin/fixture"
|
|
|
|
const it = testEffect(PluginTestLayer)
|
|
const decode = Schema.decodeUnknownSync(Info)
|
|
|
|
const document = (...policies: ConfigPolicy.Info[]) => new Document({ type: "document", info: decode({ policies }) })
|
|
const provider = (effect: ConfigPolicy.Effect, resource: string): ConfigPolicy.Info => ({
|
|
action: "provider.use",
|
|
resource,
|
|
effect,
|
|
})
|
|
const tool = (effect: ConfigPolicy.Effect, resource: string): ConfigPolicy.Info => ({
|
|
action: "tool.use",
|
|
resource,
|
|
effect,
|
|
})
|
|
|
|
const addPlugin = Effect.fn(function* (entries: Entry[]) {
|
|
const plugin = yield* Plugin.Service
|
|
const host = yield* PluginHost.make(plugin)
|
|
yield* ConfigPolicyPlugin.Plugin.effect(host).pipe(Effect.provide(Config.testLayer(entries)))
|
|
})
|
|
|
|
const evaluate = Effect.fn(function* (action: string, resources: string[], effect: Permission.Effect = "allow") {
|
|
const hooks = yield* PluginHooks.Service
|
|
const event = yield* hooks.trigger("permission", "evaluate", {
|
|
sessionID: Session.ID.make("ses_policy"),
|
|
action,
|
|
resources,
|
|
effect,
|
|
})
|
|
return { effect: event.effect, message: event.message }
|
|
})
|
|
|
|
describe("ConfigPolicyPlugin.Plugin", () => {
|
|
it.effect("preserves last-match precedence for tool.use without granting approvals", () =>
|
|
Effect.gen(function* () {
|
|
yield* addPlugin([
|
|
document(tool("deny", "shell:*"), tool("allow", "shell:git *"), tool("deny", "shell:git push *")),
|
|
])
|
|
expect((yield* evaluate("shell", ["git status"], "ask")).effect).toBe("ask")
|
|
expect((yield* evaluate("shell", ["git push origin main"])).effect).toBe("deny")
|
|
expect((yield* evaluate("shell", ["ls"])).effect).toBe("deny")
|
|
expect((yield* evaluate("edit", ["notes.md"])).effect).toBe("allow")
|
|
}),
|
|
)
|
|
|
|
it.effect("organization tool.use policy overrides authored policy", () =>
|
|
Effect.gen(function* () {
|
|
yield* addPlugin([organizationPolicy([tool("deny", "shell:*")], "Acme"), document(tool("allow", "shell:*"))])
|
|
expect(yield* evaluate("shell", ["ls"])).toEqual({ effect: "deny", message: "Blocked by Acme's policy" })
|
|
}),
|
|
)
|
|
|
|
it.effect("filters plugin-provided providers with ordered wildcard policies", () =>
|
|
Effect.gen(function* () {
|
|
const catalog = yield* Provider.Service
|
|
yield* catalog.transform((catalog) => {
|
|
catalog.update(Provider.ID.openai, () => {})
|
|
catalog.update(Provider.ID.anthropic, () => {})
|
|
catalog.update(Provider.ID.make("company-internal"), () => {})
|
|
})
|
|
yield* addPlugin([
|
|
document(provider("deny", "*"), provider("allow", "anthropic"), provider("allow", "company-*")),
|
|
])
|
|
|
|
expect(yield* catalog.get(Provider.ID.openai)).toBeUndefined()
|
|
expect(yield* catalog.get(Provider.ID.anthropic)).toBeDefined()
|
|
expect(yield* catalog.get(Provider.ID.make("company-internal"))).toBeDefined()
|
|
}),
|
|
)
|
|
|
|
it.effect("prevents project policy from overriding user-global policy", () =>
|
|
Effect.gen(function* () {
|
|
const catalog = yield* Provider.Service
|
|
yield* catalog.transform((catalog) => catalog.update(Provider.ID.openai, () => {}))
|
|
yield* addPlugin([document(provider("deny", "openai")), document(provider("allow", "openai"))])
|
|
|
|
expect(yield* catalog.get(Provider.ID.openai)).toBeUndefined()
|
|
}),
|
|
)
|
|
|
|
it.live("reloads changed policies", () =>
|
|
Effect.gen(function* () {
|
|
const catalog = yield* Provider.Service
|
|
const bus = yield* Bus.Service
|
|
const test = yield* Config.Test
|
|
const plugin = yield* Plugin.Service
|
|
const host = yield* PluginHost.make(plugin)
|
|
yield* catalog.transform((catalog) => catalog.update(Provider.ID.openai, () => {}))
|
|
yield* ConfigPolicyPlugin.Plugin.effect(host)
|
|
expect(yield* catalog.get(Provider.ID.openai)).toBeUndefined()
|
|
|
|
yield* test.setEntries([document(provider("allow", "openai"))])
|
|
yield* bus.publish(Event.Updated, {})
|
|
yield* waitUntil(catalog.get(Provider.ID.openai).pipe(Effect.map((provider) => provider !== undefined)))
|
|
}).pipe(Effect.provide(Config.testLayer([document(provider("deny", "openai"))]))),
|
|
)
|
|
|
|
it.effect("denies permissions matched as action:resource", () =>
|
|
Effect.gen(function* () {
|
|
yield* addPlugin([document(tool("deny", "shell:git push *"))])
|
|
|
|
expect(yield* evaluate("shell", ["git push"])).toEqual({
|
|
effect: "deny",
|
|
message: "Blocked by configuration policy",
|
|
})
|
|
expect(yield* evaluate("shell", ["git push origin main"])).toEqual({
|
|
effect: "deny",
|
|
message: "Blocked by configuration policy",
|
|
})
|
|
// Compound commands check several resources; any denied resource denies the operation.
|
|
expect((yield* evaluate("shell", ["git status", "git push"])).effect).toBe("deny")
|
|
expect(yield* evaluate("shell", ["git status"])).toEqual({ effect: "allow", message: undefined })
|
|
expect(yield* evaluate("edit", ["git push"])).toEqual({ effect: "allow", message: undefined })
|
|
}),
|
|
)
|
|
|
|
it.effect("turns an ask into a deny but never grants", () =>
|
|
Effect.gen(function* () {
|
|
yield* addPlugin([document(tool("deny", "webfetch:*"), tool("allow", "shell:*"))])
|
|
|
|
expect((yield* evaluate("webfetch", ["https://example.com"], "ask")).effect).toBe("deny")
|
|
expect((yield* evaluate("shell", ["ls"], "ask")).effect).toBe("ask")
|
|
}),
|
|
)
|
|
|
|
it.effect("lets a later allow lift an earlier broad deny", () =>
|
|
Effect.gen(function* () {
|
|
yield* addPlugin([document(tool("deny", "shell:*"), tool("allow", "shell:git status *"))])
|
|
|
|
expect((yield* evaluate("shell", ["git status --short"])).effect).toBe("allow")
|
|
expect((yield* evaluate("shell", ["rm -rf /"])).effect).toBe("deny")
|
|
}),
|
|
)
|
|
|
|
it.effect("denies every permission with a bare wildcard", () =>
|
|
Effect.gen(function* () {
|
|
yield* addPlugin([document(tool("deny", "*"))])
|
|
|
|
expect((yield* evaluate("question", ["*"])).effect).toBe("deny")
|
|
expect((yield* evaluate("read", ["/tmp/notes.txt"])).effect).toBe("deny")
|
|
expect((yield* evaluate("github_delete_repository", ["*"])).effect).toBe("deny")
|
|
}),
|
|
)
|
|
|
|
it.effect("lets organization provider statements override authored documents", () =>
|
|
Effect.gen(function* () {
|
|
const catalog = yield* Provider.Service
|
|
yield* catalog.transform((catalog) => {
|
|
catalog.update(Provider.ID.openai, () => {})
|
|
catalog.update(Provider.ID.anthropic, () => {})
|
|
catalog.update(Provider.ID.opencode, () => {})
|
|
})
|
|
yield* addPlugin([
|
|
organizationPolicy([provider("deny", "*"), provider("allow", "opencode")]),
|
|
document(provider("allow", "anthropic")),
|
|
])
|
|
|
|
expect(yield* catalog.get(Provider.ID.anthropic)).toBeUndefined()
|
|
expect(yield* catalog.get(Provider.ID.openai)).toBeUndefined()
|
|
expect(yield* catalog.get(Provider.ID.opencode)).toBeDefined()
|
|
}),
|
|
)
|
|
|
|
it.effect("lets an organization allow restore a provider denied by the user", () =>
|
|
Effect.gen(function* () {
|
|
const catalog = yield* Provider.Service
|
|
yield* catalog.transform((catalog) => catalog.update(Provider.ID.openai, () => {}))
|
|
yield* addPlugin([organizationPolicy([provider("allow", "openai")]), document(provider("deny", "openai"))])
|
|
|
|
expect(yield* catalog.get(Provider.ID.openai)).toBeDefined()
|
|
}),
|
|
)
|
|
|
|
for (const input of [
|
|
{ organization: "Acme", message: "Blocked by Acme's policy" },
|
|
{ organization: undefined, message: "Blocked by your organization's policy" },
|
|
])
|
|
it.effect(`names the organization when its permission statement decides: ${input.organization}`, () =>
|
|
Effect.gen(function* () {
|
|
yield* addPlugin([
|
|
organizationPolicy([tool("deny", "shell:sudo *")], input.organization),
|
|
document(tool("allow", "shell:*")),
|
|
])
|
|
|
|
expect(yield* evaluate("shell", ["sudo ls"])).toEqual({ effect: "deny", message: input.message })
|
|
expect((yield* evaluate("shell", ["ls"])).effect).toBe("allow")
|
|
}),
|
|
)
|
|
|
|
it.live("rebuilds the catalog when the organization's policy changes", () =>
|
|
Effect.gen(function* () {
|
|
const catalog = yield* Provider.Service
|
|
const bus = yield* Bus.Service
|
|
const test = yield* Config.Test
|
|
const host = yield* PluginHost.make(yield* Plugin.Service)
|
|
const opencode = Integration.ID.make("opencode")
|
|
yield* catalog.transform((catalog) => catalog.update(Provider.ID.openai, () => {}))
|
|
yield* ConfigPolicyPlugin.Plugin.effect(host)
|
|
expect(yield* catalog.get(Provider.ID.openai)).toBeDefined()
|
|
|
|
yield* test.integration?.(opencode, organizationPolicy([provider("deny", "openai")])) ?? Effect.void
|
|
yield* bus.publish(Event.Updated, {})
|
|
yield* waitUntil(catalog.get(Provider.ID.openai).pipe(Effect.map((provider) => provider === undefined)))
|
|
|
|
yield* test.integration?.(opencode, undefined) ?? Effect.void
|
|
yield* bus.publish(Event.Updated, {})
|
|
yield* waitUntil(catalog.get(Provider.ID.openai).pipe(Effect.map((provider) => provider !== undefined)))
|
|
}).pipe(Effect.provide(Config.testLayer())),
|
|
)
|
|
})
|
|
|
|
const waitUntil = Effect.fnUntraced(function* (condition: Effect.Effect<boolean>) {
|
|
for (let attempt = 0; attempt < 200; attempt++) {
|
|
if (yield* condition) return
|
|
yield* Effect.sleep("10 millis")
|
|
}
|
|
return yield* Effect.die("Timed out waiting for policy reload")
|
|
})
|