Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

fix(hosts): guard non-array PostToolUse; re-heal shim exec bit on reinstall

Shrish Dwivedi9d10218ee03f
diff --git a/src/hosts/codex-hooks.ts b/src/hosts/codex-hooks.ts
index 7befd7aa..0bfdd4c1 100644
--- a/src/hosts/codex-hooks.ts
+++ b/src/hosts/codex-hooks.ts
@@ -19,7 +19,10 @@ function dirExists(p: string): boolean {
function writeOwned(id: string, path: string, content: string, mode?: number): HookWrite {
const existed = existsSync(path);
- if (existed && readFileSync(path, 'utf8') === content) return { id, path, action: 'unchanged' };
+ if (existed && readFileSync(path, 'utf8') === content) {
+ if (mode !== undefined && (statSync(path).mode & 0o777) !== mode) chmodSync(path, mode);
+ return { id, path, action: 'unchanged' };
+ }
mkdirSync(dirname(path), { recursive: true });
writeFileSync(path, content);
if (mode !== undefined) chmodSync(path, mode);
@@ -53,6 +56,9 @@ export function installCodexHooks(home: string): HookWrite[] {
if (typeof hooks !== 'object' || hooks === null || Array.isArray(hooks)) {
return [shimWrite, { id: 'codex-hooks', path: cfgPath, action: 'skipped-unparseable' }];
}
+ if (hooks.PostToolUse !== undefined && !Array.isArray(hooks.PostToolUse)) {
+ return [shimWrite, { id: 'codex-hooks', path: cfgPath, action: 'skipped-unparseable' }];
+ }
const prior: unknown[] = Array.isArray(hooks.PostToolUse) ? hooks.PostToolUse : [];
hooks.PostToolUse = [
...prior.filter((e) => !isGraftEntry(e)),
diff --git a/test/hosts-codex-hooks.test.ts b/test/hosts-codex-hooks.test.ts
index 86621cdc..6a235cd0 100644
--- a/test/hosts-codex-hooks.test.ts
+++ b/test/hosts-codex-hooks.test.ts
@@ -1,6 +1,6 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
-import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, existsSync, statSync } from 'node:fs';
+import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, existsSync, statSync, chmodSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { installCodexHooks } from '../src/hosts/codex-hooks.js';
@@ -54,3 +54,26 @@ test('unparseable hooks.json is never rewritten', () => {
assert.ok(w.some((x) => x.action === 'skipped-unparseable'));
assert.equal(readFileSync(join(home, '.codex', 'hooks.json'), 'utf8'), '{ nope');
});
+
+test('non-array PostToolUse (foreign single object) is never rewritten', () => {
+ const home = fresh();
+ mkdirSync(join(home, '.codex'), { recursive: true });
+ const original = JSON.stringify({
+ hooks: { PostToolUse: { matcher: 'Bash', hooks: [{ type: 'command', command: 'other-tool' }] } },
+ });
+ writeFileSync(join(home, '.codex', 'hooks.json'), original);
+ const w = installCodexHooks(home);
+ assert.ok(w.some((x) => x.action === 'skipped-unparseable'));
+ assert.equal(readFileSync(join(home, '.codex', 'hooks.json'), 'utf8'), original);
+});
+
+test('re-heals shim exec bit when a prior install had its mode stripped', () => {
+ const home = fresh();
+ mkdirSync(join(home, '.codex'), { recursive: true });
+ installCodexHooks(home);
+ const shim = join(home, '.codex', 'hooks', 'graft', 'graft-hooks.cjs');
+ chmodSync(shim, 0o644);
+ assert.ok(!(statSync(shim).mode & 0o111), 'exec bit stripped before re-run');
+ installCodexHooks(home);
+ assert.ok(statSync(shim).mode & 0o111, 'exec bit restored after re-run');
+});