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 imported credential header values

Chris435dead57f86
diff --git a/apps/desktop/src/main/bot-import/README.md b/apps/desktop/src/main/bot-import/README.md
index ccf17ff4e3..d6044cf7c2 100644
--- a/apps/desktop/src/main/bot-import/README.md
+++ b/apps/desktop/src/main/bot-import/README.md
@@ -599,8 +599,12 @@ Readable import copies collect credentials from structured argv/stdin as well as
command env. The same literal/JSON/URL/form traversal is shared with output masking;
publication treats stdin and credential-named options as private literals and
examines other arguments for structured credential fields and capability URLs.
-Authorization and Proxy-Authorization header literals also mask their credential
-payload without making the scheme or ordinary headers global masks. This covers
+Credential-named header literals (including X-API-Key and X-Auth-Token) mask
+their payload; Authorization/Proxy-Authorization additionally mask the credential
+after the scheme. MCP and commands share Cookie decomposition: every nonempty
+cookie value, including quoted and percent-decoded forms, stays private because
+cookie names are application-defined. This does not make scheme names or ordinary
+Accept/Content-Type values global masks. This covers
separate `-H`/`--header` arguments, `--header=value`, and joined `-Hvalue` forms
in both publication and execution-output paths; original argv remains intact.
Form-encoded stdin, argv and command environment values collect credential-named
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 6cdabccce6..f8056298e3 100644
--- a/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts
+++ b/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts
@@ -379,6 +379,29 @@ it.each(['stdin', 'argv', 'assignment', 'env', 'inherited-env'])('masks form cre
expect(shared.message.mock.calls[0]![1].content).toBe(result!.direct);
});
+it.each(['X-API-Key', 'X-Auth-Token', 'Cookie'])('masks independently echoed %s header values through command execution and retry publication', async name => {
+ const header = `${name}: ${name === 'Cookie' ? 'session=fixture-session%2Fsecret; second="fixture-second-cookie"' : 'fixture-header-secret'}`;
+ const code = 'const h=process.argv[1];const v=h.slice(h.indexOf(":")+1).trim();process.stdout.write((/^cookie:/i.test(h)?v.split(";").map(p=>decodeURIComponent(p.slice(p.indexOf("=")+1).trim().replace(/^"|"$/g,""))).join(" | "):v)+" | public report");';
+ const original = { payload: { kind: 'command', argv: [process.execPath, '-e', code, header], cwd: root } };
+ const output = name === 'Cookie' ? 'fixture-session/secret | fixture-second-cookie | public report' : 'fixture-header-secret | public report';
+ const routine = { id: 'routine', botId: 'bot', prompt: 'Fixture header report' } as Routine;
+ 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 result = await prepareImportedAutomation(root, routine, 'header-run', signal, () => {});
+ expect(result!.direct).toMatch(/^(?:\[command_literal_\w+\] \| ){1,2}public report$/);
+ const saved = (await shared.store.read(root, 'bot', () => {}))!;
+ expect(saved.automations!.routine!.original).toEqual(original);
+ expect(saved.automations!.routine!.prepared?.direct).toBe(result!.direct);
+ await shared.store.update(root, 'bot', () => {}, env => { env.automations!.routine!.prepared = { runId: 'header-run', prompt: '', direct: output }; });
+ expect((await prepareImportedAutomation(root, routine, 'header-run', signal, () => {}))?.direct).toBe(result!.direct);
+ await shared.store.update(root, 'bot', () => {}, env => { env.automations!.routine!.deliveryProgress = { runId: 'header-run', text: output, direct: true, deliveries: [], next: 0 }; });
+ expect((await prepareImportedAutomation(root, routine, 'retry-run', signal, () => {}))?.direct).toBe(result!.direct);
+ await finishImportedAutomation(root, routine, 'chat', 'retry-run', output, true, signal, () => {});
+ expect(shared.message.mock.calls[0]![1].content).toBe(result!.direct);
+});
+
it.each(['-u', '--user', '-U', '--proxy-user', '-ujoined', '-Ujoined', '--user=', '--proxy-user='])
('masks curl %s passwords from output, retry caches and final chat', async option => {
const userinfo = 'alice:fixture-auth:password';
diff --git a/apps/desktop/src/main/bot-import/__tests__/connectionProvider.test.ts b/apps/desktop/src/main/bot-import/__tests__/connectionProvider.test.ts
index 9854c714fa..15c52027c1 100644
--- a/apps/desktop/src/main/bot-import/__tests__/connectionProvider.test.ts
+++ b/apps/desktop/src/main/bot-import/__tests__/connectionProvider.test.ts
@@ -313,8 +313,10 @@ it.each([true, false])('preserves MCP response syntax and isError=%s when enviro
it('redacts resolved catalog credentials without changing schema syntax, tool dispatch or connection configuration', async () => {
const env = { TOKEN: 'fixture-global-token', TYPE: 'object' };
const connection = { name: 'fixture-server', url: 'https://fixture-user:[email protected]/mcp/fixture-path%2Ftoken?key=fixture-url-key#access_token=fixture%2Ffragment%2Bsecret',
- env: { TOKEN: 'fixture-local-token' }, headers: { Authorization: 'Bearer fixture-header-token', 'X-Api-Key': 'fixture-api-key' } };
- const secrets = [env.TOKEN, connection.env.TOKEN, connection.headers.Authorization, 'fixture-header-token', connection.headers['X-Api-Key'], connection.url, 'fixture-user', 'fixture-password', 'fixture-url-key', 'fixture-path%2Ftoken', 'fixture-path/token', 'fixture/fragment+secret', 'fixture%2Ffragment%2Bsecret'];
+ env: { TOKEN: 'fixture-local-token' }, headers: { Authorization: 'Bearer fixture-header-token', 'X-Api-Key': 'fixture-api-key',
+ cOoKiE: 'session=fixture-cookie-session; alternate="fixture-cookie%2Fquoted=="; preference=dark' } };
+ const secrets = [env.TOKEN, connection.env.TOKEN, connection.headers.Authorization, 'fixture-header-token', connection.headers['X-Api-Key'], connection.url, 'fixture-user', 'fixture-password', 'fixture-url-key', 'fixture-path%2Ftoken', 'fixture-path/token', 'fixture/fragment+secret', 'fixture%2Ffragment%2Bsecret',
+ 'fixture-cookie-session', 'fixture-cookie%2Fquoted==', 'fixture-cookie/quoted==', 'dark'];
const echo = secrets.join(' ');
const secretKey = 'argument_fixture-path/token';
const tool: Tool = { name: `read_${connection.env.TOKEN}`, title: echo, description: echo,
diff --git a/apps/desktop/src/main/bot-import/__tests__/environmentSelection.test.ts b/apps/desktop/src/main/bot-import/__tests__/environmentSelection.test.ts
index e5232522de..5dd4dd726a 100644
--- a/apps/desktop/src/main/bot-import/__tests__/environmentSelection.test.ts
+++ b/apps/desktop/src/main/bot-import/__tests__/environmentSelection.test.ts
@@ -56,6 +56,13 @@ it.each([
['-HAuthorization: Bearer fixture-header-token'],
['--proxy-header', 'Proxy-Authorization: Bearer fixture-header-token'],
['-H', 'Authorization: Basic fixture-header-token'],
+ ['-H', 'X-API-Key: fixture-header-token'],
+ ['--header', 'x-auth-token: fixture-header-token'],
+ ['--header=X-API-Key: fixture-header-token'],
+ ['-HX-API-Key: fixture-header-token'],
+ ['--proxy-header', 'X-API-Key: fixture-header-token'],
+ ['-H', 'Cookie: session=fixture-header-token; other=fixture-second-cookie'],
+ ['--header=Cookie: session="fixture-header-token"; empty='],
])('masks the credential payload in command headers %j without masking public header settings', (...args) => {
const payload = { kind: 'command', argv: ['curl', ...args, '-H', 'Accept: application/json', '--header=Content-Type: application/json'] };
const items: ImportItem[] = [{ view: { id: 'job', category: 'automations', name: 'Job', selected: true },
@@ -69,6 +76,21 @@ it.each([
expect(items).toEqual(before);
});
+it.each(['command', 'mcp'])('masks individual %s cookies before publication without changing original headers', source => {
+ const header = 'session=fixture-cookie%2Fone==; repeat="fixture-cookie-two"; repeat=fixture-cookie+three; empty=; emptyQuoted=""; malformed; broken=fixture%not-encoded';
+ const items: ImportItem[] = [{ view: { id: 'entry', category: source === 'mcp' ? 'connections' : 'automations', name: 'Job', selected: true },
+ ...(source === 'mcp' ? { mcp: { name: 'data', headers: { cOoKiE: header }, url: 'https://example.invalid/mcp' } }
+ : { automation: { sourceId: 'job', fingerprint: 'fixture', original: { payload: { kind: 'command', argv: ['curl', '-H', 'Cookie: ' + header] } } } }) }];
+ const original = structuredClone(items);
+ const secrets = ['fixture-cookie%2Fone==', 'fixture-cookie/one==', 'fixture-cookie-two', 'fixture-cookie+three', 'fixture%not-encoded'];
+ for (const collect of [previewImportRedactions, selectedImportRedactions]) {
+ const result = redactEnvironmentValues(`${secrets.join('\n')}\npublic report Cookie Accept Content-Type application/json ""`, collect(items));
+ for (const secret of secrets) expect(result).not.toContain(secret);
+ expect(result).toContain('public report Cookie Accept Content-Type application/json ""');
+ }
+ expect(items).toEqual(original);
+});
+
it.each(['stdin', 'argv', 'assignment', 'env'])('masks credential fields in form-encoded command %s while retaining public values', source => {
const form = 'access_token=fixture-form%2Fsecret%2Bvalue&access_token=fixture+second+secret&%70assword=fixture%3Dpassword&city=Paris&days=7&token_type=Bearer';
const payload = { kind: 'command', argv: ['curl', '--data', ...(source === 'argv' ? [form] : source === 'assignment' ? [`--config=${form}`] : [])],
diff --git a/apps/desktop/src/main/bot-import/__tests__/host.test.ts b/apps/desktop/src/main/bot-import/__tests__/host.test.ts
index 839b1359b1..3ad2a3c3bd 100644
--- a/apps/desktop/src/main/bot-import/__tests__/host.test.ts
+++ b/apps/desktop/src/main/bot-import/__tests__/host.test.ts
@@ -399,7 +399,7 @@ it.each(['hermes', 'openclaw'] as const)('masks %s names from real source files
});
it.each([false, true])('redacts all known credentials from profile/memory copies while importing only selected connections (deselected: %s)', async deselected => {
- const secrets = ['fake-env-key', 'fake-local-key', 'fake-header-token', 'fake/url+key', 'fake-access-token', 'fake-refresh-token', '123:fake-telegram-token'];
+ const secrets = ['fake-env-key', 'fake-local-key', 'fake-header-token', 'fake/url+key', 'fake-access-token', 'fake-refresh-token', '123:fake-telegram-token', 'fixture-cookie-session', 'fixture-cookie/second'];
const text = `简短一点,带点幽默。status en us true 3000\n${secrets.join('\n')}\nfake-unselected-key`;
const documents: ImportSnapshot['items'] = [
{ view: { id: 'soul', name: 'SOUL.md', category: 'personality', selected: true }, role: 'identity', text },
@@ -410,7 +410,7 @@ it.each([false, true])('redacts all known credentials from profile/memory copies
];
h.snapshot.items = [...documents,
{ view: { id: 'env', name: 'env', category: 'connections', selected: true }, env: { DATA_TOKEN: secrets[0]!, LANG: 'en', REGION: 'us', DEBUG: 'true', PORT: '3000' } },
- { view: { id: 'mcp', name: 'Data', category: 'connections', selected: true }, mcp: { name: 'Data', url: 'https://example.invalid/mcp?token=fake%2Furl%2Bkey', env: { KEY: secrets[1]!, REFERENCED: '${DATA_TOKEN}' }, headers: { Authorization: `Bearer ${secrets[2]}` } } },
+ { view: { id: 'mcp', name: 'Data', category: 'connections', selected: true }, mcp: { name: 'Data', url: 'https://example.invalid/mcp?token=fake%2Furl%2Bkey', env: { KEY: secrets[1]!, REFERENCED: '${DATA_TOKEN}' }, headers: { Authorization: `Bearer ${secrets[2]}`, Cookie: 'session=fixture-cookie-session; another="fixture-cookie%2Fsecond"' } } },
{ view: { id: 'oauth', name: 'Auth', category: 'connections', selected: true }, credential: { format: 'native-auth', value: { value: { access_token: secrets[4], nested: { refreshToken: secrets[5] } } } } },
{ view: { id: 'telegram', name: 'Telegram', category: 'connections', selected: true }, credential: { format: 'telegram', value: { token: secrets[6], account: 'default' } } },
{ view: { id: 'excluded', name: 'Excluded', category: 'connections', selected: false }, env: { EXCLUDED: 'fake-unselected-key', UNUSED_ONLY: 'fake-absent-from-selected-content' } },
@@ -435,6 +435,7 @@ it.each([false, true])('redacts all known credentials from profile/memory copies
expect(stored.env.DATA_TOKEN).toBe(secrets[0]);
expect(stored.mcp[0]?.env?.REFERENCED).toBe(secrets[0]);
expect(stored.mcp[0]?.headers?.Authorization).toBe(`Bearer ${secrets[2]}`);
+ expect(stored.mcp[0]?.headers?.Cookie).toBe('session=fixture-cookie-session; another="fixture-cookie%2Fsecond"');
}
expect(stored.env).not.toHaveProperty('EXCLUDED');
expect(JSON.stringify(stored)).not.toContain('fake-absent-from-selected-content');
@@ -447,7 +448,7 @@ it.each([false, true])('redacts all known credentials from profile/memory copies
it.each([false, true])('publishes command env/argv/stdin credentials safely (command selected: %s)', async selected => {
const secret = 'fixture-command-json-token';
const nested = 'fixture-command-json-nested';
- const literalSecrets = ['fixture-argv-token', 'fixture-stdin-token', 'fixture-plain-token', 'fixture-header-token', 'fixture-form argv', 'fixture-form%20argv', 'fixture-form/env', 'fixture-form%2Fenv', 'fixture-curl-password'];
+ const literalSecrets = ['fixture-argv-token', 'fixture-stdin-token', 'fixture-plain-token', 'fixture-header-token', 'fixture-form argv', 'fixture-form%20argv', 'fixture-form/env', 'fixture-form%2Fenv', 'fixture-curl-password', 'fixture-custom-header-key', 'fixture-command-cookie'];
const urlSecrets = ['fixture-hook-token', 'fixture-fragment token', 'fixture-fragment%20token',
'fixture-raw token', 'fixture-raw%20token', 'fixture-raw-query', 'fixture-raw-fragment'];
const config = JSON.stringify({ token: secret, credentials: [{ key: nested }],
@@ -455,7 +456,7 @@ it.each([false, true])('publishes command env/argv/stdin credentials safely (com
unused: { password: 'fixture-unused-command-secret' }, city: 'Paris', count: 7 });
const text = `Keep node --mode -e, Paris and 7. ${secret} ${nested} ${urlSecrets.join(' ')} ${literalSecrets.join(' ')}`;
const skill = `---\nname: report\ndescription: ${text}\n---\n${text}\n`;
- const original = { enabled: false, payload: { kind: 'command', argv: ['curl', '-u', 'alice:fixture-curl-password', '--config=' + JSON.stringify({ token: literalSecrets[0], city: 'Paris' }), '--token=' + literalSecrets[2], '-H', 'Authorization: Bearer ' + literalSecrets[3], '--data', 'access_token=fixture-form%20argv&city=Paris'], input: JSON.stringify({ credentials: [{ privateKeyPem: literalSecrets[1] }], count: 7 }), env: { CONFIG: config, FORM: 'password=fixture-form%2Fenv&days=7', WEBHOOK_URL: 'https://host/hooks/fixture-raw%20token?token=fixture-raw-query#access_token=fixture-raw-fragment' } } };
+ const original = { enabled: false, payload: { kind: 'command', argv: ['curl', '-u', 'alice:fixture-curl-password', '--config=' + JSON.stringify({ token: literalSecrets[0], city: 'Paris' }), '--token=' + literalSecrets[2], '-H', 'Authorization: Bearer ' + literalSecrets[3], '-H', 'X-API-Key: fixture-custom-header-key', '-HCookie: session=fixture-command-cookie', '--data', 'access_token=fixture-form%20argv&city=Paris'], input: JSON.stringify({ credentials: [{ privateKeyPem: literalSecrets[1] }], count: 7 }), env: { CONFIG: config, FORM: 'password=fixture-form%2Fenv&days=7', WEBHOOK_URL: 'https://host/hooks/fixture-raw%20token?token=fixture-raw-query#access_token=fixture-raw-fragment' } } };
h.sourceEnabled = false;
h.snapshot.items = [
{ view: { id: 'task', name: text, category: 'automations', selected, enabled: false }, automation: { sourceId: 'task', fingerprint: 'fixture', original,
diff --git a/apps/desktop/src/main/bot-import/commandRedactions.ts b/apps/desktop/src/main/bot-import/commandRedactions.ts
index 91b52b97e6..f6ae18fc1a 100644
--- a/apps/desktop/src/main/bot-import/commandRedactions.ts
+++ b/apps/desktop/src/main/bot-import/commandRedactions.ts
@@ -1,4 +1,4 @@
-import { importedContentRedactions, isImportedCredentialField } from './connectionCatalog.js';
+import { headerCredentialValues, importedContentRedactions, isImportedCredentialField } from './connectionCatalog.js';
/** Shared by readable import copies and execution output; originals stay intact. */
export function commandLiteralRedactions(literals: string[], environmentValues: string[] = [], maskLiterals = true): Record<string, string> {
@@ -9,10 +9,10 @@ export function commandLiteralRedactions(literals: string[], environmentValues:
const structured: { id: string; format: string; value: unknown }[] = [];
for (const value of new Set([...candidates, ...environmentValues])) {
// curl accepts a separate -H/--header value, --header=value or -Hvalue.
- // Strip the authorization scheme as with MCP headers: commands may echo
- // only its credential payload. Public headers/scheme names are not masks.
- const header = /^(?:-H)?[\t ]*(?:proxy-)?authorization[\t ]*:[\t ]*(\S+[\t ]+(.+?))[\t ]*$/i.exec(value);
- if (header) values.push(header[1]!, header[2]!);
+ // Share credential-header and cookie parsing with MCP: commands may echo
+ // just one private value. Public header values/scheme names are not masks.
+ const header = /^(?:-H)?[\t ]*([\w-]+)[\t ]*:[\t ]*(.+?)[\t ]*$/i.exec(value);
+ if (header) values.push(...headerCredentialValues(header[1]!, header[2]!));
// Form bodies can echo one field independently of the original scalar.
// Keep both wire and decoded values, including repeated/encoded field names;
// ordinary form settings are not credentials. Do not interpret URLs or JSON
diff --git a/apps/desktop/src/main/bot-import/connectionCatalog.ts b/apps/desktop/src/main/bot-import/connectionCatalog.ts
index 69e1024898..604973dffa 100644
--- a/apps/desktop/src/main/bot-import/connectionCatalog.ts
+++ b/apps/desktop/src/main/bot-import/connectionCatalog.ts
@@ -15,14 +15,34 @@ const CREDENTIAL_FIELD = /^(?:keys?|.*(?:api|private|signing|encryption|decrypti
export function isImportedCredentialField(name: string): boolean { return CREDENTIAL_FIELD.test(name); }
+/** Shared transport-header decomposition for MCP and imported commands. Cookie
+ * names are application-defined, so every nonempty cookie value stays private. */
+export function headerCredentialValues(name: string, value: string): string[] {
+ const header = name.trim(); const payload = value.trim();
+ const authorization = /^(proxy-)?authorization$/i.test(header);
+ if (!payload || !authorization && !isImportedCredentialField(header)) return [];
+ const values = [payload];
+ if (authorization) {
+ const credential = /^\S+\s+(.+)$/.exec(payload)?.[1];
+ if (credential) values.push(credential);
+ }
+ if (/^cookie$/i.test(header)) for (const part of payload.split(';')) {
+ const separator = part.indexOf('=');
+ if (separator <= 0 || !part.slice(0, separator).trim()) continue;
+ const raw = part.slice(separator + 1).trim();
+ const unquoted = raw.startsWith('"') && raw.endsWith('"') ? raw.slice(1, -1) : raw;
+ if (!unquoted) continue;
+ values.push(raw, unquoted);
+ try { values.push(decodeURIComponent(unquoted)); } catch { /* Preserve malformed encodings verbatim. */ }
+ }
+ return [...new Set(values)];
+}
+
/** Include resolved connection-local values without overwriting same-named imports. */
export function connectionRedactions(server: ImportedMcpServer, environment: Record<string, string>): Record<string, string> {
const values = [...Object.values(environmentRedactions(environment)), ...Object.values(environmentRedactions(server.env ?? {})), ...Object.values(server.headers ?? {})];
for (const [name, value] of Object.entries(server.headers ?? {})) {
- if (/^(proxy-)?authorization$/i.test(name)) {
- const credential = /^\S+\s+(.+)$/.exec(value)?.[1];
- if (credential) values.push(credential);
- }
+ values.push(...headerCredentialValues(name, value));
}
if (server.url) {
values.push(...urlCredentialValues(server.url, true));