Skip to content

Harden the Claude adapter (SECFOO-40 to 43) - #26

Merged
Varunpoojari merged 4 commits into
secfoo-com:mainfrom
Varunpoojari:fix/claude-adapter-hardening
Oct 3, 2026
Merged

Varunpoojari merged 4 commits into
secfoo-com:mainfrom
Varunpoojari:fix/claude-adapter-hardening

Conversation

@Varunpoojari

Copy link
Copy Markdown
Collaborator

Summary

Four fixes to the existing Claude Code adapter (src/secfoo/agents/claude.py), found while verifying SECFOO-5. One commit per Jira issue.

Commit Issue Change
1 SECFOO-40 Stop Claude Code loading the scanned repo's own settings and MCP servers
2 SECFOO-41 Record why a failed Claude run failed
3 SECFOO-42 Send the prompt on stdin instead of argv
4 SECFOO-43 No session file per scan; document the --permission-mode value; module docstring

The adapter's command changes from

claude -p <prompt> --output-format json --allowedTools <tools> --permission-mode default

to

claude -p --output-format json --allowedTools <tools> --permission-mode default \
  --setting-sources user --strict-mcp-config --no-session-persistence     # prompt on stdin

What each commit does

SECFOO-40. claude -p skips the workspace-trust prompt, so running it inside a target loaded that target's .claude/settings.json. --setting-sources user ignores the target's project and local settings (the user's own still apply). --strict-mcp-config loads only the MCP servers secfoo passes through --mcp-config. docs/REFERENCE.md gets one sentence saying so.

SECFOO-41. On an error result Claude Code exits 1 with an empty stderr and puts the reason in its stdout JSON. secfoo keeps only stderr as the failure reason, so the run was saved as failed with nothing to explain it. base.py gains a describe_failure() hook, consulted only on a non-zero exit; adapters that do not override it behave as before. ClaudeAdapter reads subtype and errors from the result.

SECFOO-42. Same change PR #23 made for Codex and Copilot: prompt_via_stdin = True, prompt dropped from the argv.

SECFOO-43. --no-session-persistence, so a scan does not leave a transcript under ~/.claude/projects/. --permission-mode default is kept: default is no longer listed in claude --help but is still accepted. A module docstring records the CLI version the flags were checked against.

Testing

Claude Code 2.1.288 on Linux, main @ 138452f.

  • pytest tests: 751 passed (736 before, 15 new).
  • ruff check on the touched files: clean. Repo-wide count unchanged.
  • Real runs against a test repo that ships a SessionStart hook, a .mcp.json server, and settings with enableAllProjectMcpServers and permissions.allow: ["Write", "Edit", "Bash"]:
Check Before After
Target's hook runs yes no
Target's .mcp.json server starts yes no
Write outside the tool grant denied denied
secfoo's own --mcp-config server starts not checked yes
Session file written yes no
Three-line prompt reaches the model in full not checked yes
  • A real secfoo run --skill sast --agent claude against that repo: status success, the SQL injection in the test file reported as one High finding, tokens and cost recorded, target unchanged.
  • A stub claude that replays a captured error result (error_max_budget_usd, exit 1): the run is failed and stderr.log now reads secfoo: Claude Code reported error_max_budget_usd: Reached maximum budget ($0.0001).

Not tested

  • Windows and macOS. The Windows argv truncation behind SECFOO-42 is not reproduced for Claude; only the stdin path is verified, on Linux.
  • Older Claude Code versions. A CLI that lacks one of the new flags exits 1 with error: unknown option, so the run fails visibly instead of running without the protection.
  • Other adapters. Whether Codex, Copilot, Gemini, Cursor or Antigravity load project-level config from the target is not checked here.

Notes for reviewers

  • SECURITY.md says the agent CLI "does whatever it would normally do when you point it at that same target directly". For Claude that is now stricter than the text. I have left SECURITY.md unchanged; say if you want it updated in this PR.
  • With --strict-mcp-config, MCP servers from the user's global Claude config are no longer started during a scan. Their tools were never in --allowedTools, so they could not be called before either.
  • PR Feature/memory bank commit tracking #22 also edits src/secfoo/agents/base.py. This branch does not touch the run() signature line, and a trial merge of the two auto-merges base.py.

🤖 Generated with Claude Code

https://claude.ai/code/session_01JkVdBSUgu2rKg3G7Tg6DJD

Varunpoojari and others added 4 commits October 3, 2026 10:28
…P servers (SECFOO-40)

`claude -p` skips the workspace-trust prompt, so running it inside an
untrusted target loaded that target's `.claude/settings.json`: its hooks
ran as shell commands, and `enableAllProjectMcpServers` started whatever
its `.mcp.json` named. Both reproduced on Claude Code 2.1.288.

- `--setting-sources user`: ignore project and local settings from the
  target; the user's own settings still apply.
- `--strict-mcp-config`: only load MCP servers secfoo passes through
  `--mcp-config`.
- Tests for both flags, and that permissions are never bypassed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JkVdBSUgu2rKg3G7Tg6DJD
On an error result Claude Code exits 1 with an empty stderr and puts the
reason in its stdout JSON (`is_error`, `subtype`, `errors`). secfoo only
keeps stderr as the failure reason, so the run was saved as failed with
no explanation.

- base.py: add a `describe_failure()` hook, consulted only on a non-zero
  exit, whose reason is prepended to stderr like `detect_failure()`'s.
  Adapters that don't override it are unaffected.
- claude.py: read `subtype` and `errors` (or `result`) from an error
  result. Share one JSON-object parser, so a non-object or null `result`
  no longer raises in `extract_report()`.
- Tests use an error payload captured from Claude Code 2.1.288.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JkVdBSUgu2rKg3G7Tg6DJD
PR secfoo-com#23 moved Codex and Copilot to stdin because npm-installed CLIs are
`.cmd` shims on Windows and cmd.exe cuts an argv prompt at its first
newline. The Claude adapter still passed the prompt as an argument.

`claude -p` with no prompt argument reads it from stdin; a two-line
prompt was read in full on Linux with Claude Code 2.1.288. The Windows
truncation itself was not reproduced for Claude (no Windows machine).

- claude.py: `prompt_via_stdin = True`, prompt dropped from the argv.
- test_base.py: the "stdin is DEVNULL" test used Claude as its example
  of an argv-prompt adapter; it now uses Cursor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JkVdBSUgu2rKg3G7Tg6DJD
- `--no-session-persistence`: a scan no longer leaves a transcript under
  ~/.claude/projects/<target path>/ (the Codex adapter's `--ephemeral`).
- `--permission-mode default` is kept. `default` is no longer listed in
  `claude --help` (2.1.288) but is still accepted, and a tool outside the
  grant is still denied with it. The listed names were not tried on older
  CLIs, so the value is unchanged and the reasoning is now in a comment.
- Module docstring recording the CLI version the flags were checked
  against, as codex.py and copilot.py do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JkVdBSUgu2rKg3G7Tg6DJD
@Varunpoojari
Varunpoojari merged commit 422bebf into secfoo-com:main Oct 3, 2026
2 checks passed
@Varunpoojari Varunpoojari mentioned this pull request Oct 9, 2026
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant