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): bound legacy reads and redact command auth headers

Chris7fb7d4de3074
diff --git a/apps/desktop/src/main/bot-import/README.md b/apps/desktop/src/main/bot-import/README.md
index d4494b9445..91b32c3282 100644
--- a/apps/desktop/src/main/bot-import/README.md
+++ b/apps/desktop/src/main/bot-import/README.md
@@ -5,6 +5,13 @@ Remote Resource transport, and the `companion_import.import_agent` command.
Desktop IPC encodes stable import errors through `throwIpcError`; the form uses
the shared decoder to distinguish editable rejections from uncertain outcomes.
Mobile retains the existing Remote Resource error codes and retry behavior.
+Controllers without `companion-import-chunks-v1` keep the complete legacy
+projection when its serialized UTF-8 JSON fits 1.5 MiB, leaving envelope space
+within the 2 MiB frame. Larger source lists, previews and results return the
+existing `UNSUPPORTED_CAPABILITY` error with `IMPORT_CLIENT_UPGRADE_REQUIRED`;
+no partial selection/catalog is presented. Older UI may show its generic error,
+so upgrading the controller is required for large reads. Chunk-capable clients
+and local imports keep the complete data; no import count cap is restored.
The command has `sources`, `preview`, `start`, and `status` operations. Callers
retain one `requestId` across reconnects and retries. Previews expose selectable
metadata, never source paths, environment values or credential contents.
@@ -577,6 +584,10 @@ Readable import copies collect credentials from structured argv/stdin as well as
command env. The same literal/JSON/URL 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
+separate `-H`/`--header` arguments, `--header=value`, and joined `-Hvalue` forms
+in both publication and execution-output paths; original argv remains intact.
It does not blanket-mask ordinary positional arguments or command settings:
doing so corrupts day counts, output formats and subcommands in imported text.
Unlabelled opaque positional values are not newly classified as credentials by
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 44d9271cdd..cacf3d1a98 100644
--- a/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts
+++ b/apps/desktop/src/main/bot-import/__tests__/automationRuntime.test.ts
@@ -319,3 +319,27 @@ it.each(['env', 'inherited-env', 'stdin', 'argv', 'assignment'] as const)('masks
await finishImportedAutomation(root, routine, 'chat', 'retry-run', 'must reuse pending output', true, signal, () => {});
expect(shared.message.mock.calls[0]![1].content).toBe(result!.direct);
});
+
+it.each(['-H', '--header=', '-Hjoined', '--proxy-header'])('masks an echoed authorization payload from %s before caching and publishing', async option => {
+ const header = `${option === '--proxy-header' ? 'Proxy-' : ''}Authorization: Bearer fixture-header-token`;
+ const args = option === '--header=' ? [option + header] : option === '-Hjoined' ? ['-H' + header] : [option, header];
+ const code = 'const header=process.argv.slice(1).find(arg=>arg.includes("Authorization:")); process.stdout.write(header.split("Bearer ")[1]+" public report");';
+ const original = { payload: { kind: 'command', argv: [process.execPath, '-e', code, '--', ...args], cwd: root } };
+ 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).not.toContain('fixture-header-token');
+ expect(result!.direct).toContain('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: 'fixture-header-token public report' };
+ });
+ expect((await prepareImportedAutomation(root, routine, 'header-run', signal, () => {}))?.direct).toBe(result!.direct);
+ await finishImportedAutomation(root, routine, 'chat', 'header-run', 'fixture-header-token public report', true, signal, () => {});
+ expect(shared.message.mock.calls[0]![1].content).toBe(result!.direct);
+});
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 cd9528f2f3..cc5d6fabee 100644
--- a/apps/desktop/src/main/bot-import/__tests__/environmentSelection.test.ts
+++ b/apps/desktop/src/main/bot-import/__tests__/environmentSelection.test.ts
@@ -47,3 +47,23 @@ it.each(['stdin-object', 'stdin-string', 'argv-json', 'argv-assignment', 'argv-v
}
expect(items).toEqual(before);
});
+
+it.each([
+ ['-H', 'Authorization: Bearer fixture-header-token'],
+ ['--header', 'authorization: bearer fixture-header-token '],
+ ['--header=Authorization: Bearer fixture-header-token'],
+ ['-HAuthorization: Bearer fixture-header-token'],
+ ['--proxy-header', 'Proxy-Authorization: Bearer fixture-header-token'],
+ ['-H', 'Authorization: Basic fixture-header-token'],
+])('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 },
+ automation: { sourceId: 'job', fingerprint: 'fixture', original: { payload } } }];
+ const before = structuredClone(items);
+ for (const collect of [previewImportRedactions, selectedImportRedactions]) {
+ const readable = redactEnvironmentValues('fixture-header-token curl -H --header Authorization Bearer Basic Accept: application/json Content-Type: application/json', collect(items));
+ expect(readable).not.toContain('fixture-header-token');
+ expect(readable).toContain('curl -H --header Authorization Bearer Basic Accept: application/json Content-Type: application/json');
+ }
+ expect(items).toEqual(before);
+});
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 100db2a404..fccfab1c92 100644
--- a/apps/desktop/src/main/bot-import/__tests__/host.test.ts
+++ b/apps/desktop/src/main/bot-import/__tests__/host.test.ts
@@ -447,7 +447,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'];
+ const literalSecrets = ['fixture-argv-token', 'fixture-stdin-token', 'fixture-plain-token', 'fixture-header-token'];
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 +455,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: ['node', '-e', '', '--config=' + JSON.stringify({ token: literalSecrets[0], city: 'Paris' }), '--token=' + literalSecrets[2]], input: JSON.stringify({ credentials: [{ privateKeyPem: literalSecrets[1] }], count: 7 }), env: { CONFIG: config, 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: ['node', '-e', '', '--config=' + JSON.stringify({ token: literalSecrets[0], city: 'Paris' }), '--token=' + literalSecrets[2], '-H', 'Authorization: Bearer ' + literalSecrets[3]], input: JSON.stringify({ credentials: [{ privateKeyPem: literalSecrets[1] }], count: 7 }), env: { CONFIG: config, 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,
@@ -1003,9 +1003,10 @@ it('transfers a 10,000-entry preview in bounded, immutable chunks and preserves
await expect(readRemoteCompanionImport(continuation, 'phone', true)).rejects.toThrow('OWNER_CHANGED');
h.boundary = false;
const [legacySource] = await listCompanionImportSources('old-phone');
- const legacy = await readRemoteCompanionImport(`preview:${legacySource!.id}`, 'old-phone', false);
- expect(legacy).not.toHaveProperty('chunk');
- expect((legacy.preview as { entries: unknown[] }).entries).toHaveLength(10_000);
+ await expect(readRemoteCompanionImport(`preview:${legacySource!.id}`, 'old-phone', false)).rejects.toThrow('IMPORT_CLIENT_UPGRADE_REQUIRED');
+ expect(h.created).toBe(false);
+ expect(h.snapshot.items).toHaveLength(10_000);
+ expect(await readRemoteCompanionImport(continuation, 'phone', true)).toEqual(again);
});
it.each(['directory', undefined] as const)('retries a repaired memory directory with its original request, including legacy kind=%s', async kind => {
@@ -1594,3 +1595,36 @@ it.each([false, true])('pages setup without vault reads after one-time legacy me
await fs.writeFile(receiptFile, JSON.stringify(receipt));
await expect(getCompanionImportSetupStatus(result.botId, h.root, () => {}, 0)).rejects.toThrow('INVALID_COMPANION');
});
+
+it.each(['short', 'unicode', 'escaped'] as const)('checks serialized bytes for legacy %s previews without truncating entries', async kind => {
+ const name = kind === 'unicode' ? '漢'.repeat(600_000) : kind === 'escaped' ? '\u0000'.repeat(300_000) : 'short';
+ h.snapshot.items = [{ view: { id: 'skill', name, category: 'skills', selected: true } }];
+ const [source] = await listCompanionImportSources('old-phone');
+ const read = readRemoteCompanionImport(`preview:${source!.id}`, 'old-phone', false);
+ if (kind === 'short') {
+ const data = await read;
+ expect(data).not.toHaveProperty('chunk');
+ expect(data.preview).toMatchObject({ entries: h.snapshot.items.map(item => item.view) });
+ } else {
+ await expect(read).rejects.toThrow('IMPORT_CLIENT_UPGRADE_REQUIRED');
+ expect(h.created).toBe(false);
+ }
+});
+
+it('bounds legacy source lists and saved results while preserving the chunk-capable path', async () => {
+ h.readName.mockResolvedValue('漢'.repeat(600_000));
+ await expect(readRemoteCompanionImport('sources', 'old-phone', false)).rejects.toThrow('IMPORT_CLIENT_UPGRADE_REQUIRED');
+ const requestId = 'fixture-legacy-large-result';
+ const result = { requestId, botId: 'bot', status: 'needs-attention', saved: true,
+ checks: Array.from({ length: 25_000 }, (_, index) => ({ entryId: `entry-${index}`, status: 'needs-attention', message: 'IMPORT_ITEM_FAILED' })) };
+ await fs.mkdir(path.join(h.root, 'companion-imports'), { recursive: true });
+ await fs.writeFile(path.join(h.root, 'companion-imports', `${requestId}.json`), JSON.stringify({
+ result, companionCreated: true, checkpointSaved: true, handoverMarkers: true,
+ }));
+ await expect(readRemoteCompanionImport(`result:${requestId}`, 'old-phone', false)).rejects.toThrow('IMPORT_CLIENT_UPGRADE_REQUIRED');
+ const api = remoteCompanionImportApi(async id => ({ blocks: [{ primitive: 'companion-import',
+ data: await readRemoteCompanionImport(id, 'new-phone', true) }] }), async () => {});
+ expect((await api.sources())[0]?.name).toBe('漢'.repeat(600_000));
+ expect(await api.status(requestId)).toEqual(result);
+ expect(h.created).toBe(false);
+});
diff --git a/apps/desktop/src/main/bot-import/__tests__/registration.test.ts b/apps/desktop/src/main/bot-import/__tests__/registration.test.ts
index bfa67ff9df..700396e1da 100644
--- a/apps/desktop/src/main/bot-import/__tests__/registration.test.ts
+++ b/apps/desktop/src/main/bot-import/__tests__/registration.test.ts
@@ -56,3 +56,10 @@ it('acknowledges a selection chunk without navigating before the host accepts th
h.start.mockResolvedValueOnce(undefined);
expect(await invoke()).toMatchObject({ effects: [] });
});
+
+it('returns a bounded upgrade error through the legacy Remote Resource IPC', async () => {
+ h.channel = 'maker:remote-resources:get';
+ h.remote.mockRejectedValueOnce(new CompanionImportError('IMPORT_CLIENT_UPGRADE_REQUIRED'));
+ await expect(h.handlers.get(h.channel)!({}, { ref: { collectionId: 'companion-import', kind: 'import', id: 'preview:source' },
+ client: { protocolVersion: 1, primitives: ['companion-import'] } })).rejects.toThrow('[UNSUPPORTED_CAPABILITY] IMPORT_CLIENT_UPGRADE_REQUIRED');
+});
diff --git a/apps/desktop/src/main/bot-import/commandRedactions.ts b/apps/desktop/src/main/bot-import/commandRedactions.ts
index a1a8d52f5f..b3398615a0 100644
--- a/apps/desktop/src/main/bot-import/commandRedactions.ts
+++ b/apps/desktop/src/main/bot-import/commandRedactions.ts
@@ -8,6 +8,11 @@ export function commandLiteralRedactions(literals: string[], environmentValues:
const values = maskLiterals ? [...candidates] : [];
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]!);
// command-env applies both the credential-field rules and URL component
// traversal, including URLs beneath ordinary structured keys like endpoint.
structured.push({ id: `command_${structured.length}`, format: 'command-env', value });
diff --git a/apps/desktop/src/main/bot-import/host.ts b/apps/desktop/src/main/bot-import/host.ts
index 7e68dde039..2a5150f569 100644
--- a/apps/desktop/src/main/bot-import/host.ts
+++ b/apps/desktop/src/main/bot-import/host.ts
@@ -147,8 +147,13 @@ export async function readRemoteCompanionImport(id: string, controller: string,
: id.startsWith('result:') ? { result: await getCompanionImportResult(id.slice(7)) ?? null } : undefined;
scope.assert();
if (!data) throw new CompanionImportError('INVALID_REQUEST');
- if (!chunked) return data;
const text = JSON.stringify(data);
+ if (!chunked) {
+ // Keep the original shape for older controllers, with room for the resource
+ // and invoke envelopes inside a 2 MiB frame. Never silently omit entries.
+ if (Buffer.byteLength(text) > 1.5 * 1024 * 1024) throw new CompanionImportError('IMPORT_CLIENT_UPGRADE_REQUIRED');
+ return data;
+ }
if (text.length <= COMPANION_IMPORT_CHUNK_LENGTH) return data;
if (text.length > COMPANION_IMPORT_READ_MAX_LENGTH || text.length * 2 > MAX_SNAPSHOT_BYTES) throw new CompanionImportError('SOURCE_SNAPSHOT_TOO_LARGE');
prune(remoteReads);
diff --git a/apps/desktop/src/main/bot-import/registration.ts b/apps/desktop/src/main/bot-import/registration.ts
index 1ce3f4e674..cf75cb4339 100644
--- a/apps/desktop/src/main/bot-import/registration.ts
+++ b/apps/desktop/src/main/bot-import/registration.ts
@@ -34,7 +34,10 @@ export function registerCompanionImport(): void {
return { ref: request.ref, revision: '1', display: { title: 'Import' }, links: [],
blocks: [{ id: 'import', primitive: 'companion-import', fallbackMarkdown: 'Hermes / OpenClaw', data }],
actions: id.startsWith('preview:') ? [{ id: 'import', label: 'Import' }] : [] };
- } catch (error) { throw new RemoteResourceRegistryError('NOT_FOUND', error instanceof CompanionImportError ? error.code : 'IMPORT_FAILED'); }
+ } catch (error) {
+ const code = error instanceof CompanionImportError ? error.code : 'IMPORT_FAILED';
+ throw new RemoteResourceRegistryError(code === 'IMPORT_CLIENT_UPGRADE_REQUIRED' ? 'UNSUPPORTED_CAPABILITY' : 'NOT_FOUND', code);
+ }
},
async invoke(context, request) {
try {