From 21ea515ffaafb81946cf0b1e76aa421ce2fffb4d Mon Sep 17 00:00:00 2001 From: Varun Poojari Date: Mon, 21 Sep 2026 17:30:45 +0100 Subject: [PATCH] feat: add Codex CLI agent adapter --- Dockerfile | 2 +- README.md | 8 ++--- SECURITY.md | 2 +- docs/REFERENCE.md | 3 +- src/secfoo/agents/codex.py | 54 +++++++++++++++++++++++++++++++++ src/secfoo/agents/registry.py | 2 ++ src/secfoo/cli.py | 1 + src/secfoo/config.example.toml | 2 +- src/secfoo/mcp.py | 3 +- tests/test_agents/test_codex.py | 43 ++++++++++++++++++++++++++ tests/test_cli.py | 14 +++++++-- 11 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 src/secfoo/agents/codex.py create mode 100644 tests/test_agents/test_codex.py diff --git a/Dockerfile b/Dockerfile index ed49883..6935fa4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ # robust route for a container image, since there's no cross-platform # concern (the image itself is the platform). # -# Note: none of the 4 agent CLIs (claude, agent/cursor, agy, gemini) are +# Note: none of the 5 agent CLIs (claude, agent/cursor, agy, gemini, codex) are # installed in this image, and none of their auth lives here either -- # `secfoo run` needs one available on PATH inside the container (e.g. via # a custom image built FROM this one, or a volume mount) to actually diff --git a/README.md b/README.md index fa5a858..3dcb742 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Context-based security architectural assessment orchestrator: pick skills, targe Pick one or more security **activities** (see the catalog below), point them at a **target** (a public GitHub URL, a local directory, plus optional Confluence links for extra context), choose which coding-agent CLI runs them -(Claude Code, Cursor, Antigravity, or Gemini CLI), and browse every +(Claude Code, Cursor, Antigravity, Gemini CLI, or Codex CLI), and browse every assessment ever run, across every project, in a local web dashboard. > This repository is the open-source CLI and local dashboard. secfoo's @@ -41,7 +41,7 @@ secfoo run --skill security-architecture-review --agent claude ``` You'll need an agent CLI already installed and authenticated — Claude -Code, Cursor, Antigravity, or Gemini CLI (`--agent claude|agent|agy|gemini`). +Code, Cursor, Antigravity, Gemini CLI, or Codex CLI (`--agent claude|agent|agy|gemini|codex`). On a real terminal, that first run prompts for a project name and application ID, then browse every result in the dashboard: @@ -103,8 +103,8 @@ the [CLI reference](https://secfoo.com/docs/cli.html) for more. record every rescan. - **Concurrent by default**: multiple `--skill` flags run at the same time, not one after another. -- **Bring your own agent**: Claude Code, Cursor, Antigravity, or Gemini - CLI — pick whichever you already use and trust. +- **Bring your own agent**: Claude Code, Cursor, Antigravity, Gemini + CLI, or Codex CLI — pick whichever you already use and trust. - **Open source CLI**: MIT licensed. ## Activity catalog diff --git a/SECURITY.md b/SECURITY.md index 1054dae..b65d603 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -14,7 +14,7 @@ and, if possible, steps to reproduce. We aim to acknowledge reports within secfoo is a developer tool that runs in your environment with your user permissions. `secfoo run` shells out to a coding-agent CLI you already have -installed and trust (Claude Code, Cursor, Antigravity, or Gemini CLI) and +installed and trust (Claude Code, Cursor, Antigravity, Gemini CLI, or Codex CLI) and points it at a target — a local directory or a shallow clone of a public GitHub repo — with an instruction prompt telling it to *read and report only*. secfoo does not itself execute code from the target; the agent CLI diff --git a/docs/REFERENCE.md b/docs/REFERENCE.md index bb486ba..0e1aef7 100644 --- a/docs/REFERENCE.md +++ b/docs/REFERENCE.md @@ -360,6 +360,7 @@ How MCP servers reach each agent varies by what its CLI actually supports: |---|---| | `claude` | Automatic on every `secfoo run` (`--mcp-config`, scoped to that invocation only — your global Claude config is never touched) | | `gemini`, `agent` (Cursor) | Run `secfoo mcp sync --agent ` once to register persistently in that tool's own config | +| `codex` | Not wired up yet — Codex reads MCP servers from its own `~/.codex/config.toml` | | `agy` (Antigravity) | Not supported yet — its CLI has no MCP configuration mechanism as of this writing | ```bash @@ -389,7 +390,7 @@ along with a `SHA256SUMS` file. - **Docker**: `ghcr.io/rakfortltd/secfoo`, built from the PyPI package on a slim Python base (simplest, most robust route for a container image — no cross-platform concern since the image itself is the platform). - **Note**: none of the 4 agent CLIs (claude, cursor, antigravity, gemini) + **Note**: none of the 5 agent CLIs (claude, cursor, antigravity, gemini, codex) are installed in this image, so `secfoo run` needs one made available on `PATH` inside the container yourself (e.g. a custom image built `FROM` this one, or a volume/bind mount) to actually execute a skill — without diff --git a/src/secfoo/agents/codex.py b/src/secfoo/agents/codex.py new file mode 100644 index 0000000..be3efe0 --- /dev/null +++ b/src/secfoo/agents/codex.py @@ -0,0 +1,54 @@ +"""OpenAI Codex CLI adapter. + +Flags verified against a real `codex exec --help` (codex-cli 0.155, Sep 2026): +`codex exec` runs a single prompt non-interactively. Progress/tool activity +goes to stderr and only the agent's final message is printed to stdout, so +the base class's default `extract_report` (stdout is the report) is correct +here and deliberately not overridden. `--json` would switch stdout to a JSONL +event stream, which we do not want for a plain-markdown report. + +Deliberately NOT passed: +- `--dangerously-bypass-approvals-and-sandbox` / `--full-auto`: a read-only + review never needs write access, so we pin `--sandbox read-only` instead. +- `--json`: see above. + +MCP servers from ~/.secfoo/config.toml are not wired up for Codex yet +(`secfoo mcp sync --agent codex` is a follow-up); Codex runs use whatever is +already in the user's own ~/.codex/config.toml. +""" + +from __future__ import annotations + +from pathlib import Path + +from secfoo.agents.base import AgentAdapter + + +class CodexAdapter(AgentAdapter): + name = "codex" + binary = "codex" + default_timeout_seconds = 1800 + + def build_command(self, prompt: str, *, workdir: Path) -> list[str]: + return [ + self.binary, + "exec", + # Read-only filesystem: the assessment only reads the target. + "--sandbox", + "read-only", + # secfoo runs against arbitrary targets (cloned repos, user + # paths, non-git dirs); without this Codex refuses to start + # outside a trusted git repository. + "--skip-git-repo-check", + # Don't litter ~/.codex/sessions with one file per assessment. + "--ephemeral", + # No ANSI escapes in captured stdout/stderr. + "--color", + "never", + "--cd", + str(workdir), + # `--` so a prompt that happens to start with "-" is never + # parsed as a flag. + "--", + prompt, + ] diff --git a/src/secfoo/agents/registry.py b/src/secfoo/agents/registry.py index 0f11b9b..3543115 100644 --- a/src/secfoo/agents/registry.py +++ b/src/secfoo/agents/registry.py @@ -3,6 +3,7 @@ from secfoo.agents.antigravity import AntigravityAdapter from secfoo.agents.base import AgentAdapter from secfoo.agents.claude import ClaudeAdapter +from secfoo.agents.codex import CodexAdapter from secfoo.agents.cursor import CursorAdapter from secfoo.agents.gemini import GeminiAdapter from secfoo.agents.secfoo import SecFooAdapter @@ -13,6 +14,7 @@ AntigravityAdapter.name: AntigravityAdapter, GeminiAdapter.name: GeminiAdapter, SecFooAdapter.name: SecFooAdapter, + CodexAdapter.name: CodexAdapter, } diff --git a/src/secfoo/cli.py b/src/secfoo/cli.py index 2fc3fc2..22b50d1 100644 --- a/src/secfoo/cli.py +++ b/src/secfoo/cli.py @@ -95,6 +95,7 @@ class AgentId(str, Enum): ANTIGRAVITY = "agy" GEMINI = "gemini" SECFOO = "secfoo" + CODEX = "codex" class DepthId(str, Enum): diff --git a/src/secfoo/config.example.toml b/src/secfoo/config.example.toml index 28ac0a0..752d7a2 100644 --- a/src/secfoo/config.example.toml +++ b/src/secfoo/config.example.toml @@ -5,7 +5,7 @@ [defaults] # Used whenever --agent / --depth / --timeout aren't passed on the command # line. CLI flags always win over these. -agent = "claude" # claude | agent (Cursor) | agy (Antigravity) | gemini | secfoo +agent = "claude" # claude | agent (Cursor) | agy (Antigravity) | gemini | secfoo | codex depth = "quick" # quick | standard # timeout = 1800 # seconds; omit to use each agent's own default diff --git a/src/secfoo/mcp.py b/src/secfoo/mcp.py index dc79225..b22adba 100644 --- a/src/secfoo/mcp.py +++ b/src/secfoo/mcp.py @@ -157,5 +157,6 @@ def sync(agent_id: str, servers: list[MCPServerConfig]) -> list[SyncResult]: raise ValueError( f"secfoo mcp sync isn't supported for {agent_id!r}. " f"claude uses --mcp-config automatically on every run (no sync needed); " - f"agy doesn't expose any MCP configuration mechanism via its CLI yet." + f"agy doesn't expose any MCP configuration mechanism via its CLI yet; " + f"codex isn't wired up for it yet (it reads ~/.codex/config.toml directly)." ) diff --git a/tests/test_agents/test_codex.py b/tests/test_agents/test_codex.py new file mode 100644 index 0000000..30cc83d --- /dev/null +++ b/tests/test_agents/test_codex.py @@ -0,0 +1,43 @@ +from __future__ import annotations + +from secfoo.agents import ADAPTERS, get_adapter +from secfoo.agents.codex import CodexAdapter + + +def test_registered_under_codex_id(): + assert ADAPTERS["codex"] is CodexAdapter + assert isinstance(get_adapter("codex"), CodexAdapter) + + +def test_build_command_is_non_interactive_exec(tmp_path): + cmd = CodexAdapter().build_command("hello", workdir=tmp_path) + assert cmd[:2] == ["codex", "exec"] + + +def test_build_command_is_read_only(tmp_path): + cmd = CodexAdapter().build_command("hello", workdir=tmp_path) + assert cmd[cmd.index("--sandbox") + 1] == "read-only" + assert "--dangerously-bypass-approvals-and-sandbox" not in cmd + assert "--full-auto" not in cmd + + +def test_build_command_headless_flags(tmp_path): + cmd = CodexAdapter().build_command("hello", workdir=tmp_path) + assert "--skip-git-repo-check" in cmd + assert "--ephemeral" in cmd + assert cmd[cmd.index("--color") + 1] == "never" + assert "--json" not in cmd # report must stay plain text on stdout + + +def test_build_command_sets_working_root(tmp_path): + cmd = CodexAdapter().build_command("hello", workdir=tmp_path) + assert cmd[cmd.index("--cd") + 1] == str(tmp_path) + + +def test_prompt_is_last_arg_after_double_dash(tmp_path): + cmd = CodexAdapter().build_command("--looks-like-a-flag", workdir=tmp_path) + assert cmd[-2:] == ["--", "--looks-like-a-flag"] + + +def test_extract_report_is_stdout_passthrough(): + assert CodexAdapter().extract_report("final message") == "final message" diff --git a/tests/test_cli.py b/tests/test_cli.py index b247a43..b7deceb 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -201,10 +201,10 @@ def fake_execute_runs(**kwargs): assert "Security Architecture Review" in result.stdout -def test_agents_command_lists_all_five_adapters(): +def test_agents_command_lists_all_adapters(): result = runner.invoke(app, ["agents"]) assert result.exit_code == 0 - for agent_id in ["claude", "agent", "agy", "gemini", "secfoo"]: + for agent_id in ["claude", "agent", "agy", "gemini", "secfoo", "codex"]: assert agent_id in result.stdout @@ -417,3 +417,13 @@ def test_mcp_sync_unsupported_agent_exits_nonzero(monkeypatch): ) result = runner.invoke(app, ["mcp", "sync", "--agent", "agy"]) assert result.exit_code == 1 + + +def test_mcp_sync_codex_is_reported_unsupported(monkeypatch): + servers = [MCPServerConfig(name="s", command="npx")] + monkeypatch.setattr( + "secfoo.cli.load_config", lambda: SecfooConfig(defaults=Defaults(), mcp_servers=servers) + ) + result = runner.invoke(app, ["mcp", "sync", "--agent", "codex"]) + assert result.exit_code == 1 + assert "codex" in result.output