This repository has been archived on 2026-11-25. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
MeltMind-Archive/handover/collab/upload_preflight.py
T

58 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Upload preflight: size and secret checks for a candidate file list (stdlib only).
Usage: python3 upload_preflight.py ROOT CANDIDATES.txt REPORT.json [--max-file-mb 5] [--max-total-mb 200]
CANDIDATES.txt: one path per line, relative to ROOT. Exit 0 = pass, 1 = blocked.
Never prints secret values; reports only file, line number and pattern name.
"""
import json, os, re, sys, hashlib
PATTERNS = {
'private_key': re.compile(rb'-----BEGIN (?:RSA |EC |OPENSSH |DSA )?PRIVATE KEY-----'),
'aws_access_key': re.compile(rb'AKIA[0-9A-Z]{16}'),
'signed_url': re.compile(rb'(?i)(x-amz-signature|signature=|expires=\d{9,}&|ossaccesskeyid=)'),
'bearer_token': re.compile(rb'(?i)authorization:\s*bearer\s+[a-z0-9._-]{20,}'),
'github_token': re.compile(rb'gh[pousr]_[A-Za-z0-9]{30,}'),
'slack_token': re.compile(rb'xox[abposr]-[A-Za-z0-9-]{10,}'),
'password_assign': re.compile(rb'(?i)\b(password|passwd|api[_-]?key|secret[_-]?key|access[_-]?token)\s*[:=]\s*["\']?[^\s"\']{8,}'),
'url_credentials': re.compile(rb'https?://[^/\s:@]+:[^/\s@]+@'),
}
SECRET_NAMES = re.compile(r'(?i)(^|/)(\.env|\.git-credentials|id_rsa|id_ed25519|.*\.pem|.*\.key|credentials(\.json)?)$')
def main(argv):
root, cand, out = argv[1], argv[2], argv[3]
max_file = 5 * 2**20; max_total = 200 * 2**20
if '--max-file-mb' in argv: max_file = int(float(argv[argv.index('--max-file-mb') + 1]) * 2**20)
if '--max-total-mb' in argv: max_total = int(float(argv[argv.index('--max-total-mb') + 1]) * 2**20)
paths = [l.strip() for l in open(cand, encoding='utf-8') if l.strip() and not l.startswith('#')]
rep = {'root': root, 'n_candidates': len(paths), 'missing': [], 'too_large': [], 'secret_names': [],
'secret_hits': [], 'total_bytes': 0, 'files': []}
for p in paths:
fp = os.path.join(root, p)
if not os.path.isfile(fp):
rep['missing'].append(p); continue
size = os.path.getsize(fp); rep['total_bytes'] += size
if size > max_file: rep['too_large'].append([p, size])
if SECRET_NAMES.search(p): rep['secret_names'].append(p)
h = hashlib.sha256()
with open(fp, 'rb') as f:
data = f.read()
h.update(data)
if b'\x00' not in data[:8192]:
for i, line in enumerate(data.splitlines(), 1):
for name, rx in PATTERNS.items():
if rx.search(line):
rep['secret_hits'].append([p, i, name])
rep['files'].append([p, size, h.hexdigest()])
rep['over_total'] = rep['total_bytes'] > max_total
rep['limits'] = {'max_file_bytes': max_file, 'max_total_bytes': max_total}
blocked = bool(rep['missing'] or rep['too_large'] or rep['secret_names'] or rep['secret_hits'] or rep['over_total'])
rep['result'] = 'BLOCKED' if blocked else 'PASS'
json.dump(rep, open(out, 'w'), indent=1)
print(rep['result'], 'files', len(rep['files']), 'bytes', rep['total_bytes'], 'too_large', len(rep['too_large']),
'secret_hits', len(rep['secret_hits']), 'secret_names', len(rep['secret_names']), 'missing', len(rep['missing']))
return 1 if blocked else 0
if __name__ == '__main__':
sys.exit(main(sys.argv))