Skip to content

Commit 1daffe8

Browse files
committed
fix(claude): probe uncertain command loader sources
Made with GPT-6.1 Sol via Codex in T3 Code.
1 parent 271a3cf commit 1daffe8

2 files changed

Lines changed: 125 additions & 11 deletions

File tree

‎apps/server/src/provider/ClaudeCapabilitiesProbe.test.ts‎

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// @effect-diagnostics nodeBuiltinImport:off - cleanup uses Node's retrying rm, which the FileSystem service does not expose.
2+
import * as NodeOS from "node:os";
3+
24
import * as NodeServices from "@effect/platform-node/NodeServices";
35
import { ClaudeSettings, type ServerProvider } from "@t3tools/contracts";
46
import { vi } from "vite-plus/test";
@@ -110,7 +112,6 @@ it.effect("starts the CLI again only when a source of its command list changes",
110112
path.join(config, "workflows", "a.js"),
111113
path.join(config, "synced", "account", "a", "SKILL.md"),
112114
path.join(config, "settings.json"),
113-
path.join(config, "remote-settings.json"),
114115
path.join(config, "plugins", "installed_plugins.json"),
115116
path.join(config, "plugins", "synced", "account", ".marketplace-a.json"),
116117
path.join(config, "plugins", "cache", "market", "plugin", "1.0.0", "commands", "a.md"),
@@ -199,13 +200,68 @@ it.effect("always starts the CLI when its sources cannot be vouched for", () =>
199200
});
200201

201202
yield* probesTwice(yield* makeCommands({ CLAUDE_CODE_PLUGIN_DIRS: "/elsewhere" }));
203+
for (const environment of [
204+
{ CLAUDE_CODE_PLUGIN_USE_ZIP_CACHE: "1" },
205+
{ CLAUDE_CODE_REMOTE_SETTINGS_PATH: "/elsewhere" },
206+
{ GIT_DIR: "/elsewhere" },
207+
{ GIT_COMMON_DIR: "/elsewhere" },
208+
{ GIT_WORK_TREE: "/elsewhere" },
209+
{ GIT_CEILING_DIRECTORIES: "/elsewhere" },
210+
]) {
211+
yield* probesTwice(yield* makeCommands(environment));
212+
}
213+
yield* probesTwice(yield* makeCommands()).pipe(
214+
Effect.provideService(HostProcess.Platform, "win32"),
215+
);
216+
yield* probesTwice(yield* makeCommands({ WSL_INTEROP: "/run/WSL/1_interop" })).pipe(
217+
Effect.provideService(HostProcess.Platform, "linux"),
218+
);
202219

220+
const fileSystem = yield* FileSystem.FileSystem;
203221
const commands = yield* makeCommands();
204222
const settings = path.join(cwd, ".claude", "settings.json");
205223
yield* write(settings, '{"env":{"CLAUDE_CODE_PLUGIN_SEED_DIR":"/elsewhere"}}');
206224
yield* probesTwice(commands);
207225
yield* write(settings, "{}");
208226

227+
yield* write(settings, '{"env":{"\\u0043LAUDE_CODE_PLUGIN_CACHE_DIR":"/elsewhere"}}');
228+
yield* probesTwice(commands);
229+
yield* write(settings, "{}");
230+
231+
for (const plist of [
232+
`/Library/Managed Preferences/${NodeOS.userInfo().username}/com.anthropic.claudecode.plist`,
233+
"/Library/Managed Preferences/com.anthropic.claudecode.plist",
234+
]) {
235+
const fixturePlist = path.join(root, "managed.plist");
236+
yield* write(fixturePlist);
237+
yield* probesTwice(commands).pipe(
238+
Effect.provideService(HostProcess.Platform, "darwin"),
239+
Effect.provideService(FileSystem.FileSystem, {
240+
...fileSystem,
241+
stat: (entry) => fileSystem.stat(entry === plist ? fixturePlist : entry),
242+
}),
243+
);
244+
}
245+
246+
for (const contents of [
247+
'{"env":{"CLAUDE_CONFIG_DIR":"/elsewhere"}}',
248+
'{"env":{"HOME":"/elsewhere"}}',
249+
'{"env":{"USERPROFILE":"/elsewhere"}}',
250+
'{"permissions":{"additionalDirectories":["/elsewhere"]}}',
251+
'{"policyHelpers":{"macos":{"command":"helper"}}}',
252+
]) {
253+
yield* write(settings, contents);
254+
yield* probesTwice(commands);
255+
}
256+
yield* write(settings, "{}");
257+
258+
const remoteSettings = path.join(config, "remote-settings.json");
259+
yield* write(remoteSettings, '{"env":{"CLAUDE_CODE_PLUGIN_CACHE_DIR":"/elsewhere"}}');
260+
yield* probesTwice(commands);
261+
yield* write(remoteSettings, "{}");
262+
yield* probesTwice(commands);
263+
yield* fileSystem.remove(remoteSettings);
264+
209265
const redirectedPlugins = path.join(root, "redirected-plugins");
210266
const redirectedCommand = path.join(
211267
redirectedPlugins,
@@ -229,8 +285,10 @@ it.effect("always starts the CLI when its sources cannot be vouched for", () =>
229285
yield* write(settingsPath, "{}");
230286
}
231287

288+
const managedPath = (entry: string) =>
289+
entry.replace(/^\/etc\/claude-code(?=\/|$)/, path.join(root, "ClaudeCode"));
232290
yield* Effect.gen(function* () {
233-
const managedCommands = yield* makeCommands({ PROGRAMDATA: root });
291+
const managedCommands = yield* makeCommands();
234292
const dropIn = path.join(root, "ClaudeCode", "managed-settings.d", "plugins.json");
235293
yield* write(dropIn, "{}");
236294
const beforeManaged = probes();
@@ -248,9 +306,30 @@ it.effect("always starts the CLI when its sources cannot be vouched for", () =>
248306
yield* managedCommands.get(machine, cwd);
249307
assert.strictEqual(probes(), beforeManagedEdit + 1);
250308
yield* write(dropIn, "{}");
251-
}).pipe(Effect.provideService(HostProcess.Platform, "win32"));
309+
}).pipe(
310+
Effect.provideService(HostProcess.Platform, "linux"),
311+
Effect.provideService(FileSystem.FileSystem, {
312+
...fileSystem,
313+
stat: (entry) => fileSystem.stat(managedPath(entry)),
314+
readDirectory: (entry) => fileSystem.readDirectory(managedPath(entry)),
315+
readFileString: (entry) => fileSystem.readFileString(managedPath(entry)),
316+
}),
317+
);
252318

253319
const installed = path.join(config, "plugins", "installed_plugins.json");
320+
for (const installPath of [
321+
path.join(config, "plugins", "marketplaces", "a"),
322+
path.join(config, "plugins", "store", "a"),
323+
path.join(config, "plugins"),
324+
]) {
325+
yield* write(
326+
installed,
327+
JSON.stringify({
328+
plugins: { "a@b": [{ installPath }] },
329+
}),
330+
);
331+
yield* probesTwice(commands);
332+
}
254333
yield* write(
255334
installed,
256335
JSON.stringify({ plugins: { "a@b": [{ installPath: path.join(root, "elsewhere") }] } }),

‎apps/server/src/provider/Drivers/ClaudeSkills.ts‎

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,13 @@ const CLAUDE_COMMAND_SOURCE_REDIRECTS = [
625625
"CLAUDE_CODE_MANAGED_SETTINGS_PATH",
626626
"CLAUDE_CODE_PLUGIN_DIRS",
627627
"CLAUDE_CODE_PLUGIN_SEED_DIR",
628+
"CLAUDE_CODE_PLUGIN_USE_ZIP_CACHE",
629+
"CLAUDE_CODE_REMOTE_SETTINGS_PATH",
628630
"CLAUDE_CODE_USE_COWORK_PLUGINS",
631+
"GIT_DIR",
632+
"GIT_COMMON_DIR",
633+
"GIT_WORK_TREE",
634+
"GIT_CEILING_DIRECTORIES",
629635
];
630636
// The CLI rewrites these on its own: `store` on every start, the rest on background syncs.
631637
// Marketplace clones can be huge, and only their manifests, added separately, feed the list.
@@ -664,8 +670,8 @@ const InstalledPluginPaths = Schema.fromJsonString(
664670
);
665671

666672
/**
667-
* A digest of every local file the CLI builds its `initialize` command list from, checked
668-
* against 2.1.316 by changing each source and comparing real probe output. `undefined` means
673+
* A digest of local command sources, audited against the 2.1.316 loader and isolated probes.
674+
* `undefined` means
669675
* the sources cannot be vouched for, so the caller must probe. Account state and remote flags
670676
* live outside these files, so callers also pass the health check's view as `machineState`.
671677
*/
@@ -682,6 +688,16 @@ export const fingerprintClaudeCommandSources = Effect.fn("fingerprintClaudeComma
682688
const fileSystem = yield* FileSystem.FileSystem;
683689
const path = yield* Path.Path;
684690
const platform = yield* HostProcess.Platform;
691+
// Windows and WSL also load managed settings from registry keys outside this walk.
692+
if (
693+
platform === "win32" ||
694+
(platform === "linux" &&
695+
(environment.WSL_DISTRO_NAME !== undefined ||
696+
environment.WSL_INTEROP !== undefined ||
697+
/microsoft/i.test(NodeOS.release())))
698+
) {
699+
return undefined;
700+
}
685701
const home = yield* HostProcess.HomeDirectory;
686702
const configDirPath = yield* resolveClaudeConfigDirPath(config, environment, cwd);
687703
const pluginsPath = yield* resolveClaudePluginsPath(configDirPath, cwd, environment);
@@ -721,6 +737,10 @@ export const fingerprintClaudeCommandSources = Effect.fn("fingerprintClaudeComma
721737
.readFileString(settingsPath)
722738
.pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.undefined));
723739
if (
740+
/\\u[0-9a-f]{4}/i.test(contents ?? "") ||
741+
["CLAUDE_CONFIG_DIR", "HOME", "USERPROFILE", "additionalDirectories", "policyHelpers"].some(
742+
(name) => contents?.includes(name),
743+
) ||
724744
contents?.includes("CLAUDE_CODE_PLUGIN_CACHE_DIR") ||
725745
CLAUDE_COMMAND_SOURCE_REDIRECTS.some((name) => contents?.includes(name))
726746
) {
@@ -761,7 +781,6 @@ export const fingerprintClaudeCommandSources = Effect.fn("fingerprintClaudeComma
761781
for (const subdirectory of [...CLAUDE_COMMAND_SUBDIRECTORIES, "synced"]) {
762782
yield* addStat(path.join(configDirPath, subdirectory));
763783
}
764-
yield* addStat(path.join(configDirPath, "remote-settings.json"));
765784
yield* addStat(path.join(configDirPath, "policy-limits.json"));
766785

767786
for (const directory of claudeManagedDirectories(path, platform, environment)) {
@@ -780,10 +799,20 @@ export const fingerprintClaudeCommandSources = Effect.fn("fingerprintClaudeComma
780799
yield* addStat(path.join(directory, ".claude", subdirectory));
781800
}
782801
}
783-
if (platform === "darwin") {
784-
const { username } = NodeOS.userInfo();
785-
yield* addStat(`/Library/Managed Preferences/${username}/com.anthropic.claudecode.plist`);
786-
yield* addStat("/Library/Managed Preferences/com.anthropic.claudecode.plist");
802+
// Remote policy also depends on signatures and consent, not just the settings bytes.
803+
for (const settingsPath of [
804+
path.join(configDirPath, "remote-settings.json"),
805+
...(platform === "darwin"
806+
? [
807+
`/Library/Managed Preferences/${NodeOS.userInfo().username}/com.anthropic.claudecode.plist`,
808+
"/Library/Managed Preferences/com.anthropic.claudecode.plist",
809+
]
810+
: []),
811+
]) {
812+
const info = yield* fileSystem
813+
.stat(settingsPath)
814+
.pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.undefined));
815+
if (info !== undefined) return yield* Effect.fail("redirected" as const);
787816
}
788817

789818
yield* addStat(
@@ -797,7 +826,13 @@ export const fingerprintClaudeCommandSources = Effect.fn("fingerprintClaudeComma
797826
for (const { installPath } of installs) {
798827
if (
799828
installPath !== undefined &&
800-
!isInsideDirectory(path, pluginsPath, path.resolve(workspace, installPath))
829+
(!isInsideDirectory(path, pluginsPath, path.resolve(workspace, installPath)) ||
830+
path.resolve(workspace, installPath) === pluginsPath ||
831+
CLAUDE_PLUGIN_ROOT_IGNORED.has(
832+
path
833+
.relative(pluginsPath, path.resolve(workspace, installPath))
834+
.split(path.sep)[0] ?? "",
835+
))
801836
) {
802837
return yield* Effect.fail("pluginOutsideCache" as const);
803838
}

0 commit comments

Comments
 (0)