Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(bg): route /bg shell commands through the approval boundary

SeashoreShiefb8a451f403
diff --git a/src/core/agent.ts b/src/core/agent.ts
index 9c490b5e..d4388a50 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -2176,6 +2176,17 @@ export class Agent {
return;
}
+ // `/bg <command>` is an alternate shell entry point: it must honor the
+ // same approval boundary as the run_command tool, otherwise an
+ // authenticated chat user can bypass Ask-Me approval and run arbitrary
+ // commands. checkShellCommand() also enforces the blocked list and any
+ // cwd/scope containment rules.
+ const check = await this.capabilities.permissions.checkShellCommand(command);
+ if (!check.allowed) {
+ await channel.send(`⛔ Not running in background: ${check.reason ?? 'command requires approval'}\nApprove the command first (or run it without /bg) and try again.`, msg.channelId);
+ return;
+ }
+
const cwd = this.capabilities.getCwd();
const bgId = this.backgroundTasks.spawnShell(command, cwd);
await channel.send(`📋 Background task ${bgId} started: "${command.slice(0, 50)}${command.length > 50 ? '...' : ''}"`, msg.channelId);
diff --git a/src/core/bg-shell-approval.test.ts b/src/core/bg-shell-approval.test.ts
new file mode 100644
index 00000000..0d77e41c
--- /dev/null
+++ b/src/core/bg-shell-approval.test.ts
@@ -0,0 +1,25 @@
+import { describe, expect, it } from 'vitest';
+import { readFileSync } from 'node:fs';
+import { fileURLToPath } from 'node:url';
+import { dirname, join } from 'node:path';
+
+const agentSrc = readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'agent.ts'), 'utf8');
+
+/**
+ * `/bg <command>` is an alternate shell entry point. It used to call
+ * `backgroundTasks.spawnShell()` directly, so an authenticated chat user could
+ * bypass Ask-Me approval and run arbitrary OS commands (reported in #73). The
+ * contract: the background shell path must run the command through
+ * `PermissionManager.checkShellCommand()` before spawning, exactly like the
+ * `run_command` tool, and must not spawn when the check denies it.
+ */
+describe('/bg <command> honors the shell approval boundary', () => {
+ it('checks the command before spawning it in the background', () => {
+ // The shell branch must call checkShellCommand(command) and bail out when
+ // it is not allowed, before reaching spawnShell(command, cwd).
+ const shellBranch = agentSrc.match(/const command = args \|\| ''[\s\S]*?spawnShell\(command, cwd\)/s)?.[0] ?? '';
+ expect(shellBranch).toContain('checkShellCommand(command)');
+ expect(shellBranch).toMatch(/if \(!check\.allowed\)/);
+ expect(shellBranch.indexOf('checkShellCommand(command)')).toBeLessThan(shellBranch.indexOf('spawnShell(command, cwd)'));
+ });
+});