spaceforge / backend /app /security /secretscan.py
2Btainment's picture
UI v2: analyze manifest, mode radio, target cards, compat matrix, progress/log/download; backward-compatible API
bc2711b verified
Raw History Blame Contribute Delete
1.05 kB
"""Refuse to ship artifacts that contain token-like strings (HF / OpenAI / generic bearer)."""
from __future__ import annotations
import re
import tarfile
import zipfile
from pathlib import Path
PATTERNS = re.compile(rb"\b(hf_[A-Za-z0-9]{30,}|sk-[A-Za-z0-9_-]{32,}|ghp_[A-Za-z0-9]{36})\b")
MAX_MEMBER = 2_000_000
def find_secrets(path: Path) -> list[str]:
hits: list[str] = []
try:
if zipfile.is_zipfile(path):
with zipfile.ZipFile(path) as z:
for i in z.infolist():
if i.file_size <= MAX_MEMBER and PATTERNS.search(z.read(i)):
hits.append(i.filename)
elif path.name.endswith((".tar.gz", ".tgz")):
with tarfile.open(path) as t:
for m in t.getmembers():
if m.isfile() and m.size <= MAX_MEMBER:
f = t.extractfile(m)
if f and PATTERNS.search(f.read()):
hits.append(m.name)
except Exception:
pass
return hits