|
| 1 | +name: No AI traces and secrets |
| 2 | + |
| 3 | +# One quick job on every pull request and every push to main. It fails when |
| 4 | +# - a commit in the change has an AI author or committer, an AI Co-Authored-By trailer, a |
| 5 | +# Claude-Session trailer, a "Generated with Claude Code" footer or a merge message naming a |
| 6 | +# claude/ branch, or a pull request comes from a claude/ branch; |
| 7 | +# - an AI-agent file is tracked (CLAUDE.md, AGENTS.md, .mcp.json, .cursorrules, .claude/, |
| 8 | +# .claude-plugin/, .swarm/, .claude-flow/); |
| 9 | +# - a tracked file looks like a secret: a keystore or key file, local.properties, a .env file, a |
| 10 | +# private key block or a token-shaped string. End a deliberate example line with |
| 11 | +# "secret-scan: allow". |
| 12 | +# An AI footer in a pull request's description only gets a warning: it never reaches the history. |
| 13 | +# It uses no actions (plain git and Python), so it runs whatever the repository's Actions policy. |
| 14 | +on: |
| 15 | + pull_request: |
| 16 | + push: |
| 17 | + branches: [main] |
| 18 | + |
| 19 | +permissions: |
| 20 | + contents: read |
| 21 | + |
| 22 | +# A newer push to the same pull request cancels the older run; pushes each get their own group. |
| 23 | +concurrency: |
| 24 | + group: repo-checks-${{ github.event_name == 'pull_request' && github.ref || github.run_id }} |
| 25 | + cancel-in-progress: true |
| 26 | + |
| 27 | +jobs: |
| 28 | + check: |
| 29 | + name: check (AI traces, secrets) |
| 30 | + runs-on: ubuntu-24.04 |
| 31 | + timeout-minutes: 5 |
| 32 | + env: |
| 33 | + TOKEN: ${{ github.token }} |
| 34 | + EVENT: ${{ github.event_name }} |
| 35 | + PR_BASE: ${{ github.event.pull_request.base.sha }} |
| 36 | + PR_HEAD: ${{ github.event.pull_request.head.sha }} |
| 37 | + BEFORE: ${{ github.event.before }} |
| 38 | + PR_BODY: ${{ github.event.pull_request.body }} |
| 39 | + HEAD_REF: ${{ github.head_ref }} |
| 40 | + steps: |
| 41 | + - name: Fetch the change |
| 42 | + run: | |
| 43 | + set -euo pipefail |
| 44 | + git init -q repo && cd repo |
| 45 | + git remote add origin "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git" |
| 46 | + git config http.extraheader "AUTHORIZATION: basic $(printf 'x-access-token:%s' "$TOKEN" | base64 -w0)" |
| 47 | + if [ "$EVENT" = pull_request ]; then head=$PR_HEAD; base=$PR_BASE; else head=$GITHUB_SHA; base=$BEFORE; fi |
| 48 | + git fetch -q --no-tags --filter=blob:none origin "$head" |
| 49 | + range="$head~1..$head" |
| 50 | + if [ -n "$base" ] && [ "$base" != 0000000000000000000000000000000000000000 ] \ |
| 51 | + && git fetch -q --no-tags --filter=blob:none origin "$base" 2>/dev/null; then |
| 52 | + range="$base..$head" |
| 53 | + fi |
| 54 | + git rev-parse -q --verify "$head~1" >/dev/null || range="$head" |
| 55 | + git checkout -q --detach "$head" |
| 56 | + echo "RANGE=$range" >> "$GITHUB_ENV" |
| 57 | +
|
| 58 | + - name: Check commits, files and secrets |
| 59 | + working-directory: repo |
| 60 | + run: | |
| 61 | + python3 - <<'PY' |
| 62 | + import os, re, subprocess, sys |
| 63 | + from pathlib import Path |
| 64 | +
|
| 65 | + AI = r"claude|anthropic|copilot|cursor|chatgpt|openai|codex|devin|gemini" |
| 66 | + AI_PEOPLE = re.compile(AI, re.I) |
| 67 | + AI_LINE = re.compile(r"^\s*(?:co-authored-by:.*(?:%s)|claude-session:|.*generated (?:with|by) \[claude code\]\(|.*claude\.ai/code|merge\b.*\bclaude/)" % AI, re.I) |
| 68 | + AI_FILES = re.compile(r"^(CLAUDE\.md|AGENTS\.md|\.mcp\.json|\.cursorrules)$|^\.(claude|claude-plugin|swarm)/|(^|/)\.claude-flow/") |
| 69 | + BAD_NAMES = [ |
| 70 | + (re.compile(r"(^|/)[^/]+\.(jks|keystore|p12|pfx)$", re.I), "signing keystore"), |
| 71 | + (re.compile(r"(^|/)[^/]+\.(pem|key)$", re.I), "key file"), |
| 72 | + (re.compile(r"(^|/)local\.properties$"), "Android local.properties"), |
| 73 | + (re.compile(r"(^|/)\.env(\.[\w-]+)?$"), ".env file"), |
| 74 | + (re.compile(r"(^|/)id_(rsa|ed25519|ecdsa)$"), "SSH private key"), |
| 75 | + ] |
| 76 | + NAME_OK = re.compile(r"(^|/)\.env\.(example|sample|template)$|(^|/)example\.env$") |
| 77 | + TOKENS = [ |
| 78 | + (re.compile(r"-----BEGIN (?:[A-Z]+ )?PRIVATE KEY-----"), "private key block"), |
| 79 | + (re.compile(r"\bgh[pousr]_[A-Za-z0-9]{36,}\b"), "GitHub token"), |
| 80 | + (re.compile(r"\bgithub_pat_[A-Za-z0-9_]{60,}\b"), "GitHub fine-grained token"), |
| 81 | + (re.compile(r"\bAKIA[0-9A-Z]{16}\b"), "AWS access key"), |
| 82 | + (re.compile(r"\bAIza[0-9A-Za-z_-]{35}\b"), "Google API key"), |
| 83 | + (re.compile(r"\bxox[abprs]-[A-Za-z0-9-]{10,}\b"), "Slack token"), |
| 84 | + (re.compile(r"\bhf_[A-Za-z0-9]{34,}\b"), "Hugging Face token"), |
| 85 | + (re.compile(r"\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{32,}\b"), "API secret key"), |
| 86 | + ] |
| 87 | + ALLOW = "secret-scan: allow" |
| 88 | +
|
| 89 | + def git(*a): |
| 90 | + return subprocess.run(["git", *a], check=True, capture_output=True, text=True).stdout |
| 91 | +
|
| 92 | + errors = [] |
| 93 | + log = git("log", "--format=%H%x00%an <%ae>%x00%cn <%ce>%x00%B%x01", os.environ["RANGE"]) |
| 94 | + for entry in filter(None, (e.strip("\n") for e in log.split("\x01"))): |
| 95 | + sha, author, committer, body = entry.split("\0", 3) |
| 96 | + for who in {author, committer}: |
| 97 | + if AI_PEOPLE.search(who): |
| 98 | + errors.append(f"commit {sha[:9]}: AI identity {who}") |
| 99 | + for line in body.splitlines(): |
| 100 | + if AI_LINE.match(line): |
| 101 | + errors.append(f"commit {sha[:9]}: {line.strip()}") |
| 102 | +
|
| 103 | + files = [p for p in git("ls-files", "-z").split("\0") if p] |
| 104 | + for rel in files: |
| 105 | + if AI_FILES.search(rel): |
| 106 | + errors.append(f"{rel}: AI-agent file") |
| 107 | + if not NAME_OK.search(rel): |
| 108 | + errors += [f"{rel}: {what}" for pattern, what in BAD_NAMES if pattern.search(rel)] |
| 109 | + path = Path(rel) |
| 110 | + if not path.is_file() or path.is_symlink() or path.stat().st_size > 2_000_000: |
| 111 | + continue |
| 112 | + data = path.read_bytes() |
| 113 | + if b"\0" in data[:8192]: |
| 114 | + continue |
| 115 | + for n, line in enumerate(data.decode("utf-8", "replace").splitlines(), 1): |
| 116 | + if ALLOW not in line: |
| 117 | + errors += [f"{rel}:{n}: {what}" for pattern, what in TOKENS if pattern.search(line)] |
| 118 | +
|
| 119 | + if os.environ.get("EVENT") == "pull_request": |
| 120 | + body = os.environ.get("PR_BODY") or "" |
| 121 | + if (os.environ.get("HEAD_REF") or "").lower().startswith("claude/"): |
| 122 | + errors.append(f"branch {os.environ['HEAD_REF']}: claude/ branch names end up in merge messages; use a neutral name") |
| 123 | + if re.search(r"generated (with|by) \[claude code\]\(|claude\.ai/code|co-authored-by:.*(claude|anthropic)", body, re.I): |
| 124 | + print("::warning::The pull request description has an AI attribution line; remove it.") |
| 125 | +
|
| 126 | + for e in errors: |
| 127 | + print(f"::error::{e}") |
| 128 | + print(f"{len(errors)} problem(s) in {len(files)} files and the commits {os.environ['RANGE']}.") |
| 129 | + sys.exit(1 if errors else 0) |
| 130 | + PY |