/** * The wire format between `dcrypt agent` and whatever holds the unlocked vault * (the desktop app). One request per connection, one newline-terminated JSON * object each way. Every field is validated on the serving side: the socket is * reachable by any process running as this user, so nothing it sends is trusted. */ export const PROTOCOL_VERSION = 1; /** Largest request a server will read, in bytes. */ export const MAX_REQUEST_BYTES = 64 * 1024; /** How the caller describes itself. Self-reported: a label, never proof. */ export interface ClientInfo { /** e.g. "Claude Code", "Codex", "a terminal". */ name: string; /** Working directory the request was made from. */ cwd: string; pid?: number; } /** One secret to put into the child's environment. */ export interface SecretRequest { /** Environment variable name. */ env: string; /** Item id or title. */ item: string; /** Field name; omitted means "the item's one secret field". */ field?: string; } export type AgentRequest = | { v: typeof PROTOCOL_VERSION; op: 'status' } | { v: typeof PROTOCOL_VERSION; op: 'env'; client: ClientInfo; /** The command the values are for, shown to the person approving. */ command: string[]; secrets: SecretRequest[]; }; export type AgentErrorCode = | 'bad_request' | 'locked' | 'denied' | 'not_found' | 'ambiguous' | 'busy' | 'internal'; export type AgentResponse = | { ok: true; op: 'status'; unlocked: boolean } | { ok: true; op: 'env'; env: Record } | { ok: false; code: AgentErrorCode; message: string }; export class AgentProtocolError extends Error { constructor(message: string) { super(message); this.name = 'AgentProtocolError'; } } const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,127}$/; const MAX_SECRETS = 32; const MAX_COMMAND_PARTS = 256; const MAX_STRING = 4096; const isRecord = (value: unknown): value is Record => typeof value === 'object' && value !== null && !Array.isArray(value); const boundedString = (value: unknown, what: string, allowEmpty = false): string => { if (typeof value !== 'string') throw new AgentProtocolError(`${what} must be a string`); if (!allowEmpty && value.length === 0) throw new AgentProtocolError(`${what} is empty`); if (value.length > MAX_STRING) throw new AgentProtocolError(`${what} is too long`); // control characters would let a caller redraw the approval prompt // eslint-disable-next-line no-control-regex if (/[\u0000-\u0008\u000b-\u001f\u007f]/.test(value)) { throw new AgentProtocolError(`${what} contains control characters`); } return value; }; /** Parse and validate one request; throws AgentProtocolError on anything off. */ export const parseRequest = (raw: string): AgentRequest => { let data: unknown; try { data = JSON.parse(raw); } catch { throw new AgentProtocolError('request is not JSON'); } if (!isRecord(data)) throw new AgentProtocolError('request must be an object'); if (data.v !== PROTOCOL_VERSION) { throw new AgentProtocolError(`unsupported protocol version ${String(data.v)}`); } if (data.op === 'status') return { v: PROTOCOL_VERSION, op: 'status' }; if (data.op !== 'env') throw new AgentProtocolError(`unknown op ${String(data.op)}`); if (!isRecord(data.client)) throw new AgentProtocolError('client is required'); const client: ClientInfo = { name: boundedString(data.client.name, 'client.name').slice(0, 80), cwd: boundedString(data.client.cwd, 'client.cwd'), }; if (data.client.pid !== undefined) { if (!Number.isInteger(data.client.pid)) throw new AgentProtocolError('client.pid must be an integer'); client.pid = data.client.pid as number; } if (!Array.isArray(data.command) || data.command.length === 0) { throw new AgentProtocolError('command is required'); } if (data.command.length > MAX_COMMAND_PARTS) throw new AgentProtocolError('command is too long'); const command = data.command.map((part, index) => boundedString(part, `command[${index}]`, index > 0) ); if (!Array.isArray(data.secrets) || data.secrets.length === 0) { throw new AgentProtocolError('at least one secret is required'); } if (data.secrets.length > MAX_SECRETS) throw new AgentProtocolError('too many secrets'); const seen = new Set(); const secrets = data.secrets.map((entry, index): SecretRequest => { if (!isRecord(entry)) throw new AgentProtocolError(`secrets[${index}] must be an object`); const env = boundedString(entry.env, `secrets[${index}].env`); if (!ENV_NAME.test(env)) throw new AgentProtocolError(`"${env}" is not an environment variable name`); if (seen.has(env)) throw new AgentProtocolError(`${env} is requested twice`); seen.add(env); const secret: SecretRequest = { env, item: boundedString(entry.item, `secrets[${index}].item`) }; if (entry.field !== undefined) secret.field = boundedString(entry.field, `secrets[${index}].field`); return secret; }); return { v: PROTOCOL_VERSION, op: 'env', client, command, secrets }; }; /** * Parse `ENV=item` or `ENV=item#field`, the command-line form. The field is * split at the last `#`, so a title containing `#` needs its field named too — * or the item's id instead of its title. */ export const parseSecretSpec = (spec: string): SecretRequest => { const eq = spec.indexOf('='); if (eq <= 0) throw new AgentProtocolError(`"${spec}" should look like ENV=item or ENV=item#field`); const env = spec.slice(0, eq); const ref = spec.slice(eq + 1); const hash = ref.lastIndexOf('#'); const item = hash > 0 ? ref.slice(0, hash) : ref; const field = hash > 0 ? ref.slice(hash + 1) : undefined; if (!item) throw new AgentProtocolError(`"${spec}" names no item`); if (field === '') throw new AgentProtocolError(`"${spec}" has an empty field after #`); return field ? { env, item, field } : { env, item }; };