fix(core): hold session-less schedule shell runs to global permissions

This commit is contained in:
Kit Langton 2026-11-04 23:39:48 -08:00
parent 61a70d34eb
commit e8ce682fd3
3 changed files with 95 additions and 5 deletions

View file

@ -43,7 +43,7 @@ export const authorize = Effect.fnUntraced(function* (
/**
* Runs one shell action under narrow unattended permissions. Never fails: every outcome becomes a run result. Without a
* Session it runs in the global config Location and asks for nothing, because the caller that created it is trusted.
* Session it runs in the global config Location, held to its default agent's rules with nothing asked.
* A check's `message` arrives on stdin with a trailing newline and in `OPENCODE_SCHEDULE_MESSAGE`.
*/
export const run = Effect.fnUntraced(function* (

View file

@ -367,14 +367,15 @@ export const scanShell = Effect.fnUntraced(function* (command: string, cwd?: str
/**
* Asks once for the shell and `external_directory` permissions a shell check or action needs. Without a Session nothing
* is asked: a session-less schedule was created by a trusted API caller. This is the only place that trust rule lives.
* is asked, because there is no sessionID to attach a request to: the global default agent's rules decide, and anything
* they do not allow is denied.
*/
export const assertShellPermissions = Effect.fnUntraced(function* (
session: SessionSchema.Info | undefined,
scanned: { readonly external: ReadonlyArray<string>; readonly commands: ReadonlyArray<string> },
metadata?: Record<string, unknown>,
) {
if (session === undefined) return
if (session === undefined) return yield* assertGlobalShellPermissions(scanned)
const permission = yield* Permission.Service
const agents = yield* Agent.Service
const agent = yield* agents.select(session.agent)
@ -398,6 +399,35 @@ export const assertShellPermissions = Effect.fnUntraced(function* (
}
})
/** Rules for a location with no global default agent: everything is denied. */
const denyAll: Permission.Ruleset = [{ action: "*", resource: "*", effect: "deny" }]
/**
* Holds a session-less run to the global config Location's rules, from its default agent. A run without a Session runs
* inside that Location, so `agents.select(undefined)` reads the agent the config there would pick. The pure
* `Permission.evaluate` is used instead of `Permission.assert`, because nobody is present and a request that would ask
* has no sessionID to belong to: anything other than an allow is denied.
*/
const assertGlobalShellPermissions = Effect.fnUntraced(function* (scanned: {
readonly external: ReadonlyArray<string>
readonly commands: ReadonlyArray<string>
}) {
const agents = yield* Agent.Service
const agent = yield* agents.select(undefined)
const rules = agent.info?.permissions ?? denyAll
for (const action of ["external_directory", "shell"] as const) {
const resources = action === "external_directory" ? scanned.external : scanned.commands
const blocked = resources.filter((resource) => Permission.evaluate(action, resource, rules).effect !== "allow")
if (blocked.length === 0) continue
return yield* new Permission.BlockedError({
rules,
permission: action,
resources: blocked,
reason: `Permission ${action} (${blocked.join(", ")}) is not allowed by the global config, and nobody is present to ask`,
})
}
})
const authorizeShell = Effect.fnUntraced(function* (
session: SessionSchema.Info,
name: string | undefined,

View file

@ -741,12 +741,49 @@ describe("Schedule", () => {
}),
)
it.effect("runs a session-less shell action without asking for permissions", () =>
it.effect("denies a session-less shell action its global config does not allow", () =>
Effect.gen(function* () {
const schedules = yield* setup()
globalFlag.on = true
agentRules = [{ action: "*", resource: "*", effect: "deny" }]
// Boots the global location with the deny rules, so an asserted permission would stop the run.
// Boots the global location with the deny rules, so the run is held to them.
expect(yield* schedules.enabled()).toBe(true)
const bus = yield* Bus.Service
const asks: Permission.Request[] = []
const unsubscribe = yield* bus.listen((event) =>
event.type === Permission.Event.Asked.type
? Effect.sync(() => {
asks.push(event.data as Permission.Request)
})
: Effect.void,
)
yield* Effect.addFinalizer(() => unsubscribe)
const schedule = yield* schedules.create({
when: { type: "in", in: "1 minute" },
action: { type: "shell", run: "echo hi" },
})
yield* TestClock.adjust("1 minute")
yield* schedules.tick
expect(shellState.runs).toEqual([])
expect((yield* schedules.get(schedule.id)).lastRun?.error).toContain(
"Permission shell (echo hi) is not allowed by the global config",
)
// A session-less run has no sessionID to attach a request to, so a rule that would ask is a denial.
expect(asks).toEqual([])
}),
)
it.effect("runs a session-less shell action its global config allows", () =>
Effect.gen(function* () {
const schedules = yield* setup()
globalFlag.on = true
shellState.stdout = "hi"
agentRules = [
{ action: "external_directory", resource: "*", effect: "allow" },
{ action: "shell", resource: "*", effect: "allow" },
]
expect(yield* schedules.enabled()).toBe(true)
const schedule = yield* schedules.create({
@ -760,6 +797,29 @@ describe("Schedule", () => {
}),
)
it.effect("holds a session-less shell check to its global config", () =>
Effect.gen(function* () {
const schedules = yield* setup()
globalFlag.on = true
agentRules = [{ action: "*", resource: "*", effect: "deny" }]
expect(yield* schedules.enabled()).toBe(true)
const schedule = yield* schedules.create({
when: { type: "every", every: "1 minute" },
if: { type: "shell", run: "probe" },
action: { type: "shell", run: "echo hi" },
})
for (let index = 0; index < 3; index++) {
yield* TestClock.adjust("1 minute")
yield* schedules.tick
}
expect(shellState.runs).toEqual([])
const info = yield* schedules.get(schedule.id)
expect(info).toMatchObject({ status: "paused", lastOutcome: "failed" })
expect(info.lastRun?.error).toContain("Permission shell (probe) is not allowed by the global config")
}),
)
it.effect("gates a session-less shell action on a shell if check", () =>
Effect.gen(function* () {
const schedules = yield* setup()