Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(security): post-upgrade reads an install's GBRAIN_HOME only from entries its receipt owns (review pass 2, #8426)

garrytana83100aa1a69
diff --git a/src/core/setup/hook-launcher-refresh.ts b/src/core/setup/hook-launcher-refresh.ts
index 4a9e55a815..d56e5e4bac 100644
--- a/src/core/setup/hook-launcher-refresh.ts
+++ b/src/core/setup/hook-launcher-refresh.ts
@@ -25,6 +25,7 @@ import { applyClaudeSetup, claudeSetupPaths, inspectClaudeSetup, readSetupReceip
import { codexSetupPaths, readCodexSetupReceipt } from './codex.ts';
import { hookLauncherInCommand, hookLauncherPath, hookLauncherState } from './hook-launcher.ts';
import { localSetupRecords } from './local-receipts.ts';
+import { ownedEntryFingerprint } from '../bootstrap/adopted-connections.ts';
import { launcherLaneFindings, setupRerunArgv } from './launcher-lanes.ts';
import { hasTurnRuleHook, isTurnRuleEntry, syncTurnRuleHook, TURN_RULE_LANE_TOKEN } from './turn-hook.ts';
@@ -72,9 +73,9 @@ async function refreshSetup(log: (line: string) => void): Promise<void> {
const paths = claudeSetupPaths({ name: receipt.name, scope: receipt.scope, projectDir: receipt.project_dir });
const owned = new Set([...Object.values(receipt.hooks.owned), ...Object.values(receipt.hooks.pending)]);
const ours = claudeHookEntries(paths.settingsPath).filter((e) => e[GBRAIN_HOOK_MARKER_KEY] === GBRAIN_SETUP_MARKER_VALUE || owned.has(hookEntryHash(e) ?? '')).map((e) => e.command as string);
- // Another install's setup hooks share this settings file, so the home comes from this install's own MCP entry or owned hooks.
- const homeEnv = claudeMcpHome(paths.configPath, receipt.name, receipt.project_dir)
- ?? claudeHookEntries(paths.settingsPath).filter((e) => owned.has(hookEntryHash(e) ?? '')).map((e) => parseClaudeHookCommand(e.command as string)?.env.GBRAIN_HOME).find((h) => h !== undefined);
+ // Another install's hooks or MCP entry can share these files, so the home comes only from entries this receipt owns.
+ const homeEnv = claudeHookEntries(paths.settingsPath).filter((e) => owned.has(hookEntryHash(e) ?? '')).map((e) => parseClaudeHookCommand(e.command as string)?.env.GBRAIN_HOME).find((h) => h !== undefined)
+ ?? (receipt.mcp_plugin ? undefined : claudeMcpHome(paths.configPath, receipt.name, receipt.project_dir, [receipt.entry_hash, receipt.pending_entry_hash]));
const plan: ClaudeSetupPlan = { name: receipt.name, scope: receipt.scope, ...(receipt.project_dir ? { projectDir: receipt.project_dir } : {}), launcher: receipt.launcher, brainHome: receipt.gbrain_home, ...(homeEnv ? { gbrainHomeEnv: homeEnv } : {}),
sourceId: receipt.source_id, surface: receipt.surface, advertise: receipt.advertise ?? null, events: Object.keys({ ...receipt.hooks.owned, ...receipt.hooks.pending }) as ClaudeHookEvent[] };
// An MCP entry still exactly what setup last wrote predates the launcher (#8426): setup's own rewrite moves it.
@@ -116,10 +117,12 @@ export async function refreshHookLaunchers(log: (line: string) => void = console
}
/** GBRAIN_HOME from setup's own MCP entry, for an install with no hooks to read it from. */
-function claudeMcpHome(configPath: string, name: string, projectDir?: string): string | undefined {
+function claudeMcpHome(configPath: string, name: string, projectDir: string | undefined, ownedHashes: Array<string | null | undefined>): string | undefined {
try {
const cfg = JSON.parse(readFileSync(configPath, 'utf8')) as { mcpServers?: Record<string, { env?: Record<string, unknown> }>; projects?: Record<string, { mcpServers?: Record<string, { env?: Record<string, unknown> }> }> };
- const home = (projectDir ? cfg.projects?.[projectDir]?.mcpServers : cfg.mcpServers)?.[name]?.env?.GBRAIN_HOME;
+ const entry = (projectDir ? cfg.projects?.[projectDir]?.mcpServers : cfg.mcpServers)?.[name];
+ if (!entry || !ownedHashes.includes(ownedEntryFingerprint(entry as Record<string, unknown>))) return undefined;
+ const home = entry.env?.GBRAIN_HOME;
return typeof home === 'string' ? home : undefined;
} catch { return undefined; }
}
diff --git a/test/setup.test.ts b/test/setup.test.ts
index 680d25377f..6b1b415d7e 100644
--- a/test/setup.test.ts
+++ b/test/setup.test.ts
@@ -1136,6 +1136,23 @@ describe('hooks written before the hook launcher (#8424): the next setup and pos
});
});
+ test('claude-code (#8426): post-upgrade never takes a plugin-lane install\'s GBRAIN_HOME from an MCP entry setup does not own', async () => {
+ const b = box();
+ const brainA = join(b.root, 'brain-a');
+ const brainC = join(b.root, 'brain-c');
+ mkdirSync(join(b.home, '.claude'), { recursive: true });
+ writeFileSync(settings(b), JSON.stringify({ enabledPlugins: { 'gbrain@gbrain': true } }));
+ writeFileSync(claudeJson(b), JSON.stringify({ mcpServers: { gbrain: { type: 'stdio', command: b.bin, args: ['serve'], env: { GBRAIN_HOME: brainC } } } }));
+ expect((await setup(b, ['claude-code', '--json'], { GBRAIN_HOME: brainA })).exitCode).toBe(0);
+ const a = { ...b, gbrainHome: brainA };
+ downgradeClaude(a);
+ await withEnv({ ...env(b), GBRAIN_HOME: brainA }, async () => { await refreshHookLaunchers(() => {}); });
+ const hooks = JSON.stringify(readJson(settings(b)).hooks);
+ expect(hooks).not.toContain(brainC);
+ expect(hooks).toContain(`GBRAIN_HOME=${brainA}`);
+ expect(hooks).toContain(launcher(a));
+ });
+
test('claude-code MCP (#8426): doctor\'s rerun keeps a recorded --no-hooks, so moving the entry turns no hooks on', async () => {
const b = box();
expect((await setup(b, ['claude-code', '--no-hooks', '--json'])).exitCode).toBe(0);