|
| 1 | +#!/usr/bin/env bash |
| 2 | +# check-secrets.sh — detect secrets (AWS keys, JWTs, GitHub tokens, private keys, etc.) in added lines. |
| 3 | +# All SEC-* rules are BLOCK_LOCKED (cannot be suppressed). |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 7 | +# shellcheck source=lib/json-emit.sh |
| 8 | +source "$SCRIPT_DIR/lib/json-emit.sh" |
| 9 | +# shellcheck source=lib/skill-self-skip.sh |
| 10 | +source "$SCRIPT_DIR/lib/skill-self-skip.sh" |
| 11 | + |
| 12 | +LANGUAGE="${LANGUAGE:-python}" |
| 13 | +DIFF_FILE="${DIFF_FILE:-/dev/stdin}" |
| 14 | + |
| 15 | +STARTED=$(now_iso) |
| 16 | + |
| 17 | +# Read diff (either from env-provided file or stdin) — extract added lines with file+line info |
| 18 | +diff_content=$(cat "$DIFF_FILE" 2>/dev/null || echo "") |
| 19 | + |
| 20 | +if [ -z "$diff_content" ]; then |
| 21 | + emit_report "secrets" "$LANGUAGE" "PASS" "$STARTED" <<< "" |
| 22 | + exit 0 |
| 23 | +fi |
| 24 | + |
| 25 | +findings=$(mktemp) |
| 26 | +trap 'rm -f "$findings"' EXIT |
| 27 | + |
| 28 | +# Parse diff and scan each added line |
| 29 | +echo "$diff_content" | awk ' |
| 30 | + BEGIN { file=""; line=0 } |
| 31 | + /^diff --git a\// { |
| 32 | + file=$4 |
| 33 | + sub(/^b\//, "", file) |
| 34 | + line=0 |
| 35 | + next |
| 36 | + } |
| 37 | + /^@@/ { |
| 38 | + if (match($0, /\+[0-9]+/)) line=substr($0, RSTART+1, RLENGTH-1)+0 |
| 39 | + next |
| 40 | + } |
| 41 | + /^\+/ && !/^\+\+\+/ { |
| 42 | + print file "\t" line "\t" substr($0, 2) |
| 43 | + line++ |
| 44 | + next |
| 45 | + } |
| 46 | + /^ / { line++; next } |
| 47 | +' | while IFS=$'\t' read -r file line_num content; do |
| 48 | + [ -z "$file" ] && continue |
| 49 | + # Self-review protection: skip skill files |
| 50 | + if [ "$(is_skill_file "$file")" = "true" ]; then continue; fi |
| 51 | + |
| 52 | + # SEC-01: AWS Access Key |
| 53 | + if echo "$content" | grep -qE 'AKIA[0-9A-Z]{16}'; then |
| 54 | + emit_finding "SEC-01" "BLOCK" "$file" "$line_num" "AWS Access Key detected — remove immediately and rotate the key" "" >> "$findings" |
| 55 | + fi |
| 56 | + # SEC-02: Google API Key |
| 57 | + if echo "$content" | grep -qE 'AIza[0-9A-Za-z_-]{35}'; then |
| 58 | + emit_finding "SEC-02" "BLOCK" "$file" "$line_num" "Google API Key detected — remove and rotate" "" >> "$findings" |
| 59 | + fi |
| 60 | + # SEC-03: GitHub PAT |
| 61 | + if echo "$content" | grep -qE 'gh[pousr]_[A-Za-z0-9_]{36,}'; then |
| 62 | + emit_finding "SEC-03" "BLOCK" "$file" "$line_num" "GitHub PAT detected — remove and rotate" "" >> "$findings" |
| 63 | + fi |
| 64 | + # SEC-04: OpenAI key |
| 65 | + if echo "$content" | grep -qE 'sk-[A-Za-z0-9]{20,}'; then |
| 66 | + emit_finding "SEC-04" "BLOCK" "$file" "$line_num" "OpenAI-like API key detected — remove and rotate" "" >> "$findings" |
| 67 | + fi |
| 68 | + # SEC-05: Slack bot token |
| 69 | + if echo "$content" | grep -qE 'xox[baprs]-[A-Za-z0-9-]+'; then |
| 70 | + emit_finding "SEC-05" "BLOCK" "$file" "$line_num" "Slack token detected — remove and rotate" "" >> "$findings" |
| 71 | + fi |
| 72 | + # SEC-06: JWT |
| 73 | + if echo "$content" | grep -qE 'eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}'; then |
| 74 | + emit_finding "SEC-06" "BLOCK" "$file" "$line_num" "JWT token detected — remove and rotate" "" >> "$findings" |
| 75 | + fi |
| 76 | + # SEC-07: Private key header |
| 77 | + if echo "$content" | grep -qE 'BEGIN (RSA |EC |OPENSSH |DSA |ENCRYPTED )?PRIVATE KEY'; then |
| 78 | + emit_finding "SEC-07" "BLOCK" "$file" "$line_num" "Private key header detected — remove and rotate" "" >> "$findings" |
| 79 | + fi |
| 80 | + # SEC-08: BTP client_secret literal (heuristic: assignment with looks-like-secret value) |
| 81 | + # Match client_secret= or clientsecret= with quoted values that look like real secrets |
| 82 | + if echo "$content" | grep -qE '"clientsecret"\s*:\s*"[A-Za-z0-9+/=]{20,}"'; then |
| 83 | + emit_finding "SEC-08" "BLOCK" "$file" "$line_num" "BTP client_secret literal detected — use secret resolver" "" >> "$findings" |
| 84 | + fi |
| 85 | +done |
| 86 | + |
| 87 | +# SEC-10: .env files in diff (not .env.example, .env.test, .env.sample) |
| 88 | +env_lines=$(echo "$diff_content" | grep -E '^\+\+\+ b/\.env(\..+)?$' || true) |
| 89 | +while IFS= read -r line; do |
| 90 | + [ -z "$line" ] && continue |
| 91 | + # extract path from "+++ b/<path>" |
| 92 | + path="${line#+++ b/}" |
| 93 | + # skip allowed suffixes |
| 94 | + case "$path" in |
| 95 | + .env.example|.env.test|.env.sample|.env.template) continue ;; |
| 96 | + .env|.env.*) emit_finding "SEC-10" "BLOCK" "$path" 1 ".env file committed — never commit .env; use .env.example" "" >> "$findings" ;; |
| 97 | + esac |
| 98 | +done <<< "$env_lines" |
| 99 | + |
| 100 | +status=$(status_from_findings < "$findings") |
| 101 | +emit_report "secrets" "$LANGUAGE" "$status" "$STARTED" < "$findings" |
0 commit comments