Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(companions): redact nested command input credentials

Chris482b62ec130e
diff --git a/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts b/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts
index 7decde47a9..e0ad2439c8 100644
--- a/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts
+++ b/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts
@@ -243,3 +243,32 @@ it('masks argv-only credentials again before publishing legacy command results',
expect(published).not.toContain(root);
expect(published).toContain('[command_literal_');
});
+
+it.each(['stdin', 'argv', 'assignment'] as const)('masks nested JSON credentials from %s in execution, cached output and publication', async source => {
+ const routine = { id: 'routine', botId: 'bot', prompt: 'Fixture structured input' } as Routine;
+ const secret = 'fixture-nested-"quoted"\n-secret';
+ const input = JSON.stringify({ credentials: [{ token: secret }], city: 'Paris', count: 7 });
+ const read = source === 'stdin' ? 'require("node:fs").readFileSync(0,"utf8")'
+ : source === 'assignment' ? 'process.argv[1].slice("--config=".length)' : 'process.argv[1]';
+ const code = `const input=JSON.parse(${read}); process.stdout.write(JSON.stringify({token:input.credentials[0].token,city:input.city,count:input.count}));`;
+ const original = { payload: { kind: 'command', argv: [process.execPath, '-e', code,
+ ...(source === 'stdin' ? [] : ['--', source === 'assignment' ? `--config=${input}` : input])],
+ ...(source === 'stdin' ? { input } : {}), cwd: root } };
+ await shared.store.write(root, 'bot', { version: 1, env: {}, mcp: [], credentials: [], automations: {
+ routine: { kind: 'openclaw', handover: 'ready', original, sourceRoot: root },
+ } }, () => {});
+ const signal = new AbortController().signal;
+ const prepared = await prepareImportedAutomation(root, routine, 'structured-run', signal, () => {});
+ expect(JSON.parse(prepared!.direct!)).toEqual({ token: expect.stringMatching(/^\[command_literal_/), city: 'Paris', count: 7 });
+ const saved = (await shared.store.read(root, 'bot', () => {}))!.automations!.routine!;
+ expect(saved.original).toEqual(original);
+ expect(saved.prepared?.direct).toBe(prepared!.direct);
+ // Old private output must be masked even when no process is executed again.
+ const legacy = JSON.stringify({ token: secret, city: 'Paris', count: 7 });
+ await shared.store.update(root, 'bot', () => {}, env => {
+ env.automations!.routine!.prepared = { runId: 'structured-run', prompt: '', direct: legacy };
+ });
+ expect((await prepareImportedAutomation(root, routine, 'structured-run', signal, () => {}))?.direct).toBe(prepared!.direct);
+ await finishImportedAutomation(root, routine, 'chat', 'structured-run', legacy, true, signal, () => {});
+ expect(shared.message.mock.calls[0]![1].content).toBe(prepared!.direct);
+});
diff --git a/apps/desktop/src/main/bot-import/automationRuntime.ts b/apps/desktop/src/main/bot-import/automationRuntime.ts
index adf42efb2f..b8dca425f3 100644
--- a/apps/desktop/src/main/bot-import/automationRuntime.ts
+++ b/apps/desktop/src/main/bot-import/automationRuntime.ts
@@ -23,6 +23,17 @@ async function outputSecrets(environment: CompanionEnvironment, job: Record<stri
const cwd = command.cwd ? await fs.realpath(command.cwd).catch(() => undefined) : undefined;
const values = [command.command, command.cwd, cwd, command.input,
...command.args.flatMap(arg => [arg, /^--?[\w-]+=(.+)$/s.exec(arg)?.[1]])].filter((value): value is string => !!value);
+ const structured: CompanionEnvironment['credentials'] = [];
+ for (const value of new Set(values)) {
+ try {
+ const parsed: unknown = JSON.parse(value);
+ if (typeof parsed === 'string') values.push(parsed);
+ else if (parsed && typeof parsed === 'object') structured.push({ id: `command_${structured.length}`, format: 'command-input', value: parsed });
+ } catch { /* Non-JSON command literals still receive exact-value masking. */ }
+ }
+ // Reuse credential-field classification for nested objects/arrays, keeping
+ // ordinary report values readable. Raw and JSON-escaped children are private.
+ values.push(...Object.values(importedContentRedactions({ env: {}, mcp: [], credentials: structured })));
for (const [index, value] of [...new Set(values)].entries()) {
secrets[`command_literal_${index}`] = value;
const escaped = JSON.stringify(value).slice(1, -1);