Download scripts/check_secrets.py from StillWorking/sentinel-face-recognition: direct link, hf CLI and curl.
- Browser
- Download file 6.47 kB
-
https://huggingface.co/StillWorking/sentinel-face-recognition/resolve/main/scripts/check_secrets.py
- Command line
-
hf download hf://StillWorking/sentinel-face-recognition/scripts/check_secrets.py
-
curl -L -o check_secrets.py https://huggingface.co/StillWorking/sentinel-face-recognition/resolve/main/scripts/check_secrets.py
6.47 kB
| """ | |
| scripts/check_secrets.py — Sentinel-AI | |
| ====================================== | |
| Fail fast when a live credential or an unignored generated artifact is about to | |
| be shared. | |
| Checks: | |
| 1. High-confidence cloud key patterns in source/docs (never prints the key). | |
| 2. ``.env`` files are present but covered by ``.gitignore``. | |
| 3. Generated index artifacts are covered by ``.gitignore``. | |
| Run from anywhere: python scripts/check_secrets.py | |
| Exit code 0 = clean, 1 = findings. | |
| """ | |
| from __future__ import annotations | |
| import fnmatch | |
| import os | |
| import re | |
| import sys | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parent.parent | |
| GITIGNORE = ROOT / ".gitignore" | |
| SKIP_DIRS = { | |
| ".git", | |
| ".venv", | |
| "venv", | |
| "env", | |
| "node_modules", | |
| "__pycache__", | |
| ".pytest_cache", | |
| ".mypy_cache", | |
| ".ruff_cache", | |
| ".tanstack", | |
| ".output", | |
| ".vite", | |
| ".wrangler", | |
| "dist", | |
| "dist-ssr", | |
| "build", | |
| "Data", | |
| "models_cache", | |
| "index_artifacts", | |
| "backup_fullimage", | |
| } | |
| TEXT_SUFFIXES = { | |
| ".py", ".pyi", ".ps1", ".sh", ".bat", ".cmd", ".ts", ".tsx", ".js", | |
| ".jsx", ".json", ".md", ".txt", ".yaml", ".yml", ".toml", ".ini", | |
| ".cfg", ".conf", ".env", ".example", ".html", ".css", ".sql", | |
| } | |
| # (label, compiled pattern). Patterns intentionally require realistic key | |
| # lengths so documentation placeholders are not flagged. | |
| PATTERNS = [ | |
| ("Google API key", re.compile(r"AIza[0-9A-Za-z_-]{35}")), | |
| ("Groq API key", re.compile(r"gsk_[0-9A-Za-z]{20,}")), | |
| ("OpenAI-style key", re.compile(r"\bsk-[0-9A-Za-z_-]{20,}")), | |
| ("AWS access key", re.compile(r"\bAKIA[0-9A-Z]{16}\b")), | |
| ("Cohere key assignment", re.compile( | |
| r"(?i)cohere[_-]?api[_-]?key\s*[=:]\s*['\"]?([0-9A-Za-z]{20,})" | |
| )), | |
| ("Generic secret assignment", re.compile( | |
| r"(?i)(?:api[_-]?key|secret|access[_-]?token|password)" | |
| r"\s*[=:]\s*['\"]([0-9A-Za-z_\-]{24,})['\"]" | |
| )), | |
| ] | |
| REQUIRED_IGNORES = [ | |
| ".env", | |
| "backend/.env", | |
| "backend/index_artifacts/celeb_index.faiss", | |
| "backend/index_artifacts/metadata.json", | |
| "backend/index_artifacts/celeb_index.order.json", | |
| "backend/index_artifacts/celeb_index.preprocessing.json", | |
| ] | |
| def _read_gitignore() -> list[str]: | |
| if not GITIGNORE.is_file(): | |
| return [] | |
| lines = [] | |
| for raw in GITIGNORE.read_text(encoding="utf-8").splitlines(): | |
| line = raw.strip() | |
| if not line or line.startswith("#"): | |
| continue | |
| lines.append(line) | |
| return lines | |
| def _is_ignored(relative_path: str, patterns: list[str]) -> bool: | |
| """Small gitignore evaluator (enough for this repository's patterns).""" | |
| ignored = False | |
| path = relative_path.replace("\\", "/").lstrip("./") | |
| for pattern in patterns: | |
| negated = pattern.startswith("!") | |
| candidate = pattern[1:] if negated else pattern | |
| candidate = candidate.rstrip("/") | |
| has_slash = "/" in candidate | |
| target = path if has_slash else Path(path).name | |
| if fnmatch.fnmatch(target, candidate) or fnmatch.fnmatch(path, candidate): | |
| ignored = not negated | |
| return ignored | |
| def _iter_files(): | |
| for dirpath, dirnames, filenames in os.walk(ROOT): | |
| dirnames[:] = [ | |
| name for name in dirnames | |
| if name not in SKIP_DIRS and not name.startswith(".git") | |
| ] | |
| for filename in filenames: | |
| path = Path(dirpath) / filename | |
| if path.stat().st_size > 2 * 1024 * 1024: | |
| continue | |
| if path.suffix.lower() in TEXT_SUFFIXES or filename in { | |
| "Dockerfile", "Makefile", "Procfile", "gitignore", | |
| }: | |
| yield path | |
| def _redact(value: str) -> str: | |
| return value[:4] + "..." + value[-2:] if len(value) > 8 else "***" | |
| def _scan_patterns() -> list[str]: | |
| findings: list[str] = [] | |
| for path in _iter_files(): | |
| relative = path.relative_to(ROOT).as_posix() | |
| # Real env files are supposed to contain secrets; they are checked for | |
| # ignore coverage below, not pattern-matched. | |
| if path.name == ".env" or path.name.startswith(".env.") and path.name != ".env.example": | |
| continue | |
| if path.name == ".env.example": | |
| continue | |
| try: | |
| text = path.read_text(encoding="utf-8", errors="ignore") | |
| except OSError: | |
| continue | |
| for line_number, line in enumerate(text.splitlines(), start=1): | |
| for label, pattern in PATTERNS: | |
| match = pattern.search(line) | |
| if not match: | |
| continue | |
| value = match.group(1) if match.groups() else match.group(0) | |
| findings.append( | |
| f"{relative}:{line_number}: {label} ({_redact(value)})" | |
| ) | |
| return findings | |
| def _check_ignores(patterns: list[str]) -> tuple[list[str], list[str]]: | |
| problems: list[str] = [] | |
| notes: list[str] = [] | |
| for relative in REQUIRED_IGNORES: | |
| if not _is_ignored(relative, patterns): | |
| problems.append(f"{relative} is NOT covered by .gitignore") | |
| for env_file in (ROOT / ".env", ROOT / "backend" / ".env"): | |
| if env_file.is_file(): | |
| relative = env_file.relative_to(ROOT).as_posix() | |
| if not _is_ignored(relative, patterns): | |
| problems.append(f"{relative} exists but is NOT covered by .gitignore") | |
| else: | |
| notes.append(f"{relative} present and gitignored") | |
| return problems, notes | |
| def main() -> int: | |
| patterns = _read_gitignore() | |
| findings = _scan_patterns() | |
| ignore_problems, notes = _check_ignores(patterns) | |
| status = 0 | |
| if findings: | |
| status = 1 | |
| print("[FAIL] Possible live credentials in non-env files:") | |
| for finding in findings: | |
| print(f" - {finding}") | |
| else: | |
| print("[OK] No high-confidence key patterns in source/docs.") | |
| if ignore_problems: | |
| status = 1 | |
| print("[FAIL] Ignore coverage:") | |
| for problem in ignore_problems: | |
| print(f" - {problem}") | |
| else: | |
| print("[OK] Required secrets/artifacts are covered by .gitignore.") | |
| for note in notes: | |
| print(f" note: {note}") | |
| if status: | |
| print("\nRotate any real key that was exposed, then remove it from the file.") | |
| print("Never commit .env or generated index artifacts.") | |
| return status | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |