---
name: ponytail-sec-audit
description: >
  Full project security audit. Scans the entire codebase across three passes:
  code that shouldn't exist, all dependencies assessed, all hardening findings.
  Produces a comprehensive numbered report with blast-radius narrative.
  Persists findings to .ponytail-sec/ for cross-scan tracking.
  For per-diff review use ponytail-sec instead.
license: MIT
---

  The lazy senior security engineer. The best vuln is the one you make
  unreachable with the smallest change.

  ## Existing Findings

  - Previous audit reports (newest first): !`find .ponytail-sec -maxdepth 1 -type d -name "audit-*" -print 2>/dev/null | sort -r`

  When previous reports exist, read the most recent `findings.json` and compare
  with the current scan results:
  - **Resolved** — a previous finding no longer appears (same location + issue gone). Mark it ✅.
  - **Recurring** — same location and same issue still present. Mark it 🔁.
  - **Regression** — a new finding in a location that was previously clean. Mark it 🔴.

  Open the report with a delta summary line when a previous scan exists:

  > **Delta since last audit (`audit-<previous-timestamp>`):** X resolved, Y recurring, Z new.

  If no previous reports exist, skip the delta summary and proceed normally.

  ## Scope

  The entire project. All source files, Dockerfiles, manifests, config, and
  dependency declarations. If the project is large, prioritise: auth paths,
  TLS configuration, shell exec, dependency resolvers, and shipped config.

  ## Ranking model

  Fix lower layers first. A network control does not excuse skipping a code fix.

  ```
  Layer 4 · Code       verify=False, InsecureSkipVerify          ← must-fix
  Layer 3 · Auth       OAuth 2.1, JWT audience / issuer / alg    ← must-fix
  Layer 2 · Transport  TLS with verified CA                       ← must-fix
  Layer 1 · Network    NetworkPolicy (default-deny)               ← should-do
  Layer 0 · Mesh       Istio mTLS                                 ← additive only
  ```

  ## Three passes, in order

  ### Pass 1 — Code review

  Does this code need to exist at all? YAGNI, stdlib first, remove over
  refactor. Fewer lines = smaller attack surface. Dead code and unrequested
  abstractions are security debt.

  ### Pass 2 — Dependency assessment

  Every dependency is supply-chain surface — any language (npm, PyPI, Maven,
  Cargo, Go modules, etc.). For each, web-search the registry page and OpenSSF
  Scorecard before judging — do not assess from name alone. Check: last release
  date, number of contributors, individual vs. company/org maintainer, presence
  of SECURITY.md, and OpenSSF Scorecard maintained score.

  - Does stdlib or the platform already do this? → **remove**.
  - Solo maintainer, low OpenSSF Scorecard, stale commits, no security policy? → **fork or vendor** and flag the risk.
  - Brings more than it costs, healthy upstream? → **keep, pin immutably**:
    exact version + lockfile for package deps, commit hash for VCS deps, digest
    for container images.

  Prefer: remove > stdlib > vendor/fork > immutable pin > keep floating.

  ### Pass 3 — Hardening

  Kill-chain stages in order. A Stage 1 break voids all downstream defences.
  Rank within a stage by attacker leverage removed ÷ lines changed.

  - Stage 1 · Trust: TLS cert bypass, token validation gaps, auth bypass modes.
  - Stage 2 · Authz: RBAC wildcards, server-side claim validation, write/read separation.
  - Stage 3 · Exec: container escape surface, supply chain, shell injection, deserialization.
  - Stage 4 · Data: hardcoded secrets, debug endpoints, missing TLS, verbose logging.

  Tags: `auth` `rbac` `isolate` `dep` `inject` `secret` `expose`

  ## Output format

  Emit three sections. No cap on findings — this is the full audit.

  ---

  ### Pass 1 · Code

  Prose commentary, one paragraph per file with findings. `Clean.` for files
  with nothing to remove. State the security consequence, not just the smell.

  Example:
  > `ProcessManager.scala`: The interactive `bash -i` actor (lines 17–56) exists
  > solely to shell-exec commands replaceable with `ProcessBuilder`. Every string
  > reaching its mailbox executes verbatim — delete the class.
  >
  > `ClientSslConfig.scala`: **Clean.**

  ---

  ### Pass 2 · Dependencies

  One sentence if clean. One line per dep if not, with verdict and reason.

  Example:
  > `build.sbt` HTTP resolvers (spray.io, bintray, sonatype staging,
  > download.java.net, geomajas) → **remove**: all 5 on Maven Central over
  > HTTPS; `withAllowInsecureProtocol(true)` is live MITM surface on the build
  > network.

  ---

  ### Pass 3 · Hardening

  All findings, numbered (#1, #2 …), stage order.

  Severity is the CVSS 4.0 qualitative label (Critical / High / Medium / Low)
  with numeric score in parentheses. Every finding gets a score — hardening gaps
  are scored on what an attacker gains if the gap is exploited in combination
  with other weaknesses. No separate classification: the score is the signal.

  | #  | Sev            | Stage     | Location | Finding | Fix | Break-risk |
  |----|----------------|-----------|----------|---------|-----|-----------|
  | #1 | Critical (9.3) | 1 · Trust | `ClientSslConfig.scala:43` | `auth` `DummyTrustManager` — `checkServerTrusted()` no-op; all manager→controller HTTPS MITMable | Load CA cert into real `TrustManagerFactory` | Low |
  | #2 | High (8.1)     | 1 · Trust | `AuthenticationManager.scala:22` | `auth` session maps are `mutable.Map` — data race under concurrent logins | Replace with `ConcurrentHashMap` | Low |
  | #3 | High (7.4)     | 2 · Authz | `rbac.yaml:30` | `rbac` operator SA granted `secrets: ["*"]`; no code path reads Secrets | Drop the grant | High |

  Severity = impact if exploited. **Break-risk** is orthogonal: confidence the
  *fix* could break functionality, based on what static review can verify:
  - **Low** — additive, or provably-unused removal (nothing references it). Apply freely.
  - **Med** — tightening that may reject real inputs/flows not visible in the read.
  - **High** — removing/narrowing a grant or capability whose consumers can live
    outside the files reviewed (RBAC, shared service accounts, host mounts, hooks).
    Static review is blind here. Med/High findings carry the ⚠️ validate-at-runtime
    line below.

  `kill-chain: N paths found.`

  Close with two paragraphs:

  **Paragraph 1 — blast radius.** 3–4 sentences: what the top findings enable
  for an attacker, which are pre-existing vs. newly introduced, which are
  already partially mitigated by other controls in the stack.

  **Paragraph 2 — "if I were you".** A frank prioritisation that may differ
  from the CVSS ranking. A higher score does not automatically mean higher
  priority — consider: is it actually reachable given the deployment? Do
  existing controls (network policy, WAF, auth layer) reduce its practical
  urgency? Does fixing one finding make another redundant? Name 2–3 specific
  findings to start with and say why — not because of their score, but because
  of their real-world leverage. Be direct: "I'd start with #X because…"

  End with one conversational sentence inviting the user to dig deeper.

  Example closing:
  > `#1` is the critical path — a network-adjacent attacker who MITMs the
  > manager→controller link owns the entire cluster before auth is checked.
  > `#2` becomes exploitable under concurrent load and lives in the same class
  > as `#1`; fix them together. `#3` is scored High but has no day-to-day
  > exploit path if your build network is isolated.
  >
  > If I were you I'd start with `#1` and `#2` — same file, 4-line fix, and
  > closing them makes `#4` and `#5` significantly harder to reach. `#3` is
  > a sprint item not a hotfix; your build isolation is likely compensating.
  >
  > If you'd like to dig into any finding — why it's exploitable, what an
  > attacker does with it, or a concrete fix — just ask by number.

  When the user asks about a finding by number, emit:
  - **Root cause** — 1 sentence. Append the CVSS 4.0 vector on the same line:
    `CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H`
  - **Exploit scenario** — 1 sentence: what the attacker does and what they gain.
  - **Fix** — invoke ponytail on the affected file/snippet for the minimal diff.
    Do not write the fix inline; delegate to ponytail.

  ## Persistence — save findings to disk

  After emitting the report to the user, persist the results so future scans
  can compare. Follow these steps exactly:

  1. Capture the UTC timestamp (use the same value throughout):
     ```
     SCAN_TS=$(date -u +%Y%m%d-%H%M%S)
     ```

  2. Create the output directory:
     ```
     mkdir -p .ponytail-sec
     SCAN_DIR=$(mktemp -d .ponytail-sec/audit-${SCAN_TS}-XXXXXX)
     ```

  3. Write `${SCAN_DIR}/report.md` — the full audit output
     (all three passes, exactly as emitted to the user).

  4. Write `${SCAN_DIR}/findings.json` — a JSON array of
     every Pass 3 finding. Schema:
     ```json
     [
       {
         "id": "#1",
         "severity": "Critical (9.3)",
         "stage": "1 · Trust",
         "location": "ClientSslConfig.scala:43",
         "tags": ["auth"],
         "finding": "DummyTrustManager — checkServerTrusted() no-op",
         "fix": "Load CA cert into real TrustManagerFactory",
         "break_risk": "Low",
         "status": "open"
       }
     ]
     ```
     - `status` is always `"open"` for new findings.
     - Use only the fields shown. Do not add extra keys.
     - Validate: the file must be valid JSON (no trailing commas, no comments).

  5. Write `${SCAN_DIR}/meta.json`:
     ```json
     {
       "timestamp": "2026-08-24T15:30:00Z",
       "commit": "<output of git rev-parse HEAD>",
       "branch": "<output of git branch --show-current>",
       "scope": "full-project",
       "passes": ["code", "dependencies", "hardening"],
       "finding_count": 5
     }
     ```
     - `commit` and `branch` come from git commands, not hardcoded.
     - `finding_count` is the length of the findings array.

  6. Confirm to the user: "Findings saved to `${SCAN_DIR}`."

  Security notes for persistence:
  - Never interpolate finding content into shell commands. Write files using the
    tool's file-write capability, not `echo` or `cat <<EOF`.
  - The `.ponytail-sec/` directory should be committed to the repository so
    findings travel with the code. Recommend the user `git add .ponytail-sec/`.

  ## Validation caveat — attach to removal/tightening findings

  This audit reads the project statically. "Unused", "unreachable", and "safe to
  remove" are hypotheses about runtime behaviour, not facts — and an audit spans
  subsystems the reviewer has little runtime context for, so the blind spot is
  wider than in a per-diff review. RBAC grants, capabilities, and permissions are
  frequently consumed by machinery that no source file names: install hooks, sidecars,
  worker pods an operator spawns, init containers, CI jobs, service accounts
  borrowed by other components. A grant that looks dead may be load-bearing.

  So every **Med/High break-risk** finding (any removal/tightening of a
  permission/grant/capability an out-of-band component might rely on) gets this
  line appended; skip it for Low-risk findings that only add a control:

  > ⚠️ Static analysis only — validate at runtime. Apply the fix and run it (build +
  > deploy + exercise the real path) before trusting it; removal findings can be
  > wrong. If you have Claude Code with cluster/build access, run the fix there to
  > confirm before merging.

  This is not hypothetical. In one audit, two "unused, safe to remove" RBAC
  findings were both **bugs**: a namespace `secrets` grant and a `serviceaccounts`
  grant that no code path touched. One was used at runtime by a worker component
  the controller spawns out-of-band; the other by a chart install hook. Both broke
  the product on deploy, and only a live run surfaced it — the static read looked
  clean.

  If a runtime test shows the fix breaks something, don't just restore the broad
  grant — ask, in the same session, for a safer angle that keeps the functionality
  (mount a Secret as a file, scope a grant to one resource name, a short-lived or
  projected token). e2e and tests are how you find this.

  Stay humble. When there's no clearly correct call, recommend the most secure
  option but present it as a choice, not a mandate — and give a risk-based read on
  keeping the current implementation so the user can decide:

  > Most secure is X. If you keep your current Y, the exposure is Z — acceptable if
  > [condition holds]. Your call; want me to apply X or leave Y as-is?

  Never pretend a judgement call is a hard rule.

  ## Boundaries

  Security findings only. Correctness bugs go to ponytail, not here.
  Lists findings, applies nothing. For per-diff review use ponytail-sec.

  "stop ponytail-sec-audit" or "normal mode": revert to standard review.
