Skip to content

Commit 35014f5

Browse files
committed
fix(claude): keep proxy credentials out of process arguments
1 parent 1bbcaf8 commit 35014f5

3 files changed

Lines changed: 85 additions & 6 deletions

File tree

‎apps/server/src/textGeneration/ClaudeTextGeneration.test.ts‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,20 @@ function withFakeClaudeEnv<A, E, R>(
6868
claudeConfig?: Partial<ClaudeSettings>;
6969
proxy?: { readonly url: string; readonly apiKey: string };
7070
},
71-
effectFn: (textGeneration: TextGeneration.TextGeneration["Service"]) => Effect.Effect<A, E, R>,
71+
effectFn: (
72+
textGeneration: TextGeneration.TextGeneration["Service"],
73+
tempDir: string,
74+
) => Effect.Effect<A, E, R>,
7275
) {
7376
return Effect.gen(function* () {
7477
const fs = yield* FileSystem.FileSystem;
7578
const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-claude-text-" });
7679
const binDir = yield* makeFakeClaudeBinary(tempDir, input.lifecyclePort);
7780
const pathDelimiter = (yield* HostProcess.isWindows) ? ";" : ":";
81+
const path = yield* Path.Path;
82+
const platform = yield* HostProcess.Platform;
83+
const previousSettingsExpected = process.env.T3_FAKE_CLAUDE_SETTINGS_EXPECTED;
84+
const previousSettingsReceipt = process.env.T3_FAKE_CLAUDE_SETTINGS_RECEIPT;
7885
const previousPath = process.env.PATH;
7986
const previousOutput = process.env.T3_FAKE_CLAUDE_OUTPUT;
8087
const previousExitCode = process.env.T3_FAKE_CLAUDE_EXIT_CODE;
@@ -87,6 +94,16 @@ function withFakeClaudeEnv<A, E, R>(
8794

8895
yield* Effect.acquireRelease(
8996
Effect.sync(() => {
97+
if (input.proxy === undefined) {
98+
delete process.env.T3_FAKE_CLAUDE_SETTINGS_EXPECTED;
99+
delete process.env.T3_FAKE_CLAUDE_SETTINGS_RECEIPT;
100+
} else {
101+
process.env.T3_FAKE_CLAUDE_SETTINGS_EXPECTED = JSON.stringify({
102+
...input.proxy,
103+
platform,
104+
});
105+
process.env.T3_FAKE_CLAUDE_SETTINGS_RECEIPT = path.join(tempDir, "settings-path.txt");
106+
}
90107
process.env.PATH = `${binDir}${pathDelimiter}${previousPath ?? ""}`;
91108
process.env.T3_FAKE_CLAUDE_OUTPUT = input.output;
92109

@@ -134,6 +151,12 @@ function withFakeClaudeEnv<A, E, R>(
134151
}),
135152
() =>
136153
Effect.sync(() => {
154+
if (previousSettingsExpected === undefined)
155+
delete process.env.T3_FAKE_CLAUDE_SETTINGS_EXPECTED;
156+
else process.env.T3_FAKE_CLAUDE_SETTINGS_EXPECTED = previousSettingsExpected;
157+
if (previousSettingsReceipt === undefined)
158+
delete process.env.T3_FAKE_CLAUDE_SETTINGS_RECEIPT;
159+
else process.env.T3_FAKE_CLAUDE_SETTINGS_RECEIPT = previousSettingsReceipt;
137160
process.env.PATH = previousPath;
138161

139162
if (previousOutput === undefined) {
@@ -193,7 +216,7 @@ function withFakeClaudeEnv<A, E, R>(
193216
Effect.succeed(SYNTHETIC_CLAUDE_MODEL_CATALOG),
194217
input.proxy,
195218
);
196-
return yield* effectFn(textGeneration);
219+
return yield* effectFn(textGeneration, tempDir);
197220
}).pipe(Effect.scoped);
198221
}
199222

@@ -209,15 +232,16 @@ it.layer(layerClaudeTextGenerationTest)("ClaudeTextGeneration", (it) => {
209232
body: "Body",
210233
},
211234
}),
235+
...(proxied ? { argsMustNotContain: "proxy-test-key" } : {}),
212236
argsMustContain: proxied
213-
? '"ANTHROPIC_BASE_URL":"http://proxy.test:8317"'
237+
? "--effort max --settings"
214238
: `--model ${SYNTHETIC_CLAUDE_CAPABLE_MODEL}[expanded] --effort max --settings {"disableAllHooks":true,"fastMode":true}`,
215239
...(proxied
216240
? { proxy: { url: "http://proxy.test:8317", apiKey: "proxy-test-key" } }
217241
: {}),
218242
claudeConfig: { customModels: [SYNTHETIC_CLAUDE_COLLIDING_ALIAS] },
219243
},
220-
(textGeneration) =>
244+
(textGeneration, tempDir) =>
221245
Effect.gen(function* () {
222246
const generated = yield* textGeneration.generatePrContent({
223247
cwd: process.cwd(),
@@ -239,6 +263,14 @@ it.layer(layerClaudeTextGenerationTest)("ClaudeTextGeneration", (it) => {
239263
});
240264

241265
expect(generated.title).toBe("Improve orchestration flow");
266+
if (proxied) {
267+
const fs = yield* FileSystem.FileSystem;
268+
const path = yield* Path.Path;
269+
const settingsPath = yield* fs.readFileString(
270+
path.join(tempDir, "settings-path.txt"),
271+
);
272+
expect(yield* fs.exists(settingsPath)).toBe(false);
273+
}
242274
}),
243275
),
244276
);

‎apps/server/src/textGeneration/ClaudeTextGeneration.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as Effect from "effect/Effect";
22
import * as FileSystem from "effect/FileSystem";
33
import * as Option from "effect/Option";
4+
import * as Path from "effect/Path";
45
import * as Schema from "effect/Schema";
56
import * as Stream from "effect/Stream";
67
import { ChildProcess, ChildProcessSpawner } from "effect/process";
@@ -87,6 +88,7 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
8788
) {
8889
const commandSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
8990
const fileSystem = yield* FileSystem.FileSystem;
91+
const path = yield* Path.Path;
9092
const claudeEnvironment = yield* makeClaudeEnvironment(claudeSettings, environment);
9193
const scopedModelCatalog = modelCatalog.pipe(
9294
Effect.map((catalog) => scopeClaudeModelCatalog(catalog, claudeSettings.customModels)),
@@ -196,6 +198,27 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
196198
),
197199
)
198200
: cwd;
201+
const settingsArgument =
202+
proxy === undefined
203+
? settingsJson
204+
: yield* Effect.gen(function* () {
205+
const directory = yield* fileSystem.makeTempDirectoryScoped({
206+
prefix: "t3code-claude-settings-",
207+
});
208+
yield* fileSystem.chmod(directory, 0o700);
209+
const settingsPath = path.join(directory, "settings.json");
210+
yield* fileSystem.writeFileString(settingsPath, settingsJson, { mode: 0o600 });
211+
return settingsPath;
212+
}).pipe(
213+
Effect.mapError((cause) =>
214+
normalizeCliError(
215+
"claude",
216+
operation,
217+
cause,
218+
"Failed to prepare Claude CLI settings",
219+
),
220+
),
221+
);
199222
const managedMcpConfig = yield* hasClaudeManagedMcpConfig();
200223
const spawnCommand = yield* resolveSpawnCommand(
201224
claudeSettings.binaryPath || "claude",
@@ -209,7 +232,7 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
209232
resolveClaudeCatalogApiModelId(catalog, resolvedModelSelection),
210233
...(cliEffort ? ["--effort", cliEffort] : []),
211234
"--settings",
212-
settingsJson,
235+
settingsArgument,
213236
...(investigate
214237
? [
215238
"--tools",

‎apps/server/src/textGeneration/testing/ClaudeTextGeneration.fixture.mjs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as NodeFS from "node:fs";
2+
import * as NodePath from "node:path";
23

34
const argv = process.argv.slice(2);
45
const args = argv.join(" ");
@@ -26,7 +27,30 @@ if (!argv.includes("--strict-mcp-config")) {
2627
fail("text generation must not load configured MCP servers", 9);
2728
}
2829
const settingsIndex = argv.indexOf("--settings");
29-
if (settingsIndex === -1 || JSON.parse(argv[settingsIndex + 1]).disableAllHooks !== true) {
30+
const settingsArgument = argv[settingsIndex + 1];
31+
const expectedProxy = process.env.T3_FAKE_CLAUDE_SETTINGS_EXPECTED;
32+
if (expectedProxy) {
33+
const { url, apiKey, platform } = JSON.parse(expectedProxy);
34+
if (args.includes(apiKey)) fail("proxy settings exposed in argv", 13);
35+
const settings = JSON.parse(NodeFS.readFileSync(settingsArgument, "utf8"));
36+
if (settings.env?.ANTHROPIC_BASE_URL !== url || settings.env?.ANTHROPIC_AUTH_TOKEN !== apiKey) {
37+
fail("settings file missing proxy route", 14);
38+
}
39+
if (
40+
platform !== "win32" &&
41+
((NodeFS.statSync(settingsArgument).mode & 0o777) !== 0o600 ||
42+
(NodeFS.statSync(NodePath.dirname(settingsArgument)).mode & 0o777) !== 0o700)
43+
) {
44+
fail("settings file permissions expose credentials", 15);
45+
}
46+
NodeFS.writeFileSync(process.env.T3_FAKE_CLAUDE_SETTINGS_RECEIPT, settingsArgument);
47+
}
48+
const settings = JSON.parse(
49+
settingsArgument.startsWith("{")
50+
? settingsArgument
51+
: NodeFS.readFileSync(settingsArgument, "utf8"),
52+
);
53+
if (settingsIndex === -1 || settings.disableAllHooks !== true) {
3054
fail("text generation must disable hooks", 10);
3155
}
3256
const cwdMustNotBe = process.env.T3_FAKE_CLAUDE_CWD_MUST_NOT_BE;

0 commit comments

Comments
 (0)