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(chat): guard reply file links and stabilize CI fixture

ylliuuu668be7cd10bd3a
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.ts
index 361854cd8c..cf84467c21 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServerReplyFiles.test.ts
@@ -1,5 +1,6 @@
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import fs from 'node:fs/promises';
+import { linkSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
@@ -132,6 +133,44 @@ describe('group reply file delivery', () => {
} finally { await fs.unlink(link); }
});
+ it('refuses an in-task hard link to an outside file before returning bytes', async () => {
+ const link = path.join(work, 'hard-link.pdf');
+ await fs.link(path.join(root, 'private.pdf'), link);
+ try {
+ const result = await prepareChatReplyFiles('[file](./hard-link.pdf)', 'lane', () => {});
+ await expect(result.attachments[0]!.readBytes!()).rejects.toThrow('ATTACHMENT_UNAVAILABLE');
+ } finally { await fs.unlink(link); }
+ });
+
+ it('refuses a new hard link created after the descriptor checks', async () => {
+ const alias = path.join(root, 'during-read.pdf');
+ let checks = 0;
+ try {
+ await expect(readReplyFile(pdf, work, () => {
+ if (++checks === 2) linkSync(pdf, alias);
+ })).rejects.toThrow('ATTACHMENT_UNAVAILABLE');
+ } finally { await fs.rm(alias, { force: true }); }
+ });
+
+ it.each([':42', ':42:3', ':42-50', '#L42', '#L42-L50'])('delivers line-located file links %s once alongside a PDF', async suffix => {
+ const source = path.join(work, 'source.ts');
+ await fs.writeFile(source, 'export const answer = 42;');
+ const result = await prepareChatReplyFiles(`[source](source.ts${suffix}) [again](./source.ts) [PDF](<${pdf}>)`, 'lane', () => {});
+ expect(result.text).toBe('source again PDF');
+ expect(result.attachments).toHaveLength(2);
+ expect(result.attachments[0]!.name).toBe('source.ts');
+ expect((await result.attachments[0]!.readBytes!()).toString()).toBe('export const answer = 42;');
+ expect((await result.attachments[1]!.readBytes!()).toString()).toContain('%PDF');
+ });
+
+ it('preserves literal line-like filenames in encoded file URLs and explicit query paths', async () => {
+ const source = path.join(work, 'report#L42');
+ await fs.writeFile(source, 'literal filename');
+ const result = await prepareChatReplyFiles(`[file](${pathToFileURL(source).href}) [query](xdt-file://local/?path=${encodeURIComponent(source)})`, 'lane', () => {});
+ expect(result.attachments).toHaveLength(1);
+ expect((await result.attachments[0]!.readBytes!()).toString()).toBe('literal filename');
+ });
+
it.each([{ remoteHostId: 'ssh-host' }, { status: 'deleted' }, { workingDir: null }])('refuses a task without local authority %j', async change => {
Object.assign(scope.row, change);
await expect(prepareChatReplyFiles('[file](./report.pdf)', 'lane', () => {})).rejects.toThrow('ATTACHMENT_UNAVAILABLE');
diff --git a/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts b/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts
index c7afc9940f..1043b4d9ca 100644
--- a/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts
@@ -20,9 +20,16 @@ const parser = unified().use(remarkParse).use(remarkGfm);
const MAX_BYTES = 100 * 1024 * 1024;
const unavailable = () => new Error('ATTACHMENT_UNAVAILABLE');
+// Local Markdown line locators follow sharedTaskFileReferences/markdownTarget.
+// Query-based URLs carry an explicit path; do not rewrite its literal filename.
+function withoutLineLocator(value: string): string {
+ return value.includes('?') ? value : value.replace(/(?::\d+(?::\d+|-\d+)?|#L\d+(?:-L\d+)?)$/, '');
+}
+
// Match local references, not arbitrary URI schemes or in-message anchors.
function isFileTarget(value: string): boolean {
if (!value || /^[#?]/.test(value)) return false;
+ value = withoutLineLocator(value);
if (/^(?:file|xdt-file|sandbox):/i.test(value) || /^[A-Za-z]:[\\/]/.test(value)) return true;
return !/^[A-Za-z][A-Za-z0-9+.-]*:/.test(value);
}
@@ -76,6 +83,7 @@ export function replyFileLinks(text: string) {
}
function localPath(url: string, workDir: string): string {
+ url = withoutLineLocator(url);
let value = url;
try {
if (/^sandbox:/i.test(url)) {
@@ -106,11 +114,14 @@ export async function readReplyFile(source: string, root: string, assertCurrent:
const real = await fs.realpath(source);
if (!isPathWithin(root, real)) throw unavailable();
const expected = await fs.stat(real, { bigint: true });
+ // A hard link can point to an inode also named outside this task directory.
+ // realpath cannot prove that all of its other names are in scope.
+ if (expected.nlink !== 1n) throw unavailable();
const handle = await fs.open(real, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0));
try {
const before = await handle.stat({ bigint: true });
const checked = await fs.realpath(source);
- if (!before.isFile() || before.size <= 0n || before.size > BigInt(MAX_BYTES)) throw unavailable();
+ if (!before.isFile() || before.nlink !== 1n || before.size <= 0n || before.size > BigInt(MAX_BYTES)) throw unavailable();
if (before.ino === 0n || expected.ino !== before.ino || expected.dev !== before.dev || checked !== real) throw unavailable();
assertCurrent();
const buffer = Buffer.alloc(Number(before.size));
@@ -121,7 +132,7 @@ export async function readReplyFile(source: string, root: string, assertCurrent:
offset += bytesRead;
}
const after = await handle.stat({ bigint: true });
- if (before.size !== after.size || before.mtimeNs !== after.mtimeNs || before.ctimeNs !== after.ctimeNs) throw unavailable();
+ if (after.nlink !== 1n || before.size !== after.size || before.mtimeNs !== after.mtimeNs || before.ctimeNs !== after.ctimeNs) throw unavailable();
assertCurrent();
return buffer;
} finally { await handle.close(); }
diff --git a/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts b/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
index 821665fe3e..c44056a34d 100644
--- a/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
+++ b/packages/maker-core/src/agents/pi/__tests__/cindySubagentRunner.test.ts
@@ -642,6 +642,13 @@ describe('Cindy durable PI Subagent runner', () => {
return Number.isSafeInteger(pid) && pid > 0 ? pid : null;
});
try {
+ // PID publication precedes prompt handling. Closing the runner sooner can
+ // make the child's initial stdout writes fail with EPIPE, so it exits
+ // before the inventory even though the injected kill attempts fail.
+ await waitFor(async () => {
+ const transcript = await readFile(path.join(fixture.runDir, 'transcript.jsonl'), 'utf8').catch(() => '');
+ return transcript.includes('"type":"tool_execution_start"') ? true : null;
+ }, undefined, 'the initial child events to reach the runner before signalling');
fixture.child.kill('SIGTERM');
await waitForClose(fixture.child, fixture.stderr);
const receipt = JSON.parse(await readFile(path.join(fixture.runDir, 'result.json'), 'utf8'));