#!/usr/bin/env bash # Nightly Codex Security scan for the exe.dev Codex Security recipe: # https://exe.dev/deploy/codex-security # # Reads owner/repo lines from ~/security/repos.txt, scans every repository # whose default branch moved since its last completed scan, with # OpenAI's codex-security container and exe.dev's LLM integration, and # emails the VM's owner a summary of findings it hasn't reported before. set -euo pipefail cd "$(dirname "$(readlink -f "$0")")" # Settings. Override any of them in ~/security/scan.env. MODEL=gpt-5.6-terra # any OpenAI model the LLM integration serves EFFORT=medium # low, medium, high or xhigh MAX_COST=5 # estimated USD limit per repository MAX_TOTAL_COST=20 # estimated USD limit per night WORKERS=2 # repositories scanned at once GIT_BASE=https://github.int.exe.xyz # exe.dev GitHub integration EMAIL=yes # email the owner a summary [ -f scan.env ] && . ./scan.env export CODEX_SECURITY_IMAGE=${CODEX_SECURITY_IMAGE:-ghcr.io/openai/codex-security:0.10.0} export CODEX_SECURITY_USER="$(id -u):$(id -g)" run=$(date -u +%Y-%m-%dT%H%M) out=results/runs/$run mkdir -p "$out" results/last-scanned export CODEX_SECURITY_CSV=$PWD/$out/repositories.csv echo id,repository,revision >"$CODEX_SECURITY_CSV" pending=0 while read -r repo _; do case $repo in '' | \#*) continue ;; esac id=${repo//\//__} url=$GIT_BASE/$repo.git if ! sha=$(git ls-remote "$url" HEAD | cut -f1) || [ -z "$sha" ]; then echo "$repo: can't read $url; is the GitHub integration attached?" >&2 continue fi if [ "$(cat "results/last-scanned/$id" 2>/dev/null)" = "$sha" ]; then echo "$repo: no new commits since the last scan of ${sha:0:12}" continue fi echo "$id,$url,$sha" >>"$CODEX_SECURITY_CSV" pending=$((pending + 1)) done "$out/summary.txt" <<'PY' import json, os, sys out, status = sys.argv[1], int(sys.argv[2]) seen_path = "results/seen-findings.txt" seen = set(open(seen_path).read().split()) if os.path.exists(seen_path) else set() rows = [] if os.path.exists(f"{out}/results.jsonl"): rows = [json.loads(l) for l in open(f"{out}/results.jsonl") if l.strip()] revs = {} for line in list(open(f"{out}/repositories.csv"))[1:]: i, _, rev = line.strip().split(",")[:3] revs[i] = rev lines, new, total_cost = [], [], 0.0 for r in rows: repo = r["id"].replace("__", "/") cost = (r.get("cost") or {}).get("estimatedUsd") or 0 total_cost += cost lines.append(f"{repo}: {r['status']} (${cost:.2f})") if r["status"] != "completed": continue open(f"results/last-scanned/{r['id']}", "w").write(revs.get(r["id"], "")) host_dir = r["outputDir"].replace("/output/", "results/", 1) path = os.path.join(host_dir, "findings.json") if not os.path.exists(path): continue for f in json.load(open(path)).get("findings", []): fp = f.get("fingerprints", {}).get("primary") or f.get("findingId") if fp in seen: continue seen.add(fp) loc = (f.get("locations") or [{}])[0] sev = (f.get("severity") or {}).get("level", "?") new.append(f"[{sev}] {repo} {loc.get('path', '')}:{loc.get('startLine', '')}\n" f" {f.get('title', '')}\n {os.path.abspath(host_dir)}/report.md") open(seen_path, "w").write("\n".join(sorted(seen)) + "\n") print(f"Codex Security run {os.path.basename(out)}: {len(rows)} repositories, " f"about ${total_cost:.2f}, {len(new)} new findings.") if status: print(f"bulk-scan exited with status {status}; see journalctl -u codex-security-scan.") print() print("\n".join(lines)) if new: print("\nNew findings:\n" + "\n".join(new)) PY cat "$out/summary.txt" if [ "$EMAIL" = yes ]; then owner=$(curl -fsS https://reflection.int.exe.xyz/email | jq -r .email) jq -n --arg to "$owner" --arg subject "Security scan: $(head -1 "$out/summary.txt")" \ --rawfile body "$out/summary.txt" '{to: $to, subject: $subject, body: $body}' | curl -fsS -X POST http://169.254.169.254/gateway/email/send \ -H 'Content-Type: application/json' --data-binary @- >/dev/null || echo "Couldn't send the summary email." >&2 fi exit "$status"