Fix Codex launch-home hook trust repair (#4348)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Brennan Benson
2026-05-31 21:30:11 -07:00
committed by GitHub
co-authored by Orca
parent b0b9e13934
commit e0502a8d8e
2 changed files with 37 additions and 2 deletions
+32 -1
View File
@@ -74,6 +74,10 @@ function hookTrustHeader(key: string): string {
return `[hooks.state."${escapeTomlBasicString(canonicalizeHookTrustKeyForTest(key))}"]`
}
function literalHookTrustHeader(key: string): string {
return `[hooks.state."${escapeTomlBasicString(key)}"]`
}
function canonicalizeHookTrustKeyForTest(key: string): string {
const lastColon = key.lastIndexOf(':')
const secondLast = lastColon === -1 ? -1 : key.lastIndexOf(':', lastColon - 1)
@@ -1050,10 +1054,37 @@ describe('CodexHookService', () => {
const trustConfig = readFileSync(join(launchHome, 'config.toml'), 'utf-8')
const launchHooksTrustPath = join(realpathSync.native(launchHome), 'hooks.json')
expect(trustConfig).toContain(hookTrustHeader(`${launchHooksTrustPath}:session_start:0:0`))
expect(trustConfig).toContain(
literalHookTrustHeader(`${launchHooksTrustPath}:session_start:0:0`)
)
expect(trustConfig).toContain(hookTrustHeader(`${managedHooksPath}:session_start:0:0`))
})
it('mirrors runtime hook trust when launch-home config is stale', () => {
const service = new CodexHookService()
expect(service.install().state).toBe('installed')
const managedCodexHome = join(userDataDir, 'codex-runtime-home', 'home')
const managedHooksPath = join(managedCodexHome, 'hooks.json')
const launchHome = join(userDataDir, 'codex-runtime-home', 'launch', 'host', 'system', 'home')
mkdirSync(launchHome, { recursive: true })
if (process.platform === 'win32') {
writeFileSync(join(launchHome, 'hooks.json'), readFileSync(managedHooksPath, 'utf-8'))
} else {
symlinkSync(managedHooksPath, join(launchHome, 'hooks.json'))
}
writeFileSync(join(launchHome, 'config.toml'), 'model = "stale-launch-config"\n', 'utf-8')
trustCodexLaunchHomeHooks(launchHome)
const trustConfig = readFileSync(join(launchHome, 'config.toml'), 'utf-8')
const launchHooksTrustPath = join(realpathSync.native(launchHome), 'hooks.json')
expect(trustConfig).toContain('model = "stale-launch-config"')
expect(trustConfig).toContain(
literalHookTrustHeader(`${launchHooksTrustPath}:session_start:0:0`)
)
})
it('repairs duplicate managed SessionStart trust tables on restart install', () => {
const systemCodexHome = join(tmpHome, '.codex')
mkdirSync(systemCodexHome, { recursive: true })
+5 -1
View File
@@ -518,6 +518,7 @@ function getLaunchHomeHookTrustSourcePath(launchHomePath: string): string {
export function trustCodexLaunchHomeHooks(launchHomePath: string): void {
const runtimeConfigPath = getConfigPath()
const runtimeTomlPath = getCodexConfigTomlPath()
const launchHooksPath = join(launchHomePath, 'hooks.json')
const launchTrustSourcePath = getLaunchHomeHookTrustSourcePath(launchHomePath)
const tomlPath = getLaunchHomeCodexConfigTomlPath(launchHomePath)
@@ -526,7 +527,10 @@ export function trustCodexLaunchHomeHooks(launchHomePath: string): void {
return
}
const trustEntries = readHookTrustEntries(tomlPath)
// Why: account launch homes isolate auth.json, but hook trust is shared
// runtime config. A launch config.toml can be a stale mutable copy, while
// Codex keys trust by the selected launch home's hooks.json path.
const trustEntries = readHookTrustEntries(runtimeTomlPath)
const launchTrustBlocks: { key: string; trustedHash: string }[] = []
const launchTrustStates: { key: string; enabled: boolean }[] = []
for (const [eventName, definitions] of Object.entries(config.hooks)) {