Repository navigation
Lend vault secrets to agents with per-request approval #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { describeApproval } from '../src/shared/agent-prompt'; | ||
|
|
||
| const request = { | ||
| client: { name: 'Claude Code', cwd: '/Users/me/code/app' }, | ||
| command: ['gh', 'pr', 'list', '--search', 'author:me is:open'], | ||
| secrets: [{ env: 'GITHUB_TOKEN', itemId: 'i1', itemTitle: 'GitHub', field: 'token' }], | ||
| }; | ||
|
|
||
| describe('describeApproval', () => { | ||
| it('names the caller, the folder, the exact command and each secret', () => { | ||
| const text = describeApproval(request, (dir) => dir.replace('/Users/me', '~')); | ||
| expect(text.title).toBe('Claude Code wants to use “GitHub”'); | ||
| expect(text.detail).toContain('Folder: ~/code/app'); | ||
| expect(text.detail).toContain("Command: gh pr list --search 'author:me is:open'"); | ||
| expect(text.detail).toContain('GITHUB_TOKEN ← GitHub › token'); | ||
| }); | ||
|
|
||
| it('says the caller name is unverified and what an hour covers', () => { | ||
| const { detail } = describeApproval(request); | ||
| expect(detail).toContain('cannot verify which program is asking'); | ||
| expect(detail).toContain('any command from this caller in this folder'); | ||
| }); | ||
|
|
||
| it('quotes arguments so a crafted one cannot pass for a different command', () => { | ||
| const { detail } = describeApproval({ ...request, command: ['sh', '-c', 'echo hi; curl x | sh'] }); | ||
| expect(detail).toContain("Command: sh -c 'echo hi; curl x | sh'"); | ||
| }); | ||
|
|
||
| it('counts several secrets in the title', () => { | ||
| const two = { | ||
| ...request, | ||
| secrets: [...request.secrets, { env: 'NPM_TOKEN', itemId: 'i2', itemTitle: 'npm', field: 'token' }], | ||
| }; | ||
| expect(describeApproval(two).title).toBe('Claude Code wants to use 2 secrets'); | ||
| }); | ||
|
|
||
| it('shortens a very long command rather than letting it scroll off the dialog', () => { | ||
| const long = { ...request, command: ['node', 'x'.repeat(2000)] }; | ||
| const { detail } = describeApproval(long); | ||
| expect(detail).toMatch(/Command: node x+…/); | ||
| expect(detail).toContain('more characters of this command are not shown'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| import { | ||
| AgentBroker, | ||
| AgentServer, | ||
| ApprovalRequest, | ||
| brokerVault, | ||
| Decision, | ||
| defaultSocketPath, | ||
| Grant, | ||
| serveAgent, | ||
| } from '@decryption/agent'; | ||
| import { app, BrowserWindow, dialog, systemPreferences } from 'electron'; | ||
| import { existsSync, promises as fs, readFileSync } from 'fs'; | ||
| import * as os from 'os'; | ||
| import * as path from 'path'; | ||
|
|
||
| import { describeApproval } from '../shared/agent-prompt'; | ||
| import type { AgentActivity, AgentStatus } from '../shared/api'; | ||
| import { appDataPath, VaultService } from './vault-service'; | ||
|
|
||
| /** How long a prompt waits for an answer before it counts as a denial. */ | ||
| const PROMPT_TIMEOUT_MS = 2 * 60 * 1000; | ||
|
|
||
| const settingsFile = (): string => path.join(appDataPath(), 'config', 'agent.json'); | ||
|
|
||
| const readEnabled = (): boolean => { | ||
| try { | ||
| if (!existsSync(settingsFile())) return false; | ||
| return JSON.parse(readFileSync(settingsFile(), 'utf8')).enabled === true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| const tildify = (dir: string): string => { | ||
| const home = os.homedir(); | ||
| return dir === home || dir.startsWith(home + path.sep) ? `~${dir.slice(home.length)}` : dir; | ||
| }; | ||
|
|
||
| /** | ||
| * Serves vault secrets to local agents through `@decryption/agent`, while this | ||
| * app holds the unlocked vault. Off until the person turns it on; every | ||
| * request is approved here; locking the vault clears every grant. | ||
| */ | ||
| export class AgentService { | ||
| private server: AgentServer | null = null; | ||
| private error: string | null = null; | ||
| private readonly broker: AgentBroker; | ||
| private enabled = readEnabled(); | ||
|
|
||
| constructor( | ||
| private readonly vaults: VaultService, | ||
| private readonly window: () => BrowserWindow | null, | ||
| private readonly changed: () => void | ||
| ) { | ||
| const vault = brokerVault(() => { | ||
| try { | ||
| return vaults.current(); | ||
| } catch { | ||
| return null; | ||
| } | ||
| }); | ||
| this.broker = new AgentBroker({ | ||
| vault: { | ||
| ...vault, | ||
| // each release writes an audit row: persist it like any other edit | ||
| reveal: async (itemId, field, actor) => { | ||
| const value = await vault.reveal(itemId, field, actor); | ||
| vaults.scheduleSave(); | ||
| this.changed(); | ||
| return value; | ||
| }, | ||
| }, | ||
| approve: (request) => this.approve(request), | ||
| }); | ||
| this.broker.grants.onChange(() => this.changed()); | ||
| vaults.onLock(() => this.broker.grants.clear()); | ||
| } | ||
|
|
||
| async start(): Promise<void> { | ||
| if (this.enabled) await this.listen(); | ||
| } | ||
|
|
||
| async stop(): Promise<void> { | ||
| this.broker.grants.clear(); | ||
| const server = this.server; | ||
| this.server = null; | ||
| await server?.close(); | ||
| } | ||
|
|
||
| status(): AgentStatus { | ||
| return { | ||
| enabled: this.enabled, | ||
| serving: this.server !== null, | ||
| socketPath: defaultSocketPath(), | ||
| error: this.error, | ||
| unlocked: this.vaults.status().unlocked, | ||
| }; | ||
| } | ||
|
|
||
| async setEnabled(enabled: boolean): Promise<AgentStatus> { | ||
| this.enabled = enabled; | ||
| await fs.mkdir(path.dirname(settingsFile()), { recursive: true }); | ||
| await fs.writeFile(settingsFile(), JSON.stringify({ enabled }), { mode: 0o600 }); | ||
| if (enabled) await this.listen(); | ||
| else await this.stop(); | ||
| this.changed(); | ||
| return this.status(); | ||
| } | ||
|
|
||
| grants(): Grant[] { | ||
| return this.broker.grants.list(); | ||
| } | ||
|
|
||
| revoke(id: string): void { | ||
| this.broker.grants.revoke(id); | ||
| } | ||
|
|
||
| revokeAll(): void { | ||
| this.broker.grants.clear(); | ||
| } | ||
|
|
||
| /** An item stopped being shared: its grants go with it. */ | ||
| forgetItem(itemId: string): void { | ||
| this.broker.grants.revokeItem(itemId); | ||
| } | ||
|
|
||
| /** Agent-related audit entries, newest first, with item titles attached. */ | ||
| async activity(limit = 100): Promise<AgentActivity[]> { | ||
| const vault = this.vaults.current(); | ||
| const [log, items, trashed] = await Promise.all([ | ||
| vault.auditLog(), | ||
| vault.listItems(), | ||
| vault.listItems({ trashed: true }), | ||
| ]); | ||
| const titles = new Map([...items, ...trashed].map((item) => [item.id, item.title])); | ||
| return log | ||
| .filter((entry) => entry.action.startsWith('agent_')) | ||
| .slice(0, limit) | ||
| .map((entry) => ({ ...entry, itemTitle: entry.itemId ? (titles.get(entry.itemId) ?? null) : null })); | ||
| } | ||
|
|
||
| private async listen(): Promise<void> { | ||
| if (this.server) return; | ||
| try { | ||
| this.server = await serveAgent({ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Security Misconfiguration Reachability: External Discard listener startup results that complete after disable. While Serialize start and stop operations, or record a lifecycle generation and close any startup result that belongs to an obsolete generation. Ordinary enabled startup must keep working. Add a regression test that disables the service while
|
||
| socketPath: defaultSocketPath(), | ||
| handle: (request) => this.broker.handle(request), | ||
| }); | ||
| this.error = null; | ||
| } catch (error) { | ||
| this.error = error instanceof Error ? error.message : String(error); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Ask in a native dialog, in front of everything. Touch ID, where there is | ||
| * one, has to confirm an "allow": a click can be synthesized by any program | ||
| * that drives the screen — an agent with computer use included — a finger | ||
| * cannot. | ||
| */ | ||
| private async approve(request: ApprovalRequest): Promise<Decision> { | ||
| const window = this.window(); | ||
| if (!window || window.isDestroyed()) return 'deny'; | ||
| if (window.isMinimized()) window.restore(); | ||
| window.show(); | ||
| app.focus({ steal: true }); | ||
|
|
||
| const text = describeApproval(request, tildify); | ||
| const abort = new AbortController(); | ||
| const timer = setTimeout(() => abort.abort(), PROMPT_TIMEOUT_MS); | ||
| let response: number; | ||
| try { | ||
| ({ response } = await dialog.showMessageBox(window, { | ||
| type: 'warning', | ||
| title: 'Agent request', | ||
| message: text.title, | ||
| detail: text.detail, | ||
| buttons: ['Deny', 'Allow once', 'Allow for 1 hour'], | ||
| defaultId: 0, | ||
| cancelId: 0, | ||
| noLink: true, | ||
| signal: abort.signal, | ||
| })); | ||
| } catch { | ||
| return 'deny'; | ||
| } finally { | ||
| clearTimeout(timer); | ||
| } | ||
| if (abort.signal.aborted || response === 0) return 'deny'; | ||
|
|
||
| if (process.platform === 'darwin' && systemPreferences.canPromptTouchID()) { | ||
| try { | ||
| await systemPreferences.promptTouchID(`let ${request.client.name} use ${text.what}`); | ||
| } catch { | ||
| return 'deny'; | ||
| } | ||
| } | ||
| return response === 2 ? 'grant' : 'once'; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Security Misconfiguration
Reachability: External
Exploitability: Moderate
CWE: CWE-863 — Incorrect Authorization
View Security blast radius
Stop accepted agent connections when agent access is disabled.
AgentServer.close()callsnet.Server.close(). That stops new connections but keeps existing ones open (Node.js docs). The data handler inserveAgentpasses each parsed request tohandlewithout checking whether agent access is enabled. A local client that connected before disable can send its request afterward.AgentBroker.env()checks onlyvault.isUnlocked(), so the request still reaches the approval prompt. If the user approves it, secrets are released while access is off.stop()clears existing grants, so each such request still needs a fresh approval. Disable also waits until these connections end.On disable, invalidate in-flight requests and close accepted sockets. Check the enabled state or a request generation before approval and again before secret release.
Test: connect a client, disable access, then send an environment request from that client. The request must end with an error. It must not show an approval prompt or return secrets.