Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Agents controller: store the JSON sealing key in the macOS keychain

Petr Bauch0ac56b027021
diff --git a/console/packages/agent-controller/src/secrets.test.ts b/console/packages/agent-controller/src/secrets.test.ts
index 3e95b5187..4a35700de 100644
--- a/console/packages/agent-controller/src/secrets.test.ts
+++ b/console/packages/agent-controller/src/secrets.test.ts
@@ -10,6 +10,7 @@ import {
FileSecretStore,
KeychainSecretStore,
MemorySecretStore,
+ SEALING_KEY,
SecretServiceStore,
} from './secrets';
@@ -111,6 +112,33 @@ describe('OS keychain stores', () => {
expect(await store.get(CONTROLLER_CREDENTIAL)).toBeNull();
});
+ it('stores a value security -i cannot quote, such as the JSON sealing key, and reads it back', async () => {
+ let stored: string | null = null;
+ const { calls, run } = runner((call) => {
+ if (call.args[0] === 'find-generic-password')
+ return stored === null
+ ? new CommandFailure(call.file, call.args, 44, 'not found', 'exit 44')
+ : { stdout: `${stored}\n` };
+ stored = /-w "([^"]*)"/.exec(call.input ?? '')?.[1] ?? null;
+ return { stdout: '' };
+ });
+ const store = new KeychainSecretStore('/data', run);
+ const keys = JSON.stringify({ publicKey: 'pub', privateKey: 'pri\\vate' });
+ await store.set(SEALING_KEY, keys);
+ expect(calls[0].input).not.toContain('{');
+ expect(await store.get(SEALING_KEY)).toBe(keys);
+
+ await store.set(SEALING_KEY, 'b64:not-encoded-by-hand');
+ expect(await store.get(SEALING_KEY)).toBe('b64:not-encoded-by-hand');
+ });
+
+ it('reads an entry stored as plain text before values were encoded', async () => {
+ const { run } = runner(() => ({ stdout: 'swcc_secret-value\n' }));
+ expect(await new KeychainSecretStore('/data', run).get(CONTROLLER_CREDENTIAL)).toBe(
+ 'swcc_secret-value'
+ );
+ });
+
it('fails loud on a keychain error that is not "not found"', async () => {
const { run } = runner(
(call) => new CommandFailure(call.file, call.args, 51, 'user interaction is not allowed', 'x')
diff --git a/console/packages/agent-controller/src/secrets.ts b/console/packages/agent-controller/src/secrets.ts
index 49dbfdeff..6e072d28b 100644
--- a/console/packages/agent-controller/src/secrets.ts
+++ b/console/packages/agent-controller/src/secrets.ts
@@ -189,6 +189,24 @@ function account(dataDir: string, name: string): string {
return `${name}@${dataDir}`;
}
+const ENCODED = 'b64:';
+
+/**
+ * `security -i` reads its commands as quoted words, so a value holding a
+ * quote, backslash or newline (the sealing key pair is JSON) is stored
+ * base64-encoded behind a marker. Anything else is stored as it is, so
+ * entries written before keep reading back unchanged.
+ */
+function toKeychain(value: string): string {
+ if (!/["\\\n]/.test(value) && !value.startsWith(ENCODED)) return value;
+ return ENCODED + Buffer.from(value, 'utf8').toString('base64');
+}
+
+function fromKeychain(stored: string): string {
+ if (!stored.startsWith(ENCODED)) return stored;
+ return Buffer.from(stored.slice(ENCODED.length), 'base64').toString('utf8');
+}
+
/**
* The macOS login keychain, through `security`. The value is written through
* `security -i` on stdin, never on the command line, where other processes
@@ -214,7 +232,7 @@ export class KeychainSecretStore implements SecretStore {
['find-generic-password', '-s', SERVICE, '-a', account(this.dataDir, name), '-w'],
null
);
- return stdout.trim();
+ return fromKeychain(stdout.trim());
} catch (error) {
// 44: errSecItemNotFound.
if (error instanceof CommandFailure && error.code === 44) return null;
@@ -223,8 +241,7 @@ export class KeychainSecretStore implements SecretStore {
}
async set(name: string, value: string): Promise<void> {
- if (/["\\\n]/.test(value)) throw new Error('This secret cannot be stored in the keychain.');
- const command = `add-generic-password -U -s ${SERVICE} -a "${account(this.dataDir, name)}" -w "${value}"\n`;
+ const command = `add-generic-password -U -s ${SERVICE} -a "${account(this.dataDir, name)}" -w "${toKeychain(value)}"\n`;
await this.run('security', ['-i'], command);
}