Repository Public MIT
1#!/usr/bin/env bash2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : ORIX Astra8# File : Scripts/secret-scan.sh9# Module : Scripts10# Repository : https://git.spboucher.ai/orix-astra11# Created : 2026-11-2112# Last Modified : 2026-11-2113#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: support@spboucher.ai22# Technical Contact: dev@spboucher.ai23# Security/Admin : admin@spboucher.ai24#25# Copyright (c) 202626# Simon-Pierre Boucher27# All rights reserved where applicable. This file is governed by the28# applicable project license, copyright notices and relevant law.29# Existing third-party rights and notices remain unaffected.30#31# License : Proprietary, all rights reserved (see license file)32# License File : LICENSE33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041# Scans the files tracked or staged in git for credentials and private material. Exits non-zero on a finding.42# With a path to a built .app as argument, also scans the app bundle's files for key-shaped strings.43set -uo pipefail44cd "$(dirname "$0")/.."45status=046files=$(git ls-files; git diff --cached --name-only)47files=$(printf '%s\n' "$files" | sort -u)4849# 1. File types that must never be committed50bad=$(printf '%s\n' "$files" | grep -E '\.(p8|p12|pem|key|cer|mobileprovision|provisionprofile|keychain|env)$|(^|/)\.env(\.|$)|AuthKey_|id_(rsa|ed25519)' || true)51if [[ -n "$bad" ]]; then echo "FORBIDDEN FILES:"; echo "$bad"; status=1; fi5253# 2. Content patterns (private keys, tokens, passwords)54patterns='-----BEGIN [A-Z ]*PRIVATE KEY-----|AKIA[0-9A-Z]{16}|sk-[A-Za-z0-9_-]{24,}|ghp_[A-Za-z0-9]{30,}|xox[baprs]-[A-Za-z0-9-]{10,}|eyJ[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{20,}\.|(password|passwd|secret|api[_-]?key|token)["'"'"' ]*[:=] *["'"'"'][^"'"'"' ]{8,}'55hits=$(printf '%s\n' "$files" | while read -r f; do56 [[ -f "$f" ]] || continue57 case "$f" in (*.png|*.jpg|*.pdf|*.ipa|*.svg|Scripts/secret-scan.sh|Tests/*|docs/RESEARCH.md|docs/PROVIDER_MATRIX.md) continue ;; esac58 grep -nIE -i "$patterns" "$f" 2>/dev/null | sed "s|^|$f:|"59done)60if [[ -n "$hits" ]]; then echo "SUSPICIOUS CONTENT:"; echo "$hits" | cut -c1-200; status=1; fi6162# 3. Build products63built=$(printf '%s\n' "$files" | grep -E '(^|/)(\.build|build|DerivedData|dist)/|\.xcarchive|\.ipa$' || true)64if [[ -n "$built" ]]; then echo "BUILD PRODUCTS TRACKED:"; echo "$built" | head; status=1; fi6566# 4. Tests and docs are scanned for REAL key shapes only (they contain fake ones on purpose): long provider-prefixed tokens67real='sk-ant-api[0-9]{2}-[A-Za-z0-9_-]{80,}|sk-proj-[A-Za-z0-9_-]{100,}|sk-or-v1-[a-f0-9]{64}|xai-[A-Za-z0-9]{70,}|AIza[A-Za-z0-9_-]{35}'68deep=$(printf '%s\n' "$files" | while read -r f; do [[ -f "$f" ]] || continue; grep -nIEo "$real" "$f" 2>/dev/null | grep -vE "LEAKCHECK|FIXTURE" | sed -E "s|^([0-9]+):.{12}.*|$f:\1: key-shaped string|"; done)69if [[ -n "$deep" ]]; then echo "KEY-SHAPED STRINGS:"; echo "$deep" | head -20; status=1; fi7071# 4b. When the development keys are loaded in the environment (source Scripts/smoke-env.sh), search for their exact values.72for v in OPENAI_API_KEY ANTHROPIC_API_KEY OPENROUTER_API_KEY XAI_API_KEY GEMINI_API_KEY ASTRA_CUSTOM_KEY; do73 k="${!v:-}"; [[ -n "$k" ]] || continue74 n=$(printf '%s\n' "$files" | while read -r f; do [[ -f "$f" ]] && grep -lF -- "$k" "$f" 2>/dev/null; done | wc -l | tr -d ' ')75 if [[ "$n" != 0 ]]; then echo "REAL KEY VALUE ($v) FOUND IN $n FILE(S)"; status=1; fi76done7778# 5. Optional: a built app bundle79if [[ -n "${1:-}" && -d "$1" ]]; then80 app=$(grep -rIlE "$real" "$1" 2>/dev/null; strings -a "$1"/ORIXAstra 2>/dev/null | grep -cE "$real" | sed 's/^0$//')81 if [[ -n "$app" ]]; then echo "KEY-SHAPED STRINGS IN APP BUNDLE: $app"; status=1; else echo "app bundle: no key-shaped string"; fi82fi8384count=$(printf '%s\n' "$files" | grep -c . )85[[ $status == 0 ]] && echo "secret scan: clean ($count files)" || echo "secret scan: FINDINGS"86exit $status87