Skip to content

Commit c5af938

Browse files
axisrowclaude
andcommitted
fix(sandbox): fail closed when a read-only pin is not honored on resume (cycle-review critical)
Codex adversarial review of #33 flagged — and a real-codex smoke confirmed — that Codex 0.144.6 keeps a loaded thread's sandbox and ignores thread/resume overrides: `task --read-only --resume-last` on a write-capable thread ran with workspace-write access, violating the --read-only contract. Verified empirically (codex-cli 0.144.6): write-thread -> resume with --read-only returned resolved.sandbox = workspaceWrite. The code never compared the resolved sandbox to the explicit pin. Fix: assertExplicitSandboxHonored() in runAppServerTurn compares an explicit read-only pin to the app-server's resolved sandbox and throws on mismatch — fail closed instead of running write-capable under a read-only flag. Only the read-only pin is enforced (a write request on a read-only resolved sandbox is a downgrade, not a safety leak). TDD: a resume-ignores-sandbox fake-codex behavior reproduces the leak (RED), the assertion turns it green. Also confirms the port's cubicj claim: a plain task with sandbox:null resolves to read-only from config (smoke: resolved readOnly), so stock behavior is unchanged. Co-Authored-By: Claude <[email protected]>
1 parent d444ef1 commit c5af938

3 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎plugins/codex/scripts/lib/codex.mjs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,37 @@ function buildTurnInput(prompt) {
111111
return [{ type: "text", text: prompt, text_elements: [] }];
112112
}
113113

114+
// Map a requested sandbox shorthand ("read-only"/"workspace-write") to the
115+
// app-server's sandbox-type vocabulary ("readOnly"/"workspaceWrite").
116+
function requestedSandboxType(requested) {
117+
if (requested === "read-only") {
118+
return "readOnly";
119+
}
120+
if (requested === "workspace-write") {
121+
return "workspaceWrite";
122+
}
123+
return null;
124+
}
125+
126+
// Fail closed when an explicit read-only pin was requested but the app-server
127+
// resolved a write-capable sandbox. Codex 0.144.x keeps a loaded thread's
128+
// sandbox and ignores thread/resume overrides, so without this check
129+
// --read-only --resume-last could silently run with write access. (The write
130+
// pin is not enforced here: a write request on a read-only resolved sandbox is
131+
// a downgrade, not a safety leak.)
132+
function assertExplicitSandboxHonored(requested, resolved) {
133+
if (requestedSandboxType(requested) !== "readOnly") {
134+
return;
135+
}
136+
const resolvedType = resolved?.type ?? null;
137+
if (resolvedType === "readOnly") {
138+
return;
139+
}
140+
throw new Error(
141+
`Requested a read-only sandbox, but the app-server resolved ${resolvedType ?? "an unknown sandbox"} — refusing to run with a sandbox that does not match the explicit read-only pin. Start a fresh thread or drop --read-only.`
142+
);
143+
}
144+
114145
function shorten(text, limit = 72) {
115146
const normalized = String(text ?? "").trim().replace(/\s+/g, " ");
116147
if (!normalized) {
@@ -1229,6 +1260,11 @@ export async function runAppServerTurn(cwd, options = {}) {
12291260
reasoningEffort: response.reasoningEffort,
12301261
sandbox: response.sandbox
12311262
};
1263+
// Fail closed when an explicit sandbox pin is not honored. Codex app-server
1264+
// (0.144.x) keeps a loaded thread's sandbox and ignores thread/resume
1265+
// overrides, so --read-only --resume-last on a write-capable thread would
1266+
// otherwise run with write access — violating the --read-only contract.
1267+
assertExplicitSandboxHonored(options.sandbox, resolved.sandbox);
12321268
await validateReasoningSelection(client, {
12331269
model: options.model ?? threadSelection.model,
12341270
effort: options.effort ?? threadSelection.reasoningEffort,

‎tests/fake-codex-fixture.mjs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,13 @@ rl.on("line", (line) => {
420420
sandbox: message.params.sandbox ?? null
421421
};
422422
saveState(state);
423-
send({ id: message.id, result: { thread: buildThread(thread), model: selectedModel, modelProvider: "openai", serviceTier: null, cwd: thread.cwd, approvalPolicy: "never", sandbox: { type: "readOnly", access: { type: "fullAccess" }, networkAccess: false }, reasoningEffort: selectedEffort } });
423+
// resume-ignores-sandbox: simulate a real Codex app-server that keeps a
424+
// loaded thread's sandbox and ignores the thread/resume override — so an
425+
// explicit --read-only pin on a write-capable thread is silently lost.
426+
const resumeSandbox = BEHAVIOR === "resume-ignores-sandbox"
427+
? { type: "workspaceWrite", writableRoots: [], networkAccess: false }
428+
: { type: "readOnly", access: { type: "fullAccess" }, networkAccess: false };
429+
send({ id: message.id, result: { thread: buildThread(thread), model: selectedModel, modelProvider: "openai", serviceTier: null, cwd: thread.cwd, approvalPolicy: "never", sandbox: resumeSandbox, reasoningEffort: selectedEffort } });
424430
break;
425431
}
426432

‎tests/runtime.test.mjs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,30 @@ test("task --read-only pins resumed app-server threads", () => {
14281428
assert.equal(fakeState.lastThreadResume.sandbox, "read-only");
14291429
});
14301430

1431+
test("task --read-only fails closed if the app-server resumes a write-capable sandbox", () => {
1432+
const repo = makeTempDir();
1433+
const binDir = makeTempDir();
1434+
installFakeCodex(binDir, "resume-ignores-sandbox");
1435+
initGitRepo(repo);
1436+
fs.writeFileSync(path.join(repo, "README.md"), "hello\n");
1437+
run("git", ["add", "README.md"], { cwd: repo });
1438+
run("git", ["commit", "-m", "init"], { cwd: repo });
1439+
1440+
const firstRun = run("node", [SCRIPT, "task", "initial task"], {
1441+
cwd: repo,
1442+
env: buildEnv(binDir)
1443+
});
1444+
assert.equal(firstRun.status, 0, firstRun.stderr);
1445+
1446+
const result = run("node", [SCRIPT, "task", "--read-only", "--resume-last", "read-only follow up"], {
1447+
cwd: repo,
1448+
env: buildEnv(binDir)
1449+
});
1450+
1451+
assert.notEqual(result.status, 0, "must fail closed when resolved sandbox is not read-only");
1452+
assert.match(result.stderr, /read-only/i);
1453+
});
1454+
14311455
test("task rejects --write with --read-only", () => {
14321456
const result = run("node", [SCRIPT, "task", "--write", "--read-only", "inspect the failing test"], {
14331457
cwd: ROOT

0 commit comments

Comments
 (0)