|
| 1 | +#!/usr/bin/env bash |
| 2 | +# audit-install-scripts.sh — Static quality gate for installation/startup scripts. |
| 3 | +# Run from anywhere: bash scripts/audit-install-scripts.sh |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 7 | +ERRORS=0 |
| 8 | +WARNINGS=0 |
| 9 | + |
| 10 | +fail() { printf '[FAIL] %s\n' "$*" >&2; ERRORS=$((ERRORS + 1)); } |
| 11 | +warn() { printf '[WARN] %s\n' "$*" >&2; WARNINGS=$((WARNINGS + 1)); } |
| 12 | + |
| 13 | +printf '=== NWE installation/startup script audit ===\n' |
| 14 | +printf 'Repository: %s\n\n' "$ROOT" |
| 15 | + |
| 16 | +while IFS= read -r -d '' file; do |
| 17 | + rel="${file#"$ROOT/"}" |
| 18 | + |
| 19 | + if ! head -n 1 "$file" | grep -Eq '^#!.*(ba)?sh([[:space:]]|$)'; then |
| 20 | + warn "$rel: missing bash-compatible shebang" |
| 21 | + fi |
| 22 | + |
| 23 | + if ! bash -n "$file"; then |
| 24 | + fail "$rel: shell syntax error" |
| 25 | + continue |
| 26 | + fi |
| 27 | + |
| 28 | + # Installation scripts should be independent of the caller's cwd. |
| 29 | + if grep -Eq '(^|[[:space:]])cd[[:space:]]+([./]|\.\.)' "$file"; then |
| 30 | + warn "$rel: contains a relative cd; prefer script-derived absolute paths" |
| 31 | + fi |
| 32 | + |
| 33 | + # Flag common committed/default credentials for manual remediation. |
| 34 | + if grep -EInq "(storepass[[:space:]]+(changeit|password)|IDENTIFIED BY[[:space:]]+'[^']+'|MYSQL_PASS[[:space:]]*=[[:space:]]*['\"])" "$file"; then |
| 35 | + fail "$rel: possible hard-coded/default credential" |
| 36 | + fi |
| 37 | + |
| 38 | + # Avoid hiding operational failures in installers. |
| 39 | + if grep -Eq '\|\|[[:space:]]*(true|echo)' "$file" && [[ "$rel" == *install* || "$rel" == *deploy* ]]; then |
| 40 | + warn "$rel: installer/deployer suppresses a command failure" |
| 41 | + fi |
| 42 | + |
| 43 | +done < <(find "$ROOT" -type f \( -name '*.sh' -o -name '*.bash' \) -print0 \ |
| 44 | + ! -path '*/.git/*' \ |
| 45 | + ! -path '*/node_modules/*') |
| 46 | + |
| 47 | +printf '\n=== Audit summary ===\n' |
| 48 | +printf 'Warnings: %d\n' "$WARNINGS" |
| 49 | +printf 'Errors: %d\n' "$ERRORS" |
| 50 | + |
| 51 | +if (( ERRORS > 0 )); then |
| 52 | + printf 'Audit FAILED. Fix errors before release/install.\n' >&2 |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +printf 'Audit passed; warnings are review items.\n' |
0 commit comments