Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(desktop): sanitize heterogeneous-agent attachment cache filenames (#13937)

Junghwan2c43f409d971
diff --git a/apps/desktop/src/main/controllers/HeterogeneousAgentCtr.ts b/apps/desktop/src/main/controllers/HeterogeneousAgentCtr.ts
index c60f8d68108..04a9d7c80be 100644
--- a/apps/desktop/src/main/controllers/HeterogeneousAgentCtr.ts
+++ b/apps/desktop/src/main/controllers/HeterogeneousAgentCtr.ts
@@ -1,6 +1,6 @@
import type { ChildProcess } from 'node:child_process';
import { spawn } from 'node:child_process';
-import { randomUUID } from 'node:crypto';
+import { createHash, randomUUID } from 'node:crypto';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import type { Readable, Writable } from 'node:stream';
@@ -137,6 +137,18 @@ export default class HeterogeneousAgentCtr extends ControllerModule {
return join(this.app.appStoragePath, FILE_CACHE_DIR);
}
+ /**
+ * Derive a filesystem-safe cache key for attachments.
+ *
+ * Never use the raw image id as a path segment — upstream callers can persist
+ * arbitrary ids and path.join would treat traversal sequences as real
+ * directories. A stable hash preserves cache hits without trusting the id as a
+ * filename.
+ */
+ private getImageCacheKey(imageId: string): string {
+ return createHash('sha256').update(imageId).digest('hex');
+ }
+
/**
* Download an image by URL, with local disk cache keyed by id.
*/
@@ -144,8 +156,9 @@ export default class HeterogeneousAgentCtr extends ControllerModule {
image: ImageAttachment,
): Promise<{ buffer: Buffer; mimeType: string }> {
const cacheDir = this.fileCacheDir;
- const metaPath = join(cacheDir, `${image.id}.meta`);
- const dataPath = join(cacheDir, image.id);
+ const cacheKey = this.getImageCacheKey(image.id);
+ const metaPath = join(cacheDir, `${cacheKey}.meta`);
+ const dataPath = join(cacheDir, cacheKey);
// Check cache first
try {
diff --git a/apps/desktop/src/main/controllers/__tests__/HeterogeneousAgentCtr.test.ts b/apps/desktop/src/main/controllers/__tests__/HeterogeneousAgentCtr.test.ts
new file mode 100644
index 00000000000..58bd4ae1afd
--- /dev/null
+++ b/apps/desktop/src/main/controllers/__tests__/HeterogeneousAgentCtr.test.ts
@@ -0,0 +1,83 @@
+import { access, mkdtemp, readdir, readFile, rm, unlink, writeFile } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import path from 'node:path';
+
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+vi.mock('electron', () => ({
+ BrowserWindow: { getAllWindows: () => [] },
+ app: { on: vi.fn() },
+ ipcMain: { handle: vi.fn() },
+}));
+
+vi.mock('@/utils/logger', () => ({
+ createLogger: () => ({
+ debug: vi.fn(),
+ error: vi.fn(),
+ info: vi.fn(),
+ verbose: vi.fn(),
+ warn: vi.fn(),
+ }),
+}));
+
+import HeterogeneousAgentCtr from '../HeterogeneousAgentCtr';
+
+describe('HeterogeneousAgentCtr', () => {
+ let appStoragePath: string;
+
+ beforeEach(async () => {
+ appStoragePath = await mkdtemp(path.join(tmpdir(), 'lobehub-hetero-'));
+ });
+
+ afterEach(async () => {
+ await rm(appStoragePath, { force: true, recursive: true });
+ });
+
+ describe('resolveImage', () => {
+ it('stores traversal-looking ids inside the cache root via a stable hash key', async () => {
+ const ctr = new HeterogeneousAgentCtr({ appStoragePath } as any);
+ const cacheDir = path.join(appStoragePath, 'heteroAgent/files');
+ const escapedTargetName = `${path.basename(appStoragePath)}-outside-storage`;
+ const escapePath = path.join(cacheDir, `../../../${escapedTargetName}`);
+
+ try {
+ await unlink(escapePath);
+ } catch {}
+
+ await (ctr as any).resolveImage({
+ id: `../../../${escapedTargetName}`,
+ url: 'data:text/plain;base64,T1VUU0lERQ==',
+ });
+
+ const cacheEntries = await readdir(cacheDir);
+
+ expect(cacheEntries).toHaveLength(2);
+ expect(cacheEntries.every((entry) => /^[a-f0-9]{64}(\.meta)?$/.test(entry))).toBe(true);
+ await expect(access(escapePath)).rejects.toThrow();
+
+ try {
+ await unlink(escapePath);
+ } catch {}
+ });
+
+ it('does not trust pre-seeded out-of-root traversal cache files as cache hits', async () => {
+ const ctr = new HeterogeneousAgentCtr({ appStoragePath } as any);
+ const cacheDir = path.join(appStoragePath, 'heteroAgent/files');
+ const traversalId = '../../preexisting-secret';
+ const outOfRootDataPath = path.join(cacheDir, traversalId);
+ const outOfRootMetaPath = path.join(cacheDir, `${traversalId}.meta`);
+
+ await writeFile(outOfRootDataPath, 'SECRET');
+ await writeFile(outOfRootMetaPath, JSON.stringify({ id: traversalId, mimeType: 'text/plain' }));
+
+ const result = await (ctr as any).resolveImage({
+ id: traversalId,
+ url: 'data:text/plain;base64,SUdOT1JFRA==',
+ });
+
+ expect(Buffer.from(result.buffer).toString('utf8')).toBe('IGNORED');
+ expect(result.mimeType).toBe('text/plain');
+ await expect(readFile(outOfRootDataPath, 'utf8')).resolves.toBe('SECRET');
+ });
+ });
+});