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(plugins): raise PNG clipboard limit to 20 MB (#5037)

Praise4d31538ce44a
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/librarySlot.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/librarySlot.test.ts
index fd4bffff58..61a1c0cbf7 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/librarySlot.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/librarySlot.test.ts
@@ -19,6 +19,7 @@ import {
type GhostLibrarySlotDeps,
} from '../librarySlot.js';
import { createHash } from 'node:crypto';
+import { crc32 } from 'node:zlib';
import { LibraryBindingStore } from '../libraryBinding.js';
import { LibraryVault } from '../libraryVault.js';
import { initCustomLibraryTree, openExistingCustomLibrary } from '../libraryDirFd.js';
@@ -1038,6 +1039,44 @@ describe('GhostLibrarySlot', () => {
expect(writeClipboardPng).not.toHaveBeenCalled();
});
+ /** 在 IEND 前插一个合法私有 ancillary 块(miVo),把 MIN_PNG 撑到恰好 totalBytes 字节。 */
+ function paddedPng(totalBytes: number): Buffer {
+ const iendAt = MIN_PNG.byteLength - 12;
+ const type = Buffer.from('miVo', 'ascii');
+ const data = Buffer.alloc(totalBytes - MIN_PNG.byteLength - 12);
+ const header = Buffer.alloc(4);
+ header.writeUInt32BE(data.byteLength);
+ const crc = Buffer.alloc(4);
+ crc.writeUInt32BE(crc32(Buffer.concat([type, data])) >>> 0);
+ const png = Buffer.concat([MIN_PNG.subarray(0, iendAt), header, type, data, crc, MIN_PNG.subarray(iendAt)]);
+ expect(png.byteLength).toBe(totalBytes);
+ return png;
+ }
+
+ it('clipboardWrite: 上限是十进制 20MB,旧 16MiB 以上可写,边界 +1 与 20MiB 拒绝', async () => {
+ expect(LIBRARY_CLIPBOARD_WRITE_MAX_BYTES).toBe(20_000_000);
+ await slot.handleLibraryRequest(GHOST_ID, { op: 'open' });
+ for (const size of [16 * 1024 * 1024 + 1, 19_999_999, 20_000_000]) {
+ clock += 4_000;
+ const r = await slot.handleLibraryRequest(GHOST_ID, {
+ op: 'clipboardWrite', content: paddedPng(size).toString('base64'), encoding: 'base64',
+ });
+ expect(r).toEqual({ ok: true, op: 'clipboardWrite', bytes: size });
+ }
+ expect(writeClipboardPng).toHaveBeenCalledTimes(3);
+ // 20,000,001 与 20,000,000 的 base64 同为 26,666,668 字符:前置长度闸放行,只能靠解码后字节闸拒。
+ const overByOne = paddedPng(20_000_001).toString('base64');
+ expect(overByOne.length).toBe(paddedPng(20_000_000).toString('base64').length);
+ for (const content of [overByOne, paddedPng(20 * 1024 * 1024).toString('base64')]) {
+ clock += 4_000;
+ const r = await slot.handleLibraryRequest(GHOST_ID, {
+ op: 'clipboardWrite', content, encoding: 'base64',
+ });
+ expect(r).toMatchObject({ ok: false, errorCode: 'TOO_LARGE' });
+ }
+ expect(writeClipboardPng).toHaveBeenCalledTimes(3);
+ });
+
it('clipboardWrite: 生产注入无主壳窗 → UNSUPPORTED,不伪装 INTERNAL', async () => {
await slot.handleLibraryRequest(GHOST_ID, { op: 'open' });
clock += 4_000;
diff --git a/apps/desktop/src/main/cindy-brain/forge.ts b/apps/desktop/src/main/cindy-brain/forge.ts
index 54afdfd0b7..29adb1a6f1 100644
--- a/apps/desktop/src/main/cindy-brain/forge.ts
+++ b/apps/desktop/src/main/cindy-brain/forge.ts
@@ -3663,6 +3663,8 @@ await cindy.library({ op: 'db.check', dbPath: 'library.sqlite' }); // quick_ch
确认后先拷到目标旁临时文件再替换,失败不破坏已有文件;
- **clipboardWrite**:只收 \`encoding:'base64'\` 的 PNG 字节,写系统剪贴板位图,
成功回 \`{ ok:true, bytes }\`。不是 saveAs,也不在文件夹中显示作品。
+ PNG 字节上限 20MB(20,000,000 字节,按解码后字节计),超限回 \`TOO_LARGE\`;
+ 旧版宿主上限为 16MiB,同样回 \`TOO_LARGE\`,插件应提示用户更新或改用下载。
空字节 / 非法 encoding / 非 PNG / 超限一律结构化失败,永不 \`ok:true\`。
同插件 3 秒内连发 \`RATE_LIMITED\`;无主壳窗 / 宿主不能写剪贴板 \`UNSUPPORTED\`;
账号切换后旧会话不得继续写(\`LIBRARY_UNAVAILABLE\`)。
diff --git a/apps/desktop/src/main/cindy-brain/librarySlot.ts b/apps/desktop/src/main/cindy-brain/librarySlot.ts
index 42fe636097..bd80313c6d 100644
--- a/apps/desktop/src/main/cindy-brain/librarySlot.ts
+++ b/apps/desktop/src/main/cindy-brain/librarySlot.ts
@@ -38,8 +38,11 @@ import type { LibraryDbResult } from './libraryDbCore.js';
/** 正本相对键:assets/<hash 前 2 位>/<64-hex>/blob.<ext>(不是 <hash>.<ext>)。 */
const LIBRARY_BLOB_REL_RE = /^assets\/([0-9a-f]{2})\/([0-9a-f]{64})\/blob\.([A-Za-z0-9]+)$/i;
const LIBRARY_SIDECAR_BASENAME = new Set(['meta.json', 'preview.webp']);
-/** clipboardWrite 单次 PNG 上限:与 library 单次 write 同为 16MiB,必须是有限整数。 */
-export const LIBRARY_CLIPBOARD_WRITE_MAX_BYTES = 16 * 1024 * 1024;
+/**
+ * clipboardWrite 单次 PNG 上限:十进制 20MB(20,000,000 字节,不是 20MiB),必须是有限整数。
+ * 只管剪贴板;library 单次 write 的 16MiB 分块阈值独立,不随此变。
+ */
+export const LIBRARY_CLIPBOARD_WRITE_MAX_BYTES = 20_000_000;
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
const PNG_IHDR = Buffer.from('IHDR', 'ascii');
const PNG_IEND = Buffer.from('IEND', 'ascii');