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(sharing): accept bounded scoped peers in mirror cache

DavidShen1b732f7c80f8
diff --git a/apps/desktop/src/main/device-link/__tests__/mirrorCacheIpc.test.ts b/apps/desktop/src/main/device-link/__tests__/mirrorCacheIpc.test.ts
index 373f24a445..e0620e7f55 100644
--- a/apps/desktop/src/main/device-link/__tests__/mirrorCacheIpc.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/mirrorCacheIpc.test.ts
@@ -6,6 +6,7 @@
* 「一条里塞任意大字符串」)。`clear` 只接受非空 deviceId、绝不触发整体清,也在这里钉住。
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
+import { sharedTaskGuestPeer, sharedTaskHostPeer } from '@cindy/device-link';
vi.mock('electron', () => ({
app: {
@@ -480,6 +481,57 @@ describe('读期间账号边界推进', () => {
});
describe('标量 id 长度上界', () => {
+ const longPeers = [
+ sharedTaskHostPeer('task-1', '\\'.repeat(128)),
+ sharedTaskGuestPeer('task-1', 'member-1', '\u0000'.repeat(128)),
+ ];
+
+ it.each(longPeers)('long scoped device key %# supports message read/write and device cleanup', async (deviceId) => {
+ expect(deviceId.length).toBeGreaterThan(256);
+ const messages = [{ id: 'm1', clientId: 'c1', content: 'cached' }];
+ await expect(handleMirrorCacheGetMessages(cache, deviceId, 'sess-1')).resolves.toMatchObject({
+ messages: [{ id: 'm1' }],
+ });
+ expect(cache.readMessagesWithInvalidation).toHaveBeenCalledWith(deviceId, 'sess-1');
+ await expect(handleMirrorCachePutMessages(cache, deviceId, 'sess-1', messages)).resolves.toMatchObject({ ok: true });
+ expect(cache.writeMessages).toHaveBeenCalledWith(
+ deviceId, 'sess-1', messages, undefined, undefined, undefined,
+ );
+ await expect(handleMirrorCacheClear(cache, deviceId)).resolves.toEqual({ ok: true });
+ expect(cache.clearDevice).toHaveBeenCalledWith(deviceId);
+ expect(cache.clearAll).not.toHaveBeenCalled();
+ });
+
+ it.each(longPeers)('long scoped device key %# is preserved in session list persistence', async (deviceId) => {
+ const devices = [{ deviceId, deviceName: 'Shared host', sessions: [{ id: 's1', status: 'active' }] }];
+ await handleMirrorCachePutSessionList(cache, devices);
+ expect(cache.writeSessionList).toHaveBeenCalledWith(devices, undefined, undefined);
+ });
+
+ it.each(longPeers)('long scoped key %# does not bypass the session ID limit', async (sessionId) => {
+ await expect(handleMirrorCacheGetMessages(cache, 'dev-1', sessionId)).rejects.toThrow(/INVALID_PARAMS/);
+ await expect(handleMirrorCachePutMessages(cache, 'dev-1', sessionId, [])).rejects.toThrow(/INVALID_PARAMS/);
+ expect(cache.readMessagesWithInvalidation).not.toHaveBeenCalled();
+ expect(cache.writeMessages).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ 'x'.repeat(300),
+ longPeers[0] + '~', // Non-canonical padding must not grant a larger budget.
+ longPeers[0].padEnd(2049, '~'),
+ ])('invalid or oversized device key %# stays rejected by every cache entry point', async (deviceId) => {
+ await expect(handleMirrorCacheGetMessages(cache, deviceId, 'sess-1')).rejects.toThrow(/INVALID_PARAMS/);
+ await expect(handleMirrorCachePutMessages(cache, deviceId, 'sess-1', [])).rejects.toThrow(/INVALID_PARAMS/);
+ await expect(handleMirrorCacheClear(cache, deviceId)).rejects.toThrow(/INVALID_PARAMS/);
+ expect(cache.readMessagesWithInvalidation).not.toHaveBeenCalled();
+ expect(cache.writeMessages).not.toHaveBeenCalled();
+ expect(cache.clearDevice).not.toHaveBeenCalled();
+ await handleMirrorCachePutSessionList(cache, [{ deviceId, deviceName: 'Invalid', sessions: [] }]);
+ expect(cache.writeSessionList).toHaveBeenCalledWith(
+ [{ deviceName: 'Invalid', sessions: [] }], undefined, undefined,
+ );
+ });
+
// review(codex P1):数组与单条字节预算管不到标量字段,而 store 会对**完整字符串**做
// trim + 正则改写 + sha256(同步)—— 一次调用就能拖住 main。
it('超长 deviceId / sessionId → INVALID_PARAMS,不碰 store', async () => {
diff --git a/apps/desktop/src/main/device-link/ipc.ts b/apps/desktop/src/main/device-link/ipc.ts
index 5f7ce851be..3dd726b928 100644
--- a/apps/desktop/src/main/device-link/ipc.ts
+++ b/apps/desktop/src/main/device-link/ipc.ts
@@ -950,16 +950,21 @@ function finalMirrorCacheReadOwnerToken(
* 缓存 id 的长度上界。renderer 被 XSS 时可以塞进任意长的 deviceId / sessionId,而 store 随后
* 会对**完整字符串**做 trim + 正则改写 + sha256(messageFileName / clearDevice),这些都是同步
* 的 —— 一次调用就能拖住 main(数组与单条字节预算管不到标量字段)(review: codex P1)。
- * 真实 id 是 cuid / uuid 量级(≤ 64),给到 256 已经宽松得离谱。
+ * 普通 id 保持 256 上限;共享连接标识包含 JSON 转义后的物理设备 id,由其 parser 单独限长。
*/
const MIRROR_CACHE_MAX_ID_LENGTH = 256;
/** opaque owner token 是 32-byte digest 的 base64url(43 字符);宽松上限防异常 renderer。 */
const MIRROR_CACHE_MAX_OWNER_TOKEN_LENGTH = 128;
-function requireCacheId(value: unknown, name: string): string {
+function isCacheIdWithinLimit(id: string, name: 'deviceId' | 'sessionId'): boolean {
+ return id.length <= MIRROR_CACHE_MAX_ID_LENGTH
+ || (name === 'deviceId' && parseSharedTaskPeer(id) !== null);
+}
+
+function requireCacheId(value: unknown, name: 'deviceId' | 'sessionId'): string {
const id = requireString(value, name);
- if (id.length > MIRROR_CACHE_MAX_ID_LENGTH) {
+ if (!isCacheIdWithinLimit(id, name)) {
throwIpcError('INVALID_PARAMS', `${name} is too long`);
}
return id;
@@ -1140,7 +1145,7 @@ export async function handleMirrorCachePutSessionList(
}
return {
deviceId: typeof source.deviceId === 'string'
- && source.deviceId.length <= MIRROR_CACHE_MAX_ID_LENGTH
+ && isCacheIdWithinLimit(source.deviceId, 'deviceId')
? source.deviceId
: undefined,
deviceName: typeof source.deviceName === 'string'