|
| 1 | +#!/usr/bin/env bash |
| 2 | +# check-errors-logging.sh — exception chaining, log level, sensitive-info in messages. |
| 3 | +# Uses AST for chaining/swallow checks. |
| 4 | +set -euo pipefail |
| 5 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 6 | +source "$SCRIPT_DIR/lib/json-emit.sh" |
| 7 | +source "$SCRIPT_DIR/lib/skill-self-skip.sh" |
| 8 | + |
| 9 | +LANGUAGE="${LANGUAGE:-python}" |
| 10 | +DIFF_FILE="${DIFF_FILE:-/dev/stdin}" |
| 11 | +REPO_ROOT="${REPO_ROOT:-.}" |
| 12 | + |
| 13 | +STARTED=$(now_iso) |
| 14 | +findings=$(mktemp); trap 'rm -f "$findings"' EXIT |
| 15 | + |
| 16 | +diff_content=$(cat "$DIFF_FILE" 2>/dev/null || echo "") |
| 17 | + |
| 18 | +if [ "$LANGUAGE" = "python" ]; then |
| 19 | + changed=$(echo "$diff_content" | grep -oE '^\+\+\+ b/src/.*\.py' | sed 's|^+++ b/||' | sort -u) |
| 20 | + if [ -n "$changed" ]; then |
| 21 | + # AST-based chaining / swallow — only reports FLAGs when body ends non-Raise |
| 22 | + # shellcheck disable=SC2086 |
| 23 | + python3 "$SCRIPT_DIR/lib/ast_python_checks.py" el-01 $changed 2>/dev/null >> "$findings" || true |
| 24 | + # shellcheck disable=SC2086 |
| 25 | + python3 "$SCRIPT_DIR/lib/ast_python_checks.py" el-02 $changed 2>/dev/null >> "$findings" || true |
| 26 | + fi |
| 27 | + |
| 28 | + # EL-04: secret-like variable name in raise args (grep-based, added lines only) |
| 29 | + echo "$diff_content" | awk ' |
| 30 | + BEGIN { file=""; line=0 } |
| 31 | + /^diff --git a\// { file=$4; sub(/^b\//, "", file); line=0; next } |
| 32 | + /^@@/ { if (match($0, /\+[0-9]+/)) line=substr($0, RSTART+1, RLENGTH-1)+0; next } |
| 33 | + /^\+/ && !/^\+\+\+/ { print file "\t" line "\t" substr($0, 2); line++; next } |
| 34 | + /^ / { line++; next } |
| 35 | + ' | while IFS=$'\t' read -r file line_num content; do |
| 36 | + [ -z "$file" ] && continue |
| 37 | + if [ "$(is_skill_file "$file")" = "true" ]; then continue; fi |
| 38 | + # match raise ...({...token|secret|password|api_key|client_secret...}) |
| 39 | + if echo "$content" | grep -qE 'raise[[:space:]].*[fF]?"[^"]*\{[^}]*(token|secret|password|api_key|client_secret)[^}]*\}'; then |
| 40 | + emit_finding "EL-04" "BLOCK" "$file" "$line_num" \ |
| 41 | + "Exception message includes secret-like variable — leaks sensitive data" "" >> "$findings" |
| 42 | + fi |
| 43 | + done |
| 44 | +fi |
| 45 | + |
| 46 | +status=$(status_from_findings < "$findings") |
| 47 | +emit_report "errors-logging" "$LANGUAGE" "$status" "$STARTED" < "$findings" |
0 commit comments