Repository navigation
fix(claude): isolate capability probe settings #8908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,66 @@ export const resolveClaudeHomePath = Effect.fn("resolveClaudeHomePath")(function | |
| return path.resolve(homePath.length > 0 ? expandHomePath(homePath) : NodeOS.homedir()); | ||
| }); | ||
|
|
||
| /** | ||
| * Resolve the Claude config directory the spawned CLI uses. An explicit | ||
| * provider override wins, followed by the instance environment, then | ||
| * Claude's default `$HOME/.claude` location. | ||
| */ | ||
| export const resolveClaudeConfigDirPath = Effect.fn("resolveClaudeConfigDirPath")(function* ( | ||
| config: Pick<ClaudeSettings, "homePath">, | ||
| environment?: NodeJS.ProcessEnv, | ||
| cwd?: string, | ||
| ): Effect.fn.Return<string, never, Path.Path> { | ||
| const path = yield* Path.Path; | ||
| const homePath = config.homePath.trim(); | ||
| if (homePath.length > 0) { | ||
| return yield* resolveClaudeHomePath(config); | ||
| } | ||
|
|
||
| const resolvedEnvironment = environment ?? process.env; | ||
| // No tilde expansion here: the spawned CLI receives this env var verbatim | ||
| // (env vars are never shell-expanded), so a literal `~` must stay literal | ||
| // for discovery to scan the same directory the runtime would. A relative | ||
| // value is resolved against the workspace cwd — the subprocess's own cwd — | ||
| // for the same reason. | ||
| const environmentConfigDir = resolvedEnvironment.CLAUDE_CONFIG_DIR?.trim() ?? ""; | ||
| if (environmentConfigDir.length > 0) { | ||
| return cwd ? path.resolve(cwd, environmentConfigDir) : path.resolve(environmentConfigDir); | ||
| } | ||
| const inheritedHome = | ||
| resolvedEnvironment.HOME?.trim() || resolvedEnvironment.USERPROFILE?.trim() || NodeOS.homedir(); | ||
| return path.join(path.resolve(inheritedHome), ".claude"); | ||
| }); | ||
|
|
||
| /** | ||
| * Capability probes run from an existing neutral cwd because Claude treats | ||
| * `<cwd>/.claude/settings.json` as project settings. The intended config dir | ||
| * remains available through CLAUDE_CONFIG_DIR, including when it does not yet | ||
| * exist or arrived as a relative inherited environment variable. | ||
| */ | ||
| export const makeClaudeCapabilitiesProbeContext = Effect.fn("makeClaudeCapabilitiesProbeContext")( | ||
| function* ( | ||
| config: Pick<ClaudeSettings, "homePath">, | ||
| environment?: NodeJS.ProcessEnv, | ||
| workspaceCwd?: string, | ||
| ): Effect.fn.Return< | ||
| { readonly cwd: string; readonly environment: NodeJS.ProcessEnv }, | ||
| never, | ||
| Path.Path | ||
| > { | ||
| const path = yield* Path.Path; | ||
| const resolvedEnvironment = environment ?? process.env; | ||
| const configDirPath = yield* resolveClaudeConfigDirPath(config, environment, workspaceCwd); | ||
| return { | ||
| cwd: path.resolve(NodeOS.tmpdir()), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift Use a private probe directory. Line 71 runs the CLI from the shared system temporary-directory root. If Create an owned, unique, existing temporary subdirectory for the probe cwd. Clean it up when the provider is disposed. Keep the workspace only in 🤖 Prompt for AI Agents |
||
| environment: { | ||
| ...resolvedEnvironment, | ||
| CLAUDE_CONFIG_DIR: configDirPath, | ||
| }, | ||
| }; | ||
| }, | ||
| ); | ||
|
|
||
| export const makeClaudeEnvironment = Effect.fn("makeClaudeEnvironment")(function* ( | ||
| config: Pick<ClaudeSettings, "homePath">, | ||
| baseEnv?: NodeJS.ProcessEnv, | ||
|
|
@@ -47,10 +107,11 @@ export const makeClaudeContinuationGroupKey = Effect.fn("makeClaudeContinuationG | |
| export const makeClaudeCapabilitiesCacheKey = Effect.fn("makeClaudeCapabilitiesCacheKey")( | ||
| function* ( | ||
| config: Pick<ClaudeSettings, "binaryPath" | "homePath">, | ||
| cwd?: string, | ||
| environment?: NodeJS.ProcessEnv, | ||
| workspaceCwd?: string, | ||
| ): Effect.fn.Return<string, never, Path.Path> { | ||
| const resolvedHomePath = yield* resolveClaudeHomePath(config); | ||
| return `${config.binaryPath}\0${resolvedHomePath}\0${cwd ?? ""}`; | ||
| const configDirPath = yield* resolveClaudeConfigDirPath(config, environment, workspaceCwd); | ||
| return `${config.binaryPath}\0${configDirPath}`; | ||
| }, | ||
| ); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { vi } from "vite-plus/test"; | |
| import * as Deferred from "effect/Deferred"; | ||
| import * as Fiber from "effect/Fiber"; | ||
| import * as TestClock from "effect/testing/TestClock"; | ||
| import * as NodeOS from "node:os"; | ||
| import { ClaudeSettings } from "@t3tools/contracts"; | ||
| import * as NodeFSP from "node:fs/promises"; | ||
| import * as NodeServices from "@effect/platform-node/NodeServices"; | ||
|
|
@@ -13,6 +14,7 @@ import * as FileSystem from "effect/FileSystem"; | |
| import * as Path from "effect/Path"; | ||
| import * as Schema from "effect/Schema"; | ||
|
|
||
| import { makeClaudeCapabilitiesProbeContext } from "../Drivers/ClaudeHome.ts"; | ||
| import { | ||
| buildClaudeCapabilitiesProbeQueryOptions, | ||
| CLAUDE_CAPABILITIES_PROBE_SETTING_SOURCES, | ||
|
|
@@ -33,12 +35,14 @@ it("isolates Claude capability probes without dropping workspace setting sources | |
| ENABLE_CLAUDEAI_MCP_SERVERS: "true", | ||
| FORCE_CODE_TERMINAL: "1", | ||
| }, | ||
| cwd: "/workspace/project", | ||
| cwd: "/config/claude", | ||
| workspaceCwd: "/workspace/project", | ||
| }); | ||
|
|
||
| assert.deepEqual(options.mcpServers, {}); | ||
| assert.equal(options.strictMcpConfig, true); | ||
| assert.equal(options.cwd, "/workspace/project"); | ||
| assert.equal(options.cwd, "/config/claude"); | ||
| assert.deepEqual(options.additionalDirectories, ["/workspace/project"]); | ||
| assert.deepEqual(options.settingSources, [...CLAUDE_CAPABILITIES_PROBE_SETTING_SOURCES]); | ||
| assert.deepEqual(options.settings, { disableAllHooks: true }); | ||
| assert.deepEqual(options.allowedTools, []); | ||
|
|
@@ -53,13 +57,14 @@ it("isolates Claude capability probes without dropping workspace setting sources | |
| }); | ||
|
|
||
| it.layer(NodeServices.layer)("Claude capability probe SDK boundary", (it) => { | ||
| it.effect("serializes strict no-MCP options and still resolves account capabilities", () => | ||
| it.effect("runs from an existing neutral cwd when the Claude config dir is missing", () => | ||
| Effect.gen(function* () { | ||
| const fs = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
| const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-probe-sdk-" }); | ||
| const executablePath = path.join(tempDir, "fake-claude.mjs"); | ||
| const invocationPath = path.join(tempDir, "invocation.json"); | ||
| const claudeConfigDir = path.join(tempDir, "missing-claude-config"); | ||
| // The probe aborts the SDK without awaiting the child's exit, and on | ||
| // Windows a directory that is still some process's cwd cannot be | ||
| // removed. Keep the workspace outside the scoped directory and let it | ||
|
|
@@ -97,6 +102,7 @@ it.layer(NodeServices.layer)("Claude capability probe SDK boundary", (it) => { | |
| "writeFileSync(process.env.T3_PROBE_INVOCATION_PATH, JSON.stringify({", | ||
| " args,", | ||
| " cwd: process.cwd(),", | ||
| " configDir: process.env.CLAUDE_CONFIG_DIR,", | ||
| " connectorEnv: process.env.ENABLE_CLAUDEAI_MCP_SERVERS,", | ||
| " mcpConfig,", | ||
| "}));", | ||
|
|
@@ -135,15 +141,25 @@ it.layer(NodeServices.layer)("Claude capability probe SDK boundary", (it) => { | |
| ); | ||
| yield* fs.chmod(executablePath, 0o755); | ||
|
|
||
| const capabilities = yield* probeClaudeCapabilities( | ||
| decodeClaudeSettings({ binaryPath: executablePath }), | ||
| const settings = decodeClaudeSettings({ | ||
| binaryPath: executablePath, | ||
| homePath: claudeConfigDir, | ||
| }); | ||
| const context = yield* makeClaudeCapabilitiesProbeContext( | ||
| settings, | ||
| { | ||
| ...process.env, | ||
| T3_PROBE_INVOCATION_PATH: invocationPath, | ||
| ENABLE_CLAUDEAI_MCP_SERVERS: "true", | ||
| }, | ||
| workspaceCwd, | ||
| ); | ||
| const capabilities = yield* probeClaudeCapabilities( | ||
| settings, | ||
| context.environment, | ||
| context.cwd, | ||
| workspaceCwd, | ||
| ); | ||
|
|
||
| assert.deepEqual(capabilities, { | ||
| email: "[email protected]", | ||
|
|
@@ -167,16 +183,22 @@ it.layer(NodeServices.layer)("Claude capability probe SDK boundary", (it) => { | |
| const invocation = JSON.parse(yield* fs.readFileString(invocationPath)) as { | ||
| readonly args: ReadonlyArray<string>; | ||
| readonly cwd: string; | ||
| readonly configDir: string; | ||
| readonly connectorEnv: string; | ||
| readonly mcpConfig: unknown; | ||
| }; | ||
| assert.equal(invocation.cwd, yield* fs.realPath(workspaceCwd)); | ||
| assert.equal(invocation.cwd, yield* fs.realPath(NodeOS.tmpdir())); | ||
| assert.notEqual(invocation.cwd, yield* fs.realPath(workspaceCwd)); | ||
| assert.equal(invocation.configDir, path.resolve(claudeConfigDir)); | ||
| assert.equal(invocation.connectorEnv, "false"); | ||
| assert.equal(invocation.args.includes("--strict-mcp-config"), true); | ||
| assert.equal(invocation.args.includes("--mcp-config"), false); | ||
| assert.equal(invocation.mcpConfig, undefined); | ||
|
|
||
| assert.equal(invocation.args.includes("--setting-sources=user,project,local"), true); | ||
| const addDirectoryFlagIndex = invocation.args.indexOf("--add-dir"); | ||
| assert.notEqual(addDirectoryFlagIndex, -1); | ||
| assert.equal(invocation.args[addDirectoryFlagIndex + 1], workspaceCwd); | ||
|
|
||
| const settingsFlagIndex = invocation.args.indexOf("--settings"); | ||
| assert.notEqual(settingsFlagIndex, -1); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.