diff --git a/src/secfoo/agents/codex.py b/src/secfoo/agents/codex.py index e456732..a8378b3 100644 --- a/src/secfoo/agents/codex.py +++ b/src/secfoo/agents/codex.py @@ -35,6 +35,9 @@ # "tokens used" on its own line, then the total (thousands separators allowed). _TOKENS_USED_RE = re.compile(r"^\s*tokens used\s*\n\s*([\d,]+)\s*$", re.MULTILINE | re.IGNORECASE) +# Module-level so tests can flip it; patching os.name itself breaks pytest. +_IS_WINDOWS = os.name == "nt" + class CodexAdapter(AgentAdapter): name = "codex" @@ -46,25 +49,29 @@ class CodexAdapter(AgentAdapter): prompt_via_stdin = True def detect_failure(self, stdout: str, stderr: str) -> str | None: - # On Windows without Codex's (admin-installed) sandbox, `--sandbox - # read-only` refuses *every* shell command, reads included, and - # `codex exec` still exits 0 with a "could not inspect" report - # (SECFOO-10 E2E, codex-cli 0.159.2). Never record that as success. + # If Codex's sandbox still refuses its commands, `codex exec` exits 0 + # with a "could not inspect" report (SECFOO-10 E2E, codex-cli + # 0.159.2). Never record that as success. if "exec_command failed" not in stderr: return None - hint = "" - if os.name == "nt": - hint = (" On Windows, Codex's read-only sandbox needs a one-time admin setup" - " (set windows.sandbox = \"elevated\" in ~/.codex/config.toml and approve the prompt).") - return "Codex's sandbox rejected its commands, so the target was never read." + hint + return "Codex's sandbox rejected its commands, so the target was never read." def build_command(self, prompt: str, *, workdir: Path) -> list[str]: + windows_sandbox: list[str] = [] + if _IS_WINDOWS: + # With no Windows sandbox configured, `--sandbox read-only` + # rejects every command, reads included ("blocked by policy"). + # The unelevated (restricted-token) sandbox needs no admin setup + # and was verified to allow reads and deny writes both inside + # and outside the workdir (SECFOO-38, codex-cli 0.159.2). + windows_sandbox = ["-c", 'windows.sandbox="unelevated"'] return [ self.binary, "exec", # Read-only filesystem: the assessment only reads the target. "--sandbox", "read-only", + *windows_sandbox, # secfoo runs against arbitrary targets (cloned repos, user # paths, non-git dirs); without this Codex refuses to start # outside a trusted git repository. diff --git a/tests/test_agents/test_codex.py b/tests/test_agents/test_codex.py index d1f3586..5b861cb 100644 --- a/tests/test_agents/test_codex.py +++ b/tests/test_agents/test_codex.py @@ -49,6 +49,21 @@ def test_prompt_is_read_from_stdin_not_argv(fake_popen, tmp_path): assert fake.stdin_input == prompt +def test_windows_uses_unelevated_sandbox(tmp_path, monkeypatch): + """SECFOO-38: on Windows, read-only with no Windows sandbox configured + rejects every command; the unelevated sandbox reads but can't write.""" + monkeypatch.setattr("secfoo.agents.codex._IS_WINDOWS", True) + cmd = CodexAdapter().build_command("hello", workdir=tmp_path) + assert cmd[cmd.index("-c") + 1] == 'windows.sandbox="unelevated"' + assert cmd[cmd.index("--sandbox") + 1] == "read-only" + + +def test_no_windows_sandbox_flag_elsewhere(tmp_path, monkeypatch): + monkeypatch.setattr("secfoo.agents.codex._IS_WINDOWS", False) + cmd = CodexAdapter().build_command("hello", workdir=tmp_path) + assert not any("windows.sandbox" in arg for arg in cmd) + + def test_sandbox_rejected_commands_is_failed_not_success(fake_popen, tmp_path): """Real stderr from a Windows E2E run (SECFOO-10): every read was refused, yet `codex exec` exited 0 with a "could not inspect" report."""