Skip to content

Commit f7f6baa

Browse files
committed
fix(log-guard): allow Disable on an unrecognized schema; repair a stale lock dir mode
Two review findings. A Codex schema upgrade could strand an installed trigger. Both the caller gate and mutateOwnedTrigger refused every mutation on an unrecognized schema, so Protect was correctly blocked and Disable was blocked too - leaving an active OpenCodex trigger with no in-product way to remove it. Installing into an unknown schema stays refused; removing our own trigger is now always allowed. mkdirSync applies its mode only when it creates the directory, so a lock directory left by an earlier build kept its old permissions and every mutation failed unsafe_path permanently. When we own the directory we now chmod it to 0700 and re-read to confirm, failing closed if the filesystem ignores it.
1 parent 2330844 commit f7f6baa

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

‎src/codex/log-guard/lock.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,26 @@ function resolveLogGuardLockDatabase(
6969
}
7070
if (process.platform !== "win32") {
7171
const uid = process.getuid?.();
72-
if (uid === undefined || dirStat.uid !== uid || (dirStat.mode & 0o777) !== 0o700) {
72+
// `mkdirSync` applies `mode` only when it CREATES the directory, so a
73+
// directory left by an earlier build (or a permissive umask on a older
74+
// version) keeps its old permissions forever and every Log Guard mutation
75+
// fails `unsafe_path` with no in-product way out. Tighten it ourselves when
76+
// we own it, and only refuse if that cannot be achieved.
77+
if (uid === undefined || dirStat.uid !== uid) {
7378
throw new CodexUserIdentityRefusal("Codex Log Guard lock directory is not private to the effective user.");
7479
}
80+
if ((dirStat.mode & 0o777) !== 0o700) {
81+
try {
82+
chmodSync(locksDir, 0o700);
83+
} catch {
84+
throw new CodexUserIdentityRefusal("Codex Log Guard lock directory is not private to the effective user.");
85+
}
86+
// Re-read rather than trusting the chmod: a filesystem that ignores mode
87+
// bits must still fail closed.
88+
if ((lstatSync(locksDir).mode & 0o777) !== 0o700) {
89+
throw new CodexUserIdentityRefusal("Codex Log Guard lock directory is not private to the effective user.");
90+
}
91+
}
7592
}
7693

7794
return join(locksDir, `${codexLogGuardLockDigest(canonicalCodexHome, canonicalLogsDbPath)}.sqlite`);

‎src/codex/log-guard/protection.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,14 @@ function mutateOwnedTrigger(
303303
db.exec("PRAGMA busy_timeout = 0; BEGIN IMMEDIATE");
304304
transactionOpen = true;
305305
if (!exactCurrentSchema(db)) {
306-
db.exec("ROLLBACK");
307-
transactionOpen = false;
308-
return { ok: false, error: "unsupported_schema" };
306+
// Same reasoning as the caller's gate: installing into an unrecognized
307+
// schema is refused, but removing a trigger we installed ourselves stays
308+
// available so a schema upgrade cannot strand it.
309+
if (mode !== "off") {
310+
db.exec("ROLLBACK");
311+
transactionOpen = false;
312+
return { ok: false, error: "unsupported_schema" };
313+
}
309314
}
310315

311316
const rows = queryReservedTriggers(db);
@@ -396,7 +401,14 @@ function performMutation(
396401
const codexHome = deps.codexHome ?? getCodexHome();
397402
const inspection = inspectCodexLogs({ codexHome });
398403
const databasePath = resolveCodexLogsDbPath({ codexHome });
399-
if (inspection.capabilities.protection.state !== "supported") {
404+
// Removal must not be gated on the schema still being recognized. A Codex
405+
// upgrade that changes the logs schema would otherwise strand an installed
406+
// trigger: Protect is refused (correctly), but so is Disable, leaving the
407+
// user with an active OpenCodex trigger and no in-product way to remove it.
408+
// Installing into an unknown schema stays refused; taking our own trigger
409+
// back out is always allowed.
410+
const removingProtection = typeof requestedMode !== "function" && requestedMode === "off";
411+
if (!removingProtection && inspection.capabilities.protection.state !== "supported") {
400412
return { ok: false, error: "unsupported_schema" };
401413
}
402414
if (!databasePathIsSafe(databasePath)) return { ok: false, error: "unsafe_path" };

‎tests/codex-log-guard-protection.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,24 @@ describe("Codex Log Guard protection", () => {
307307
expect(protectCodexLogs("compat", deps)).toEqual({ ok: false, error: "config_write_failed" });
308308
expect(triggers(databasePath)).toEqual([]);
309309
});
310+
test("Disable still works after the Codex schema moves out from under us", async () => {
311+
// Protect is correctly refused on an unrecognized schema, but gating Disable
312+
// the same way stranded an installed trigger: the user kept an active
313+
// OpenCodex trigger with no in-product way to remove it.
314+
const { codexHome, databasePath } = fixture();
315+
const deps = testDeps(codexHome);
316+
expect(protectCodexLogs("compat", deps).ok).toBe(true);
317+
expect(triggers(databasePath).map(row => row.name)).toEqual(["opencodex_log_guard_compat_v1"]);
318+
319+
// Simulate a Codex upgrade adding a column, so the exact-schema check fails.
320+
const db = new Database(databasePath);
321+
db.exec("ALTER TABLE logs ADD COLUMN future_field TEXT");
322+
db.close();
323+
324+
// Installing into the unknown schema stays refused...
325+
expect(protectCodexLogs("quiet", deps)).toEqual({ ok: false, error: "unsupported_schema" });
326+
// ...but taking our own trigger back out is always available.
327+
expect(unprotectCodexLogs(deps).ok).toBe(true);
328+
expect(triggers(databasePath)).toEqual([]);
329+
});
310330
});

0 commit comments

Comments
 (0)