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): reconcile permitted symlink reply image aliases

ylliuuu666ac11421ca52
diff --git a/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts b/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts
index 3687d59a10..d8bd783bfd 100644
--- a/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts
+++ b/apps/desktop/src/main/cindy-media/__tests__/taskImageDelivery.test.ts
@@ -147,6 +147,26 @@ describe('task image delivery', () => {
expect(images.text).not.toContain(source);
});
+ it.each(['inline', 'reference'])('reconciles accepted in-task symlink image/file targets (%s)', async mode => {
+ const aliases: string[] = [];
+ for (let i = 0; i < 11; i++) {
+ const source = path.join(work, `source-${i}.png`), alias = path.join(work, `latest-${i}.png`);
+ await fs.writeFile(source, Buffer.concat([PNG, Buffer.from([i])]));
+ await fs.symlink(source, alias);
+ aliases.push(alias);
+ }
+ const body = mode === 'inline'
+ ? aliases.map(alias => `![preview](<${alias}>) [download](<${alias}>)`).join(' ')
+ : aliases.map((alias, i) => `[download][a${i}] ![preview][a${i}]\n\n[a${i}]: <${alias}>\n`).join('\n');
+ const files = await prepareChatReplyFiles(body, 's', () => {});
+ const images = await prepareChatReplyImages(files.text, 's', () => {}, files.attachments);
+ const attachments = mergeChatReplyAttachments(images.attachments, files.attachments);
+ expect(attachments).toHaveLength(11);
+ expect(attachments.every(a => a.category === 'image')).toBe(true);
+ expect(images.text).not.toContain('![');
+ expect(images.text).not.toContain(work);
+ });
+
it.each(['image-first', 'file-first'])('uploads eleven real images once through both reply passes, in source order (%s)', async order => {
const pdf = path.join(work, 'report.pdf');
await fs.writeFile(pdf, '%PDF-1.4');
@@ -192,13 +212,29 @@ describe('task image delivery', () => {
})()).rejects.toThrow('ATTACHMENT_UNAVAILABLE');
});
- it('retains file read safeguards when a rendered image also has a hard-linked download target', async () => {
+ it.each(['direct', 'symlink'])('retains file read safeguards for hard-linked image/download targets (%s)', async mode => {
const source = path.join(work, 'linked.png');
await fs.writeFile(source, PNG);
await fs.link(source, path.join(state.root, 'outside.png'));
- const files = await prepareChatReplyFiles(`![preview](<${source}>) [download](<${source}>)`, 's', () => {});
+ const alias = mode === 'symlink' ? path.join(work, 'latest.png') : source;
+ if (mode === 'symlink') await fs.symlink(source, alias);
+ const files = await prepareChatReplyFiles(`![preview](<${alias}>) [download](<${alias}>)`, 's', () => {});
+ const images = await prepareChatReplyImages(files.text, 's', () => {}, files.attachments);
+ const attachments = mergeChatReplyAttachments(images.attachments, files.attachments);
+ expect(attachments.map(a => a.category)).toEqual(['image', 'file']);
+ await expect(attachments[1]!.readBytes!()).rejects.toThrow('ATTACHMENT_UNAVAILABLE');
+ });
+
+ it('retains file containment when an image symlink points into the image-only scratchpad root', async () => {
+ const scratch = claudeScratchpadPath({ workingDir: work, sdkSessionId: sdk, agentKind: 'cc' })!;
+ await fs.mkdir(scratch, { recursive: true });
+ const source = path.join(scratch, 'scratch.png'), alias = path.join(work, 'latest.png');
+ await fs.writeFile(source, PNG);
+ await fs.symlink(source, alias);
+ const files = await prepareChatReplyFiles(`![preview](<${alias}>) [download](<${alias}>)`, 's', () => {});
const images = await prepareChatReplyImages(files.text, 's', () => {}, files.attachments);
const attachments = mergeChatReplyAttachments(images.attachments, files.attachments);
+ // Image policy already permits this scratchpad; explicit files remain workdir-only.
expect(attachments.map(a => a.category)).toEqual(['image', 'file']);
await expect(attachments[1]!.readBytes!()).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 89d88502d8..c5150dcc2b 100644
--- a/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServerReplyFiles.ts
@@ -247,10 +247,12 @@ export async function prepareChatReplyFiles(text: string, sessionId: string, ass
// full realpath/descriptor containment is still enforced before reading.
let identity = canonicalSource;
let singleFile = false;
+ let symbolic = false;
try {
const stat = await fs.lstat(canonicalSource, { bigint: true });
if (stat.isFile() && stat.ino !== 0n) identity = `${stat.dev}:${stat.ino}`;
- singleFile = stat.isFile() && stat.nlink === 1n;
+ singleFile = stat.isFile() && stat.nlink === 1n && stat.ino !== 0n;
+ symbolic = stat.isSymbolicLink();
} catch (error) {
// Preserve deferred missing-file failure and the shared count limit.
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
@@ -258,8 +260,16 @@ export async function prepareChatReplyFiles(text: string, sessionId: string, ass
assertCurrent();
if (seen.has(identity)) continue;
// Metadata only: bytes still require the live authority and descriptor checks below.
- const realSource = singleFile ? await fs.realpath(canonicalSource).catch(() => undefined) : undefined;
+ const candidate = singleFile || symbolic ? await fs.realpath(canonicalSource).catch(() => undefined) : undefined;
assertCurrent();
+ if (symbolic && candidate && isPathWithin(root, candidate)) {
+ // Both existing readers accept in-root symlinks. Reconcile only a real,
+ // ordinary single-link target; an escape or hard link must retain its reader.
+ const target = await fs.lstat(candidate, { bigint: true }).catch(() => undefined);
+ assertCurrent();
+ singleFile = !!target?.isFile() && target.nlink === 1n && target.ino !== 0n;
+ }
+ const realSource = singleFile && candidate && isPathWithin(root, candidate) ? candidate : undefined;
// Only potential rendered-image aliases defer counting to the validated
// image merger. Independent files still fail at the existing limit here.
if (!realSource || !imageSources.has(realSource)) {
@@ -270,7 +280,7 @@ export async function prepareChatReplyFiles(text: string, sessionId: string, ass
let digest: string | undefined;
attachments.push({ id: canonicalSource, sourceOffset: link.outputStart, name: path.basename(source), category: 'file',
sourceUrl: link.url,
- sourcePaths: realSource && isPathWithin(root, realSource) ? [realSource] : [],
+ sourcePaths: realSource ? [realSource] : [],
mimeType: path.extname(source).toLowerCase() === '.pdf' ? 'application/pdf' : 'application/octet-stream',
size: 0, path: null, url: null, assertCurrent,
readBytes: async () => {