gh-aw-harness
CLAUDE.md3 starsChanged 6 months agoMITView on GitHub
gh-aw-threat-detection is the threat detection component for GitHub Agentic Workflows (gh-aw). It is a Go CLI (threat-detect) that analyzes artifacts produced by AI agents — prompts, agent output, git patches, comment memory — and decides whether to allow or block the downstream safe-outputs job. The component runs in two main contexts: It detects three categories: prompt injection, secret leak, and malicious patch, and emits a strict JSON contract. Run these from the repo root. They are the canonical entry points…
- Reads credentials
In this repository3 other files it gives its agents.
- AGENTS.mdAGENTS.md
- console-renderingSkill
- error-messagesSkill
The file
We split this CLAUDE.md into the rules it gives an agent
5 rules. About 0 are common, 3 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 55
Run these from the repo root.
- Very specific: line 150
make lifecycle-validateMUST pass: before changing this file. - Distinctive: line 173
To regenerate the container siblings: after recompiling smokes:
- Distinctive: line 190
When changing this repo:
- Distinctive: line 216
When a CI workflow fails, always follow this order:
≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
CLAUDE.md
This file provides guidance to Claude Code (and other coding agents) when working in this repository.
Project Overview
gh-aw-threat-detection is the threat detection component for GitHub Agentic Workflows (gh-aw). It is a Go CLI (threat-detect) that analyzes artifacts produced by AI agents — prompts, agent output, git patches, comment memory — and decides whether to allow or block the downstream safe-outputs job.
The component runs in two main contexts:
- As a host CLI (
./bin/threat-detect <artifacts-dir>) inside agh-aw-generated detection job. - As a published container image (
ghcr.io/github/gh-aw-threat-detection:<tag>) extracted bygh-awand executed under the AWF firewall (see AWF section below).
It detects three categories: prompt injection, secret leak, and malicious patch, and emits a strict JSON contract.
Key facts
- Language: Go 1.23+ (module
github.com/github/gh-aw-threat-detection) - Binary:
bin/threat-detect(built viamake build) - Container: published to
ghcr.io/github/gh-aw-threat-detection, Alpine-based, non-root userdetector - Spec:
specs/threat-detection-spec.md— W3C-style normative spec; the source of truth for behavior - Engines supported:
copilot(default),claude,codex— invoked fromPATH, not bundled into the image - Two-phase analysis: fast structured-output triage via
api-proxy/reflect, falling back to full CLI engine analysis
Repository Layout
cmd/threat-detect/ CLI entry point and flag parsing (main.go)
pkg/artifacts/ Artifact directory loading and validation
pkg/detector/ Core detection logic
├── detector.go BuildPrompt and prompt template handling (//go:embed prompts/)
├── triage.go Phase 1 fast triage prompt builder (bounded inline content)
├── result.go Result struct + JSON Schema + parser (THREAT_DETECTION_RESULT: prefix)
├── static.go PromptAnalysis: trusted-template vs untrusted-input breakdown
├── correction.go Self-correction retry prompt builders
├── lifecycle_registry_test.go Validates releases/threat-detection-lifecycle.json
└── prompts/ Embedded markdown prompts (threat_detection.md, *_triage.md)
pkg/engine/ AI engine abstraction
├── engine.go copilot/claude/codex CLI adapters; Copilot uses runCLIWithPromptFile, Claude uses runCLI with stdin, Codex passes prompts via codexArgs/runCLIEnv
├── reflect.go ReflectClient → api-proxy /reflect (structured output)
└── capabilities.go Per-provider structured-output mode detection (json_schema, gemini, anthropic tool)
specs/ Normative spec (threat-detection-spec.md)
releases/ Lifecycle registry (threat-detection-lifecycle.json)
scripts/ create-threat-detection-sibling-workflows.py (regenerates *-container.lock.yml)
skills/ Repo-relevant agent skills (console-rendering, error-messages)
scratchpad/ Retained design references inherited from gh-aw
.github/workflows/ CI, release, promote, yank, replay-detection, smoke-{copilot,claude,codex}[-container]
.devcontainer/ Codespaces / devcontainer setup (Go, Docker, gh, Copilot CLI, optional Vertex)
Dockerfile Multi-stage Alpine image
Makefile All build/test/lint/release targetsDaily Workflow Commands
Run these from the repo root. They are the canonical entry points — prefer them over ad-hoc go invocations.
make deps # go mod download + tidy
make deps-dev # + install gosec, govulncheck, golangci-lint v2.8.0
make build # builds bin/threat-detect with version ldflag
make test # go test -v -race ./...
make test-coverage # writes coverage.out + coverage.html
make lint # go vet ./...
make golint # golangci-lint (requires deps-dev)
make fmt # go fmt ./...
make fmt-check # CI-style gofmt check
make security-scan # gosec + govulncheck
make lifecycle-validate # validate releases/threat-detection-lifecycle.json
make docker-build # build ghcr.io/github/gh-aw-threat-detection:<version>
make docker-smoke # build + verify CA bundle + --version
make sbom # SPDX + CycloneDX SBOMs (requires syft)
make agent-finish # full maintainer validation: deps-dev, fmt, lint, build, test, security-scanAlways run make agent-finish (or at minimum make fmt lint build test) before declaring a code change complete.
CLI Contract
threat-detect [flags] <artifacts-dir>Important flags (see README.md for the full list):
--engine {copilot|claude|codex}(defaultcopilot)--model <name>— model override forwarded to the engine--prompt-template <path>— override the embedded default--output <path>— write JSON result (defaults to stdout)--triage(defaulttrue) — Phase 1 structured-output triage--reflect-url <url>—api-proxy/reflectbase (defaulthttp://127.0.0.1:8080/reflect); env:THREAT_DETECTION_REFLECT_URL,API_PROXY_REFLECT_URL,REFLECT_URL--triage-model,--triage-max-bytes,--triage-retries
Exit codes (defined in cmd/threat-detect/main.go):
| Code | Meaning |
| 0 | Safe (no threats detected) |
| 1 | Threat detected |
| 2 | Infrastructure / configuration error |
Result contract (from pkg/detector/result.go):
{ "prompt_injection": false, "secret_leak": false, "malicious_patch": false, "reasons": [] }The full detector expects engine output to contain a line beginning with THREAT_DETECTION_RESULT: followed by the JSON object. The /reflect triage path uses a strict JSON Schema (detector.ResultJSONSchema) instead.
Artifacts directory shape (validated by pkg/artifacts/artifacts.go):
<artifacts-dir>/
├── aw-prompts/
│ ├── prompt.txt # required
│ ├── prompt-template.txt # optional, pre-expansion
│ └── prompt-import-tree.json # optional, runtime-import provenance
├── agent_output.json # required
├── aw_info.json # optional, activation metadata
├── aw-*.patch # optional, git format-patch
├── aw-*.bundle # optional, git bundles
├── experiments/ # optional
└── comment-memory/*.md # optionalTwo-Phase Detection Flow
- Phase 1 — Triage (
--triageon, default)pkg/detector/triage.gobuilds a bounded prompt with inline artifact content (capped at--triage-max-bytes, default 32 KiB per artifact).pkg/engine/reflect.gocallsPOST <reflect-url>withResultJSONSchemaenforcement.- Per-provider mode selection in
pkg/engine/capabilities.go:json_schema(OpenAI),response_schema(Gemini), tool-use schemas (Anthropic), or fallbackjsonmode. - All-false valid result → exit 0 immediately (skip the full detector).
- Threats, uncertainty, malformed responses, unsupported models, or proxy errors → fail safe into Phase 2.
- Phase 2 — Full detection
pkg/detector/detector.go(BuildPrompt) rendersprompts/threat_detection.mdwith placeholders substituted from artifacts andBuildPromptAnalysis(untrusted-input breakdown).- Prefers
/reflectstructured output when available; otherwise invokes the engine CLI fromPATHviapkg/engine/engine.go(copilot,claude, andcodexuse engine-specific prompt-passing paths;runCLIWithPromptFileis used by Copilot). - On malformed output, a one-shot self-correction prompt is built (
pkg/detector/correction.go) and retried.
Result Lifecycle Registry
releases/threat-detection-lifecycle.json is the machine-readable source of truth for release status. The parent gh-aw orchestrator vendors or fetches it before pulling/running a detector image.
| Status | Behavior gh-aw must enforce |
active | Run normally |
deprecated | Warn (Actions annotation + step summary), then run |
obsolete | Fail closed before container starts |
yanked | Fail closed — stronger than obsolete; explicit pins must not be silently rewritten |
make lifecycle-validate MUST pass before changing this file. The validator is pkg/detector/lifecycle_registry_test.go (TestThreatDetectionLifecycleRegistry).
See DEVGUIDE.md for the prerelease → promote → yank workflows.
Release & Promotion Model
Four workflows orchestrate releases:
.github/workflows/create-release-tag.yml— manual; pushesvX.Y.Z..github/workflows/release.yml— triggered by tag push; gated byrelease-publishenvironment; builds + publishes container image as a prerelease..github/workflows/promote-release.yml— manual; gated byrelease-promote; retags the verified image digest aslatestand marks the GitHub release stable..github/workflows/yank-release.yml— manual; updates lifecycle registry and (if needed) retagslatestto a safe stable replacement.
The latest container tag and "Latest" release badge only move on explicit promotion — never automatically.
Smoke Workflows (and Container Siblings)
This repo runs daily AW smoke tests against all three engines:
.github/workflows/smoke-{copilot,claude,codex}.md— AW source files.github/workflows/smoke-{copilot,claude,codex}.lock.yml— compiled bygh aw compile.github/workflows/smoke-{copilot,claude,codex}-container.{md,lock.yml}— sibling workflows that pull the released container image, extractthreat-detect, and run it under AWF
To regenerate the container siblings after recompiling smokes:
scripts/create-threat-detection-sibling-workflows.py
scripts/create-threat-detection-sibling-workflows.py --checkThe top-level smoke.yml workflow can be dispatched to start all six smokes at once.
Required Actions secrets/variables for smokes are documented in README.md → Development → AW Smoke Workflows.
Replay Workflow
.github/workflows/replay-detection.yml — manual dispatch to rerun detection against artifacts from a prior gh-aw run. Supports three detector sources (current, release, image), engine and model overrides, custom prompt injection, and AWF mode (use_awf=true). Uploads a sanitized replay-detection-<run_id> artifact with manifest, inventory, logs, replay result, and comparison to the original result. Uses the dispatching repo's GITHUB_TOKEN — no extra replay token needed. run_attempt is only safe for the latest attempt of a run.
Coding Guidance
When changing this repo:
- Spec first: behavior changes must align with
specs/threat-detection-spec.md. Update the spec when the contract changes. - Preserve the JSON result contract:
prompt_injection,secret_leak,malicious_patch,reasons— schema inpkg/detector/result.gois also enforced via/reflect. - Don't bundle engine CLIs into the image. Engines (Copilot, Claude, Codex) are invoked from
PATH. The image stays small; the runner provides the engine. - No new JS scripts. Detection setup and result parsing are Go. Old gh-aw JS detection scripts are being retired.
- Custom orchestrator steps (
threat-detection.steps) belong in thegh-awjob, not inside this container. - Prefer small, local packages and targeted tests (
pkg/<area>/<area>_test.go). - Use the skills in
skills/when writing console output (skills/console-rendering) or validation errors (skills/error-messages). - Don't fix unrelated issues in the same change.
Useful retained design references in scratchpad/: code-organization.md, validation-architecture.md, go-type-patterns.md, styles-guide.md, errors.md, testing.md, safe-outputs-specification.md, safe-output-environment-variables.md, safe-output-messages.md, artifact-naming-compatibility.md, security_review.md.
Engine Authentication
Unit tests, make build, make test, and make docker-smoke need no secrets. Real AI-backed detection needs:
| Variable | When |
COPILOT_GITHUB_TOKEN | --engine copilot — fine-grained PAT with Copilot Requests: Read; GITHUB_TOKEN is not sufficient |
ANTHROPIC_API_KEY | --engine claude |
OPENAI_API_KEY | --engine codex (or CODEX_API_KEY depending on CLI setup) |
WORKFLOW_NAME, WORKFLOW_DESCRIPTION, CUSTOM_PROMPT | Optional — folded into the prompt |
Debugging GitHub Actions Failures
When a CI workflow fails, always follow this order:
- Reproduce locally first — run the same
maketarget or script the workflow runs. - Identify the root cause — read logs, error messages, system state.
- Test the fix locally.
- Then update the workflow.
This avoids trial-and-error CI commits, which waste runner time and rarely fix the real cause.
For workflow-artifact triage there is a generic gh-aw helper (scripts/download-latest-artifact.sh) in sibling repos; this repo currently relies on gh run download <run-id> directly.
Agentic Workflow Firewall (AWF)
This section is reference material. AWF is a separate project (
github/gh-aw-firewall) — it is not built or developed in this repo. It is documented here because the threat detection job runs inside AWF ingh-aw-compiled workflows, and the*-container.lock.ymlsmoke siblings exercise that path. When debugging detection-job network behavior, reach for these facts.
Overview
awf (Agentic Workflow Firewall) is a Node.js CLI that wraps any command in a sandboxed Docker network. It provides L7 (HTTP/HTTPS) egress control using a Squid proxy, restricting network access to a whitelist of approved domains while giving the agent access to the host workspace and selected system paths via chroot and selective bind mounts.
Three Container Components
The system is orchestrated by AWF's src/cli.ts and managed by src/docker-manager.ts. There are three containers — two always required, one optional:
1. Squid Proxy (always required) — containers/squid/, IP 172.30.0.10
- Enforces domain ACL filtering for all HTTP/HTTPS traffic
- Config (
squid.conf) is generated bysrc/squid-config.tsand injected via base64 env varAWF_SQUID_CONFIG_B64(not a file bind mount — avoids Docker-in-Docker issues) - Agent container
depends_onSquid's healthcheck before starting
2. Agent (always required) — containers/agent/, IP 172.30.0.20
- Runs the user's command (e.g.,
claude,copilot,threat-detect,curl) - An iptables-init init container (
awf-iptables-init) shares the agent's network namespace and runssetup-iptables.shto redirect all port 80/443 traffic via DNAT to Squid before the user command starts entrypoint.shhandles UID/GID mapping, DNS config, chroot to/host, and capability drop (SYS_CHROOT,SYS_ADMINdropped before user code runs)- Selective bind mounts (not a blanket host FS mount): system binaries (
/usr,/bin,/sbin,/lib,/lib64,/opt,/sys,/dev) read-only; workspace and/tmpread-write; empty home volume with only whitelisted$HOMEsubdirs (.cache,.config,.local,.anthropic,.claude,.cargo,.rustup,.npm,.copilot); select/etcfiles (SSL certs,passwd,group,nsswitch.conf,ld.so.cache,alternatives,hosts— not/etc/shadow) - Sensitive API keys are NOT present in the agent environment when
--enable-api-proxyis active
3. API Proxy Sidecar (optional) — containers/api-proxy/, IP 172.30.0.30
- Enabled via
--enable-api-proxy - Injects real API credentials (OpenAI, Anthropic, Copilot) that the agent never sees
- Agent calls the sidecar with no auth (e.g.,
http://172.30.0.30:10001for Anthropic); sidecar injects the real key and forwards via Squid - Ports: 10000 (OpenAI), 10001 (Anthropic), 10002 (Copilot), 10003 (Gemini), 10004 (OpenCode) — discrete ports, not a contiguous range
Container Image Strategy
AWF pulls pre-built images from GHCR by default:
- Default:
ghcr.io/github/gh-aw-firewall/{squid,agent,api-proxy}:latest --build-localto build from source--image-registry <registry>and--image-tag <tag>for overrides
Traffic Flow
awf <flags> -- <command>
↓
CLI generates squid.conf (base64) + docker-compose.yml + seccomp profile in /tmp/awf-<ts>/
↓
Docker Compose: Squid (healthcheck) → [API Proxy (optional)] → Agent
→ iptables-init runs setup-iptables.sh (writes /ready)
↓
User command executes in Agent container (chrooted to /host)
↓
HTTPS (proxy-aware tools) → HTTPS_PROXY → Squid:3128 (CONNECT) → ACL → allowed or blocked
HTTPS (proxy-unaware) → iptables DNAT → Squid:3128 → TLS handshake rejected
HTTP → iptables DNAT → Squid:3128 → ACL → allowed or 403
API calls (optional) → http://172.30.0.30:1000x → API Proxy injects key → Squid → upstream
↓
docker compose down -v + rm /tmp/awf-<ts>/Domain Whitelisting
--allow-domainsvalues are normalized (protocol/trailing slash removed)github.commatchesgithub.comand.github.com(subdomains).github.commatches all subdomains- Squid denies anything not on the allowlist
DNS Configuration
--dns-servers <comma-separated IPs>(default8.8.8.8,8.8.4.4); IPv6 supported- Docker's internal
127.0.0.11is always allowed - Host-level iptables block DNS to non-whitelisted servers; container reads
AWF_DNS_SERVERS
Proxy Environment Variables Set in the Agent
HTTP_PROXY/HTTPS_PROXY— used by curl, wget, pip, npm, etc.SQUID_PROXY_HOST/SQUID_PROXY_PORT— raw values for tools that need themJAVA_TOOL_OPTIONS— JVM proxy system properties (Maven still needs~/.m2/settings.xml)http_proxy(lowercase) is intentionally not set — avoids httpoxy issues and keeps Squid 403 responses returning non-zero exit codes for security tests
ARC / DinD Split Filesystem Support
--docker-host— override Docker socket path (auto-detected fromDOCKER_HOST)--docker-host-path-prefix <prefix>— prefix all bind-mount source paths so a separate Docker daemon can resolve runner filesystem paths (kernel VFS/dev,/sys,/procand/dev/nullare passed through unprefixed)--enable-dind— expose the Docker socket inside the agent container
Logging (AWF)
- Squid logs (L7):
firewall_detailedlogformat — timestamp, client, host (SNI/Host), dest, method, status, decision (TCP_TUNNELallowed /TCP_DENIEDblocked), URL, UA.200=allowed,403=blocked. Logs are Unix timestamps. - iptables LOG rules (L3/L4):
[FW_BLOCKED_UDP]and[FW_BLOCKED_OTHER]prefixes with--log-uid. View viadmesg. awf logs statsandawf logs summary(formats:pretty,markdown,json) — pipeawf logs summary >> $GITHUB_STEP_SUMMARYin CI.
Exit Codes & Cleanup
AWF propagates the agent container's exit code via docker inspect. Use --keep-containers to preserve /tmp/awf-<ts>/ (squid.conf, docker-compose.yml, agent-logs/, squid-logs/) for debugging.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

