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(device-link): 给 invoke 结果解压加输出上限

Dashe9576f3739c9
diff --git a/apps/desktop/src/main/device-link/__tests__/transferCodec.test.ts b/apps/desktop/src/main/device-link/__tests__/transferCodec.test.ts
index b3a7523f99..f9b2d0d784 100644
--- a/apps/desktop/src/main/device-link/__tests__/transferCodec.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/transferCodec.test.ts
@@ -17,4 +17,14 @@ describe('desktop device-link transfer codec', () => {
expect(desktopTransferCodec.contentTag!('abc')).toBe(desktopTransferCodec.contentTag!('abc'));
expect(desktopTransferCodec.contentTag!('abc')).not.toBe(desktopTransferCodec.contentTag!('abd'));
});
+
+ it('stops decompressing once the output limit is exceeded', () => {
+ const bomb = desktopTransferCodec.gzipBase64!('a'.repeat(1_000_000));
+ expect(desktopTransferCodec.gunzipBase64!(bomb, 1_000_000)).toHaveLength(1_000_000);
+ expect(() => desktopTransferCodec.gunzipBase64!(bomb, 100_000)).toThrow();
+ // Multi-byte text is measured in JS string length, not UTF-8 bytes.
+ const cjk = desktopTransferCodec.gzipBase64!('弱'.repeat(1_000));
+ expect(desktopTransferCodec.gunzipBase64!(cjk, 1_000)).toHaveLength(1_000);
+ expect(() => desktopTransferCodec.gunzipBase64!(cjk, 999)).toThrow();
+ });
});
diff --git a/apps/desktop/src/main/device-link/transferCodec.ts b/apps/desktop/src/main/device-link/transferCodec.ts
index d904451bc1..29170d86ec 100644
--- a/apps/desktop/src/main/device-link/transferCodec.ts
+++ b/apps/desktop/src/main/device-link/transferCodec.ts
@@ -8,6 +8,11 @@ import type { DeviceLinkTransferCodec } from '@cindy/device-link';
*/
export const desktopTransferCodec: DeviceLinkTransferCodec = {
gzipBase64: (text) => gzipSync(Buffer.from(text, 'utf8'), { level: constants.Z_BEST_SPEED + 3 }).toString('base64'),
- gunzipBase64: (base64) => gunzipSync(Buffer.from(base64, 'base64')).toString('utf8'),
+ gunzipBase64: (base64, maxChars) => {
+ // One UTF-16 unit never needs more than 3 UTF-8 bytes; zlib stops at the cap.
+ const text = gunzipSync(Buffer.from(base64, 'base64'), { maxOutputLength: maxChars * 3 }).toString('utf8');
+ if (text.length > maxChars) throw new RangeError('decompressed invoke result exceeds limit');
+ return text;
+ },
contentTag: (text) => createHash('sha256').update(text).digest('base64url'),
};
diff --git a/apps/mobile/src/device-link/DeviceLinkContext.tsx b/apps/mobile/src/device-link/DeviceLinkContext.tsx
index 403d1e5b8d..0a93671dc0 100644
--- a/apps/mobile/src/device-link/DeviceLinkContext.tsx
+++ b/apps/mobile/src/device-link/DeviceLinkContext.tsx
@@ -166,7 +166,7 @@ import { hasMoreOlderMessages } from '@/session/messagePaging';
import type { InputProjection, PendingInteraction, RemoteMessage } from '@/session/types';
import { prepareVisualMockDeviceLinkContext } from '@/debug/visualMock';
import { meterDeviceLinkInvokes, recordDeviceLinkPush } from '@/debug/deviceLinkTraffic';
-import { decodeGzipBase64Text } from '@/utils/gzipBase64';
+import { decodeGzipBase64TextBounded } from '@/utils/gzipBase64';
export interface DeviceLinkContextValue {
status: DeviceLinkStatus;
@@ -948,7 +948,7 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
createWebSocket: createRnWebSocket,
logger: mobileDeviceLinkLogger,
// 弱网减流量:声明可解 gzip,大结果压缩、内容未变的结果只回标记(被控端支持时)。
- transferCodec: { gunzipBase64: decodeGzipBase64Text },
+ transferCodec: { gunzipBase64: decodeGzipBase64TextBounded },
// Mobile 具备按 deviceId 独立 openLink/订阅/快照恢复能力。可靠 ACK
// 耗尽时只复位该 peer,共享 WSS 和其它远端继续工作;恢复仍只使用所有
// Desktop 版本都支持的 link-open,不增加 wire/capability 要求。
diff --git a/apps/mobile/src/utils/__tests__/gzipBase64.test.ts b/apps/mobile/src/utils/__tests__/gzipBase64.test.ts
new file mode 100644
index 0000000000..886395d790
--- /dev/null
+++ b/apps/mobile/src/utils/__tests__/gzipBase64.test.ts
@@ -0,0 +1,24 @@
+import { gzip } from 'pako';
+import { describe, expect, it } from 'vitest';
+import { decodeGzipBase64Text, decodeGzipBase64TextBounded } from '../gzipBase64';
+
+const toBase64 = (bytes: Uint8Array): string => Buffer.from(bytes).toString('base64');
+
+describe('gzip base64 decoding', () => {
+ it('decodes UTF-8 text within the limit', () => {
+ const text = JSON.stringify({ title: '弱网任务 ✓', rows: 'x'.repeat(50_000) });
+ const encoded = toBase64(gzip(text));
+ expect(decodeGzipBase64TextBounded(encoded, text.length)).toBe(text);
+ expect(decodeGzipBase64Text(encoded)).toBe(text);
+ });
+
+ it('aborts a highly compressible payload once it passes the limit', () => {
+ const encoded = toBase64(gzip('a'.repeat(2_000_000)));
+ expect(encoded.length).toBeLessThan(10_000);
+ expect(() => decodeGzipBase64TextBounded(encoded, 100_000)).toThrow(RangeError);
+ });
+
+ it('rejects corrupt input', () => {
+ expect(() => decodeGzipBase64TextBounded(toBase64(new Uint8Array([1, 2, 3, 4])), 1_000)).toThrow();
+ });
+});
diff --git a/apps/mobile/src/utils/gzipBase64.ts b/apps/mobile/src/utils/gzipBase64.ts
index b536246425..c6ed657957 100644
--- a/apps/mobile/src/utils/gzipBase64.ts
+++ b/apps/mobile/src/utils/gzipBase64.ts
@@ -2,13 +2,31 @@
* gzip + base64 文本解码,无业务依赖:文件浏览 readFile 与 device-link invoke
* 结果传输编码共用。
*/
-import { ungzip } from 'pako';
+import { Inflate, ungzip } from 'pako';
/** readFile 返回 contentEncoding:'gzip' 时的解码(base64 → gunzip → utf8)。 */
export function decodeGzipBase64Text(contentBase64: string): string {
return ungzip(base64ToUint8Array(contentBase64), { to: 'string' });
}
+/** 带输出上限的解码:边解边计数,超过 maxChars 立即中止,不先整段展开。 */
+export function decodeGzipBase64TextBounded(contentBase64: string, maxChars: number): string {
+ const inflator = new Inflate({ to: 'string' });
+ const chunks: string[] = [];
+ let length = 0;
+ // `to: 'string'` delivers text chunks at runtime; @types/pako only declares binary data.
+ inflator.onData = (chunk: unknown) => {
+ if (typeof chunk !== 'string') throw new TypeError('unexpected binary chunk');
+ const text = chunk;
+ length += text.length;
+ if (length > maxChars) throw new RangeError('decompressed text exceeds limit');
+ chunks.push(text);
+ };
+ inflator.push(base64ToUint8Array(contentBase64), true);
+ if (inflator.err) throw new Error(inflator.msg || 'gzip decode failed');
+ return chunks.join('');
+}
+
export function base64ToUint8Array(base64: string): Uint8Array {
const binary = typeof atob === 'function' ? atob(base64) : decodeBase64Fallback(base64);
const bytes = new Uint8Array(binary.length);
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index 001a09c4d8..49933b8d59 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -57,6 +57,8 @@ known 是消息 ID 与主机提供的 SHA-256 正文指纹对。支持的主机
notModified: true } }`;否则在控制端声明可解且压缩确有收益时回 `{ result: null, transfer:
{ tag, gzipBase64 } }`。共享 DeviceLinkClient 在交给调用方之前还原成普通结果,并只用发请求
时固定的缓存快照还原,回包前被淘汰也不影响;标签对不上时返回错误,不返回旧数据。
+解压有输出上限(4M 字符,与传输层单条逻辑消息同量级):解码器边解边计数,超过即中止并
+返回错误,不先整段展开;被控端对超过上限的结果不编码,仍走原有的超限处理。
handler 每次都执行,`notModified` 只省略字节完全相同的结果,因此对所有 channel 语义不变,
不需要逐 channel 登记。控制端缓存按设备、channel、args 键控,最多 32 条、约 8 MB,只在内存,
diff --git a/packages/device-link/src/__tests__/client.test.ts b/packages/device-link/src/__tests__/client.test.ts
index f9ba13febb..64551e5cba 100644
--- a/packages/device-link/src/__tests__/client.test.ts
+++ b/packages/device-link/src/__tests__/client.test.ts
@@ -7696,7 +7696,7 @@ describe('定时重发的单趟预算(TRANSPORT_RETRY_PASS_BUDGET)', () => {
it('invoke 结果传输编码:大结果 gzip,内容未变只回 notModified,调用方拿到的结果不变', async () => {
const codec = {
gzipBase64: (text: string) => gzipSync(Buffer.from(text, 'utf8')).toString('base64'),
- gunzipBase64: (base64: string) => gunzipSync(Buffer.from(base64, 'base64')).toString('utf8'),
+ gunzipBase64: (base64: string, maxChars: number) => gunzipSync(Buffer.from(base64, 'base64'), { maxOutputLength: maxChars }).toString('utf8'),
contentTag: (text: string) => createHash('sha256').update(text).digest('base64url'),
};
const relay = new MemoryRelay();
diff --git a/packages/device-link/src/__tests__/invokeTransfer.test.ts b/packages/device-link/src/__tests__/invokeTransfer.test.ts
index 8969feb99a..ddc956aeec 100644
--- a/packages/device-link/src/__tests__/invokeTransfer.test.ts
+++ b/packages/device-link/src/__tests__/invokeTransfer.test.ts
@@ -2,6 +2,7 @@ import { createHash } from 'node:crypto';
import { gunzipSync, gzipSync } from 'node:zlib';
import { describe, expect, it } from 'vitest';
import {
+ INVOKE_TRANSFER_MAX_CHARS,
INVOKE_TRANSFER_MIN_CHARS,
InboundInvokeTransferOptions,
InvokeResultCache,
@@ -12,7 +13,7 @@ import {
const codec: Required<DeviceLinkTransferCodec> = {
gzipBase64: (text) => gzipSync(Buffer.from(text, 'utf8')).toString('base64'),
- gunzipBase64: (base64) => gunzipSync(Buffer.from(base64, 'base64')).toString('utf8'),
+ gunzipBase64: (base64, maxChars) => gunzipSync(Buffer.from(base64, 'base64'), { maxOutputLength: maxChars }).toString('utf8'),
contentTag: (text) => createHash('sha256').update(text).digest('base64url'),
};
const large = { rows: 'a'.repeat(INVOKE_TRANSFER_MIN_CHARS * 4) };
@@ -102,4 +103,19 @@ describe('invoke result transfer encoding', () => {
expect(inbound.get('ios', 'r-511')).toEqual({ gzip: 1 });
expect(inbound.get('android', 'r-511')).toBeUndefined();
});
+
+ it('leaves results above the decode limit unencoded so the existing oversize path still applies', () => {
+ const huge = { ok: true as const, result: 'a'.repeat(INVOKE_TRANSFER_MAX_CHARS + 1) };
+ expect(encodeInvokeResultTransfer({ gzip: 1 }, huge, codec)).toBe(huge);
+ });
+
+ it('rejects a highly compressible result that expands past the limit instead of returning it', () => {
+ const cache = new InvokeResultCache(codec);
+ const prepared = cache.prepare('desktop', { channel: 'c', args: [] });
+ const bomb = codec.gzipBase64(JSON.stringify('a'.repeat(INVOKE_TRANSFER_MAX_CHARS + 16)));
+ expect(bomb.length).toBeLessThan(64 * 1024);
+ expect(prepared.decode({ ok: true, result: null, transfer: { tag: 't', gzipBase64: bomb } }))
+ .toMatchObject({ ok: false, error: { code: 'IPC_ERROR' } });
+ expect(cache.prepare('desktop', { channel: 'c', args: [] }).payload.transfer?.ifNoneMatch).toBeUndefined();
+ });
});
diff --git a/packages/device-link/src/invokeTransfer.ts b/packages/device-link/src/invokeTransfer.ts
index 5ea282fafb..2e982c6c40 100644
--- a/packages/device-link/src/invokeTransfer.ts
+++ b/packages/device-link/src/invokeTransfer.ts
@@ -32,8 +32,11 @@ export interface InvokeResultTransfer {
/** host 注入:device-link 包保持零依赖,压缩与摘要由各端平台实现提供。 */
export interface DeviceLinkTransferCodec {
- /** 控制端:解开被控端的 gzip 结果。 */
- gunzipBase64?(base64: string): string;
+ /**
+ * 控制端:解开被控端的 gzip 结果。解出的字符串超过 maxChars 时必须在继续
+ * 分配前抛错——压缩包只受传输大小限制,高压缩比结果可以展开成数百 MB。
+ */
+ gunzipBase64?(base64: string, maxChars: number): string;
/** 被控端:压缩结果。 */
gzipBase64?(text: string): string;
/** 被控端:结果内容标签,须抗碰撞(如 SHA-256)。 */
@@ -42,12 +45,17 @@ export interface DeviceLinkTransferCodec {
/** 小结果编码收益抵不过开销,原样发送。 */
export const INVOKE_TRANSFER_MIN_CHARS = 4 * 1024;
+/**
+ * 编码结果的解压上限,与传输层单条逻辑消息上限同量级:更大的结果被控端不编码,
+ * 仍走原有的超限处理;控制端解压超过即判失败。
+ */
+export const INVOKE_TRANSFER_MAX_CHARS = 4 * 1024 * 1024;
const MAX_TAG_CHARS = 128;
/** 缓存键(channel + args)过长说明 args 本身就是大载荷,不值得缓存。 */
const MAX_CACHE_KEY_CHARS = 4 * 1024;
const MAX_CACHE_ENTRIES = 32;
/** JS 字符串按 UTF-16 计,约 8MB 内存。 */
-const MAX_CACHE_CHARS = 4 * 1024 * 1024;
+const MAX_CACHE_CHARS = INVOKE_TRANSFER_MAX_CHARS;
const MAX_INBOUND_OPTIONS = 512;
export function parseInvokeTransferOptions(value: unknown): InvokeTransferOptions | null {
@@ -75,7 +83,8 @@ export function encodeInvokeResultTransfer(
} catch {
return payload;
}
- if (text === undefined || text.length < INVOKE_TRANSFER_MIN_CHARS) return payload;
+ if (text === undefined || text.length < INVOKE_TRANSFER_MIN_CHARS
+ || text.length > INVOKE_TRANSFER_MAX_CHARS) return payload;
const tag = codec.contentTag?.(text);
if (tag !== undefined && options.ifNoneMatch === tag) {
return { ok: true, result: null, transfer: { tag, notModified: true } };
@@ -166,7 +175,7 @@ export class InvokeResultCache {
value = JSON.parse(text);
} else if (typeof transfer.gzipBase64 === 'string') {
if (!this.codec?.gunzipBase64) throw new Error('unexpected gzip result');
- text = this.codec.gunzipBase64(transfer.gzipBase64);
+ text = this.codec.gunzipBase64(transfer.gzipBase64, INVOKE_TRANSFER_MAX_CHARS);
value = JSON.parse(text);
} else {
value = result.result;