From e8ce682fd38d33a42339f05099a9e518c135d4aa Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Wed, 4 Nov 2026 23:39:48 -0800 Subject: [PATCH] fix(core): hold session-less schedule shell runs to global permissions --- packages/core/src/schedule/action.ts | 2 +- packages/core/src/schedule/check.ts | 34 ++++++++++++++- packages/core/test/schedule.test.ts | 64 +++++++++++++++++++++++++++- 3 files changed, 95 insertions(+), 5 deletions(-) diff --git a/packages/core/src/schedule/action.ts b/packages/core/src/schedule/action.ts index 0f5447e2ff9..3a9624ee1c5 100644 --- a/packages/core/src/schedule/action.ts +++ b/packages/core/src/schedule/action.ts @@ -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* ( diff --git a/packages/core/src/schedule/check.ts b/packages/core/src/schedule/check.ts index 619f8ce2667..50fd80e7b7d 100644 --- a/packages/core/src/schedule/check.ts +++ b/packages/core/src/schedule/check.ts @@ -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; readonly commands: ReadonlyArray }, metadata?: Record, ) { - 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 + readonly commands: ReadonlyArray +}) { + 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, diff --git a/packages/core/test/schedule.test.ts b/packages/core/test/schedule.test.ts index ba853776dbe..f565992ba02 100644 --- a/packages/core/test/schedule.test.ts +++ b/packages/core/test/schedule.test.ts @@ -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()