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(claude-code): 校验图片内联边界与远程来源

DavidShen5a1f5d045442
diff --git a/apps/desktop/src/main/__tests__/claudeInput.test.ts b/apps/desktop/src/main/__tests__/claudeInput.test.ts
index f169d27a32..6460adde44 100644
--- a/apps/desktop/src/main/__tests__/claudeInput.test.ts
+++ b/apps/desktop/src/main/__tests__/claudeInput.test.ts
@@ -113,7 +113,9 @@ describe('Claude Code SDK input', () => {
it('falls back to a quoted path when the final image is too large to embed', async () => {
const tempDir = await createTempDir();
const imagePath = path.join(tempDir, 'too-large.png');
- await fs.writeFile(imagePath, Buffer.alloc(5 * 1024 * 1024 + 1));
+ const imageBytes = Buffer.alloc(Math.floor((5 * 1024 * 1024) / 4) * 3 + 1);
+ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy(imageBytes);
+ await fs.writeFile(imagePath, imageBytes);
const imageResizer = { process: vi.fn(async () => imagePath) };
const content: UserMessage['content'] = [
{ type: 'image', path: imagePath, mimeType: 'image/png' },
@@ -125,6 +127,50 @@ describe('Claude Code SDK input', () => {
);
});
+ it('allows image data exactly at the encoded inline limit', async () => {
+ const tempDir = await createTempDir();
+ const imagePath = path.join(tempDir, 'inline-limit.png');
+ const imageBytes = Buffer.alloc(Math.floor((5 * 1024 * 1024) / 4) * 3);
+ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy(imageBytes);
+ await fs.writeFile(imagePath, imageBytes);
+ const imageResizer = { process: vi.fn(async () => imagePath) };
+ const content: UserMessage['content'] = [
+ { type: 'image', path: imagePath, mimeType: 'image/png' },
+ ];
+
+ const result = await toClaudeSdkContent(content, imageResizer);
+
+ expect(result).toEqual([
+ {
+ type: 'image',
+ source: {
+ type: 'base64',
+ media_type: 'image/png',
+ data: imageBytes.toString('base64'),
+ },
+ },
+ ]);
+ });
+
+ it('keeps SSH image paths remote even when the same path exists on the desktop', async () => {
+ const tempDir = await createTempDir();
+ const imagePath = path.join(tempDir, 'remote.png');
+ await fs.writeFile(
+ imagePath,
+ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
+ );
+ const imageResizer = { process: vi.fn(async () => imagePath) };
+ const content: UserMessage['content'] = [
+ { type: 'image', path: imagePath, mimeType: 'image/png' },
+ { type: 'text', text: 'Inspect this' },
+ ];
+
+ expect(await toClaudeSdkContent(content, imageResizer, false)).toBe(
+ `@"${imagePath}" Inspect this`,
+ );
+ expect(imageResizer.process).not.toHaveBeenCalled();
+ });
+
it('does not duplicate mention chips already serialized in text', async () => {
const content: UserMessage['content'] = [
{ type: 'text', text: 'Read @src/app.ts' },
diff --git a/packages/maker-core/src/agents/claude-code/__tests__/plan-mode.test.ts b/packages/maker-core/src/agents/claude-code/__tests__/plan-mode.test.ts
index 3d193d61ea..7707737387 100644
--- a/packages/maker-core/src/agents/claude-code/__tests__/plan-mode.test.ts
+++ b/packages/maker-core/src/agents/claude-code/__tests__/plan-mode.test.ts
@@ -507,6 +507,43 @@ describe('ClaudeCodeAgent plan mode', () => {
await handle.close();
});
+ it('forwards SSH image paths without reading a same-named desktop file', async () => {
+ const configDir = await makeTempDir();
+ process.env.CLAUDE_CONFIG_DIR = configDir;
+ const workingDir = await makeTempDir();
+ const imagePath = path.join(workingDir, 'remote.png');
+ await fs.writeFile(
+ imagePath,
+ Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
+ );
+ const remoteSend = vi.fn(async () => {});
+ const fakeQuery = { ...createFakeQuery(), send: remoteSend };
+ const remoteCcQueryFactory: NonNullable<AgentDeps['remoteCcQueryFactory']> = async () =>
+ fakeQuery as never;
+ const agent = new ClaudeCodeAgent(createDeps({ remoteCcQueryFactory }));
+ const handle = await agent.startSession({
+ sessionId: 'session-remote-image-path',
+ model: 'claude-opus-4-6',
+ workingDir,
+ remoteHostId: 'remote-1',
+ permissionMode: 'auto',
+ });
+
+ await handle.send({
+ type: 'user',
+ content: [
+ { type: 'image', path: imagePath, mimeType: 'image/png' },
+ { type: 'text', text: 'Inspect this' },
+ ],
+ });
+ await vi.waitFor(() => expect(remoteSend).toHaveBeenCalledTimes(1));
+
+ expect(remoteSend.mock.calls[0]?.[0]).toMatchObject({
+ message: { content: `@"${imagePath}" Inspect this` },
+ });
+ await handle.close();
+ });
+
it('overrides remote cc-manager env with a host-materialized Claude route', async () => {
const configDir = await makeTempDir();
process.env.CLAUDE_CONFIG_DIR = configDir;
diff --git a/packages/maker-core/src/agents/claude-code/index.ts b/packages/maker-core/src/agents/claude-code/index.ts
index d03893e01a..7c2c521307 100644
--- a/packages/maker-core/src/agents/claude-code/index.ts
+++ b/packages/maker-core/src/agents/claude-code/index.ts
@@ -420,7 +420,11 @@ interface ClaudeInputImageResizer {
process(absPath: string): Promise<string>;
}
-const CLAUDE_INLINE_IMAGE_MAX_BYTES = 5 * 1024 * 1024;
+const CLAUDE_INLINE_IMAGE_MAX_ENCODED_BYTES = 5 * 1024 * 1024;
+
+function base64EncodedByteLength(rawByteLength: number): number {
+ return Math.ceil(rawByteLength / 3) * 4;
+}
function detectClaudeImageMediaType(data: Buffer): ClaudeImageMediaType | null {
if (
@@ -451,19 +455,27 @@ async function toClaudeImageBlock(imagePath: string): Promise<ClaudeSdkContentBl
try {
handle = await fs.open(imagePath, 'r');
const stat = await handle.stat();
- if (!stat.isFile() || stat.size <= 0 || stat.size > CLAUDE_INLINE_IMAGE_MAX_BYTES) {
+ if (
+ !stat.isFile()
+ || stat.size <= 0
+ || base64EncodedByteLength(stat.size) > CLAUDE_INLINE_IMAGE_MAX_ENCODED_BYTES
+ ) {
return null;
}
const data = await handle.readFile();
- if (data.length === 0 || data.length > CLAUDE_INLINE_IMAGE_MAX_BYTES) return null;
+ if (
+ data.length === 0
+ || base64EncodedByteLength(data.length) > CLAUDE_INLINE_IMAGE_MAX_ENCODED_BYTES
+ ) return null;
const mediaType = detectClaudeImageMediaType(data);
if (!mediaType) return null;
+ const encodedData = data.toString('base64');
return {
type: 'image',
source: {
type: 'base64',
media_type: mediaType,
- data: data.toString('base64'),
+ data: encodedData,
},
};
} catch {
@@ -480,6 +492,7 @@ async function toClaudeImageBlock(imagePath: string): Promise<ClaudeSdkContentBl
export async function toClaudeSdkContent(
content: UserMessage['content'],
imageResizer: ClaudeInputImageResizer = getDefaultImageResizer(),
+ readImagePathsLocally = true,
): Promise<string | ClaudeSdkContentBlock[]> {
if (typeof content === 'string') return content;
@@ -494,7 +507,7 @@ export async function toClaudeSdkContent(
Promise<{ block: ClaudeSdkContentBlock | null; finalPath: string }>
>();
content.forEach((block, idx) => {
- if (block.type === 'image') {
+ if (block.type === 'image' && readImagePathsLocally) {
imageBlockPromises.set(
idx,
imageResizer.process(block.path).then(async (finalPath) => ({
@@ -5132,7 +5145,12 @@ export class ClaudeCodeAgent extends BaseAgent {
reviewReadGrants,
);
}
- const content = await toClaudeSdkContent(message.content);
+ // SSH 图片路径属于远端主机,不能在桌面端压缩或读取;保留路径引用交给远端 SDK。
+ const content = await toClaudeSdkContent(
+ message.content,
+ undefined,
+ !opts.remoteHostId,
+ );
if (sendOpts?.signal?.aborted) {
throw new Error('Claude send cancelled before acceptance');
}
@@ -5234,7 +5252,12 @@ export class ClaudeCodeAgent extends BaseAgent {
reviewReadGrants,
);
}
- const content = await toClaudeSdkContent(message.content);
+ // steer 与 send 保持同一来源边界:SSH 图片路径只由远端 SDK 读取。
+ const content = await toClaudeSdkContent(
+ message.content,
+ undefined,
+ !opts.remoteHostId,
+ );
if (sendOpts?.signal?.aborted) {
throw new Error('Claude steer cancelled before acceptance');
}