#!/usr/bin/env bash set -euo pipefail readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" readonly PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)" readonly ENV_FILE="${AIXUN_DATAGOV_ENV_FILE:-${PROJECT_ROOT}/script/datagov/.env.datagov}" readonly RUNTIME_MODE="${1:-}" readonly LOCAL_BACKUP_ROOT="${PROJECT_ROOT}/.local-backups" fail() { echo "Secret scan failed: $*" >&2 exit 1 } [[ -z "${RUNTIME_MODE}" || "${RUNTIME_MODE}" == "--runtime" ]] || fail "usage: scan-secrets.sh [--runtime]" command -v rg >/dev/null 2>&1 && [[ -x "$(command -v rg)" ]] || fail "required executable rg is unavailable" verify_local_backup_metadata() { local match status [[ -e "${LOCAL_BACKUP_ROOT}" || -L "${LOCAL_BACKUP_ROOT}" ]] || return 0 [[ ! -L "${LOCAL_BACKUP_ROOT}" ]] || fail "local backup root must not be a symlink" [[ -d "${LOCAL_BACKUP_ROOT}" ]] || fail "local backup root must be a directory" if match="$(find "${LOCAL_BACKUP_ROOT}" -type l -print -quit 2>/dev/null)"; then : else status=$? fail "local backup symlink metadata check failed (exit ${status})" fi [[ -z "${match}" ]] || fail "local backup tree must not contain symlinks" if match="$( find "${LOCAL_BACKUP_ROOT}" \( -type d -o -type f \) \ \( -perm -004 -o -perm -002 -o -perm -001 -o \ -perm -040 -o -perm -020 -o -perm -010 \) \ -print -quit 2>/dev/null )"; then : else status=$? fail "local backup permission metadata check failed (exit ${status})" fi [[ -z "${match}" ]] || fail "local backup directories and files must deny group/other access" } verify_local_backup_metadata cd "${PROJECT_ROOT}" # A search miss is the only nonzero status that can be accepted as a clean scan. # Never expose raw diagnostics: they may contain a literal secret search value. rg_matches() { local status if rg "$@" >/dev/null 2>&1; then return 0 else status=$? fi [[ "${status}" -eq 1 ]] && return 1 fail "rg execution failed (exit ${status})" } if workspace_sensitive_files="$( find "${PROJECT_ROOT}" \ -path "${LOCAL_BACKUP_ROOT}" -prune -o \ -type f \ \( -name '.env' -o -name '.env.*' -o -name '*.pem' -o -name '*.key' -o -name '*.p12' -o -name '*.jks' -o -name '*.keystore' \) \ ! -path "${PROJECT_ROOT}/script/datagov/.env.datagov" \ ! -path "${PROJECT_ROOT}/script/datagov/runtime/*" \ ! -path '*/target/*' \ ! -path '*/node_modules/*' \ -print 2>/dev/null | sed "s#^${PROJECT_ROOT}/##" 2>/dev/null | { if rg -v '(^|/)\.env\.(example|template|development|production|test)$' 2>/dev/null; then : else status=$? [[ "${status}" -eq 1 ]] || exit "${status}" fi } )"; then : else fail "secret-bearing filename enumeration or filtering failed" fi [[ -z "${workspace_sensitive_files}" ]] || fail "reviewable secret-bearing filename detected: ${workspace_sensitive_files}" readonly SECRET_SHAPES=( '-----BEGIN (RSA |EC |OPENSSH )?PRIVATE KEY-----' 'AKIA[0-9A-Z]{16}' 'gh[pousr]_[A-Za-z0-9]{30,}' 'github_pat_[A-Za-z0-9_]{50,}' 'xox[baprs]-[A-Za-z0-9-]{20,}' 'AIza[0-9A-Za-z_-]{35}' 'npm_[A-Za-z0-9]{36}' ) for shape in "${SECRET_SHAPES[@]}"; do if rg_matches --hidden --no-messages -I -l -e "${shape}" \ -g '!.git/**' -g '!.local-backups/**' -g '!script/datagov/.env.datagov' -g '!script/datagov/runtime/**' \ -g '!**/target/**' -g '!aixun-ui/node_modules/**' . >/dev/null; then fail "high-confidence live-token/private-key shape detected" fi done secret_count=0 runtime_values=() if [[ -f "${ENV_FILE}" ]]; then while IFS= read -r line || [[ -n "${line}" ]]; do [[ "${line}" =~ ^[A-Z0-9_]+= ]] || continue name="${line%%=*}" value="${line#*=}" case "${name}" in AIXUN_DATAGOV_DB_PASSWORD|AIXUN_DATAGOV_MYSQL_ROOT_PASSWORD|AIXUN_DATAGOV_REDIS_PASSWORD|AIXUN_DATAGOV_NACOS_PASSWORD|AIXUN_DATAGOV_NACOS_AUTH_TOKEN|AIXUN_DATAGOV_NACOS_IDENTITY_KEY|AIXUN_DATAGOV_NACOS_IDENTITY_VALUE|AIXUN_DATAGOV_JWT_SECRET|AIXUN_DATAGOV_ADMIN_PASSWORD|AIXUN_DATAGOV_UAT_PASSWORD) [[ ${#value} -ge 12 ]] || fail "local secret ${name} is unexpectedly short" runtime_values+=("${value}") if rg_matches --hidden --no-messages -I -l -F \ -g '!.git/**' -g '!.local-backups/**' -g '!script/datagov/.env.datagov' -g '!script/datagov/runtime/**' \ -g '!**/target/**' -g '!aixun-ui/node_modules/**' -- "${value}" . >/dev/null; then fail "local secret value for ${name} appears in reviewable workspace content" fi ((secret_count += 1)) ;; esac done < "${ENV_FILE}" fi if [[ "${RUNTIME_MODE}" == "--runtime" ]]; then [[ ${#runtime_values[@]} -gt 0 ]] || fail "runtime scan requires the local DataGov env file" temp_dir="$(mktemp -d "${TMPDIR:-/tmp}/aixun-datagov-secret-scan.XXXXXX")" trap 'rm -rf -- "${temp_dir}"' EXIT readonly EXPECTED_CONTAINERS=(mysql redis nacos gateway auth system resource integration frontend) for service_name in "${EXPECTED_CONTAINERS[@]}"; do container_name="aixun-datagov-${service_name}" project_label="$(docker inspect "${container_name}" --format '{{index .Config.Labels "com.docker.compose.project"}}' 2>/dev/null)" || fail "owned runtime container ${container_name} is unavailable" owner_label="$(docker inspect "${container_name}" --format '{{index .Config.Labels "com.aixun.project"}}')" [[ "${project_label}" == "aixun-datagov-local" && "${owner_label}" == "aixun-datagov" ]] || fail "refusing to read logs from an unowned container" docker logs "${container_name}" >"${temp_dir}/${service_name}.log" 2>&1 || fail "owned runtime log input is unavailable" for value in "${runtime_values[@]}"; do if rg_matches -F -l -- "${value}" "${temp_dir}/${service_name}.log" >/dev/null; then fail "known DataGov secret appears in ${container_name} logs" fi done done fi echo "Secret scan passed: secret-shaped reviewable files=0, exact local-secret hits=0, local secret references checked=${secret_count}, runtime_mode=${RUNTIME_MODE:---none}."