Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(connections): avoid creating home for Claude Code config (#7182)

Ezra Ellettec06dc2799909
diff --git a/apps/screenpipe-app-tauri/lib/__tests__/ai-tools-mcp.test.ts b/apps/screenpipe-app-tauri/lib/__tests__/ai-tools-mcp.test.ts
index eee76d918..d930c4090 100644
--- a/apps/screenpipe-app-tauri/lib/__tests__/ai-tools-mcp.test.ts
+++ b/apps/screenpipe-app-tauri/lib/__tests__/ai-tools-mcp.test.ts
@@ -12,8 +12,11 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
const fsMock = vi.hoisted(() => ({
files: new Map<string, string>(),
unreadable: new Set<string>(),
+ forbiddenDirectories: new Set<string>(),
}));
+const pathMock = vi.hoisted(() => ({ home: "/Users/test" }));
+
const skillsMock = vi.hoisted(() => ({
installExternalAgentSkills: vi.fn(async () => ["a", "b"]),
removeExternalAgentSkills: vi.fn(async () => ["a", "b"]),
@@ -27,15 +30,24 @@ const tauriMock = vi.hoisted(() => ({
setAiToolAutoConnectOptOut: vi.fn(async () => ({ status: "ok", data: null })),
}));
-vi.mock("@tauri-apps/api/path", () => ({
- homeDir: vi.fn(async () => "/Users/test"),
- join: vi.fn(async (...parts: string[]) => parts.join("/")),
- dirname: vi.fn(async (p: string) => p.split("/").slice(0, -1).join("/")),
-}));
+vi.mock("@tauri-apps/api/path", async () => {
+ const { posix, win32 } = await import("node:path");
+ const paths = () => pathMock.home.includes("\\") ? win32 : posix;
+ // Tauri simplifies verbatim Windows prefixes in join/dirname and strips
+ // trailing separators in join, unlike node:path.join.
+ const simplified = (path: string) => path.replace(/^\\\\\?\\/, "");
+ return {
+ homeDir: vi.fn(async () => pathMock.home),
+ join: vi.fn(async (...parts: string[]) => simplified(paths().join(...parts)).replace(/[\\/]+$/, "")),
+ dirname: vi.fn(async (path: string) => simplified(paths().dirname(path))),
+ };
+});
vi.mock("@tauri-apps/plugin-fs", () => ({
exists: vi.fn(async (path: string) => fsMock.files.has(path) || fsMock.unreadable.has(path)),
- mkdir: vi.fn(async () => undefined),
+ mkdir: vi.fn(async (path: string) => {
+ if (fsMock.forbiddenDirectories.has(path)) throw new Error(`forbidden path: ${path}`);
+ }),
readTextFile: vi.fn(async (path: string) => {
if (fsMock.unreadable.has(path)) throw new Error("EACCES: permission denied");
const text = fsMock.files.get(path);
@@ -120,16 +132,46 @@ const tmpsOf = (path: string) =>
Array.from(fsMock.files.keys()).filter((p) => p.startsWith(`${path}.`) && p.endsWith(".tmp"));
beforeEach(() => {
+ pathMock.home = "/Users/test";
tauriMock.resolveAiToolConfigPath.mockReset();
tauriMock.resolveAiToolConfigPath.mockImplementation(async (path) => ({ status: "ok", data: path }));
fsMock.files.clear();
fsMock.unreadable.clear();
+ fsMock.forbiddenDirectories.clear();
skillsMock.installExternalAgentSkills.mockClear();
skillsMock.removeExternalAgentSkills.mockClear();
tauriMock.setAiToolAutoConnectOptOut.mockClear();
});
describe("safe config IO", () => {
+ it.each([
+ ["POSIX", "/Users/test/", "/Users/test/.claude.json", "/Users/test/.claude.json"],
+ ["Windows", "C:\\Users\\test\\", "C:\\Users\\test\\.claude.json", "C:\\Users\\test\\.claude.json"],
+ ["Windows canonical", "C:\\Users\\test\\", "C:\\Users\\test\\.claude.json", "\\\\?\\C:\\Users\\test\\.claude.json"],
+ ])("connects and disconnects Claude Code with a forbidden home directory on %s", async (_, home, configPath, target) => {
+ pathMock.home = home;
+ fsMock.forbiddenDirectories.add(home.slice(0, -1));
+ tauriMock.resolveAiToolConfigPath.mockImplementation(async (path) => ({
+ status: "ok", data: path === configPath ? target : path,
+ }));
+ const seeded = JSON.stringify({ theme: "dark", mcpServers: { other: { command: "other-tool" } } });
+ fsMock.files.set(target, seeded);
+
+ await connectAiTool("claude-code");
+ expect(JSON.parse(fsMock.files.get(target)!).mcpServers.screenpipe).toBeDefined();
+ expect(fsMock.files.get(backupsOf(target)[0])).toBe(seeded);
+ expect(tmpsOf(target)).toEqual([]);
+
+ await disconnectAiTool("claude-code");
+ expect(JSON.parse(fsMock.files.get(target)!)).toEqual(JSON.parse(seeded));
+ expect(tmpsOf(target)).toEqual([]);
+
+ // A first connection also works when the home-level config is absent.
+ fsMock.files.delete(target);
+ await connectAiTool("claude-code");
+ expect(JSON.parse(fsMock.files.get(target)!).mcpServers.screenpipe).toBeDefined();
+ });
+
it.each([
["/Users/test/.codex/config.toml", 'model = "test"\n', installCodexMcp, uninstallCodexMcp],
[CURSOR, '{"theme":"dark"}', installCursorMcp, uninstallCursorMcp],
diff --git a/apps/screenpipe-app-tauri/lib/ai-tools-mcp.ts b/apps/screenpipe-app-tauri/lib/ai-tools-mcp.ts
index 68c147512..9e15d6a0a 100644
--- a/apps/screenpipe-app-tauri/lib/ai-tools-mcp.ts
+++ b/apps/screenpipe-app-tauri/lib/ai-tools-mcp.ts
@@ -248,7 +248,12 @@ async function backupConfigIfExists(configPath: string): Promise<void> {
* a torn config the next read would refuse or misparse.
*/
async function writeConfigAtomic(configPath: string, text: string): Promise<void> {
- await mkdir(await dirname(configPath), { recursive: true });
+ const dir = await dirname(configPath);
+ // Home already exists. Its children are in the filesystem scope, but the
+ // directory itself is not (Claude Code stores its config at ~/.claude.json).
+ if (dir !== await join(await homeDir())) {
+ await mkdir(dir, { recursive: true });
+ }
const tmpPath = `${configPath}.${Date.now()}.${Math.random().toString(36).slice(2, 10)}.tmp`;
await writeFile(tmpPath, new TextEncoder().encode(text));
try {