"""Read-only checks for metadata consistency, local links and common publication leaks. This is a heuristic guardrail, not proof of anonymity or absence of secrets. Ignored runtime data is excluded; independently inspect outputs before sharing. """ import ast import json import re import subprocess from pathlib import Path from urllib.parse import urlparse, unquote ROOT=Path(__file__).resolve().parent.parent EXCLUDE={'.git','local','output','logs','__pycache__','.venv'} BANNED={'.xlsx','.csv','.jsonl','.sql','.log','.pyc','.pem','.key','.pfx','.zip','.bak'} def sensitive_findings(text, canonical_host): findings=[] checks={ 'absolute Windows path':r'\b[A-Za-z]:[\\/]', 'absolute private Unix path':r'/(?:home|mnt|srv|opt)/[\w.-]+', 'private key material':r'-----BEGIN (?:RSA |OPENSSH |EC )?PRIVATE KEY-----', 'literal credential assignment':r"""(?i)\b(?:password|passwd|api[_-]?key|access[_-]?token|secret)\s*[=:]\s*["'][A-Za-z0-9_+/=-]{12,}["']""", } for label,pattern in checks.items(): if re.search(pattern,text): findings.append(label) for email in re.findall(r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}',text): if email.rsplit('@',1)[1] not in {'example.com','example.org','example.net'}: findings.append('non-example email') for address in re.findall(r'(?"\x27)]+',text): host=urlparse(url).hostname if host not in {canonical_host,'schemas.openxmlformats.org','example.com','example.org','example.net'}: findings.append(f'URL host requires review: {host}') return sorted(set(findings)) def main(): errors=[] manifest=json.loads((ROOT/'repository.json').read_text()) variables=json.loads((ROOT/'repository-variables.json').read_text()) host=urlparse(manifest['canonical_url']).hostname version=(ROOT/'VERSION').read_text().strip() if manifest['version']!=version: errors.append('manifest version mismatch') for name in ['README.md','CHANGELOG.md','UPDATES.md']: if version not in (ROOT/name).read_text(encoding='utf-8'): errors.append(f'{name}: version missing') if manifest['authors']!=variables['identity']['authors']: errors.append('authors mismatch') if manifest['rights']!=variables['rights']: errors.append('rights mismatch') if manifest['canonical_url']!=variables['repository']['canonical_url']: errors.append('canonical URL mismatch') for name in ['AUTHOR.md','LICENSE']: for author in manifest['authors']: if author['name'] not in (ROOT/name).read_text(encoding='utf-8'): errors.append(f'{name}: author missing') if 'MIT License' not in (ROOT/'LICENSE').read_text(): errors.append('license mismatch') declared=list(manifest['entrypoints'].values())+[x['path'] for x in manifest['contents']] declared+=manifest['ai_retrieval']['read_first']+manifest['ai_retrieval']['retrieve_as_needed'] for name in declared: p=Path(name) if p.is_absolute() or '..' in p.parts or not (ROOT/p).exists(): errors.append(f'invalid declared path: {name}') files=[] for p in sorted(ROOT.rglob('*')): if not p.is_file() or any(x in EXCLUDE for x in p.relative_to(ROOT).parts): continue name=p.relative_to(ROOT).as_posix() files.append(name) if p.suffix.lower() in BANNED or p.name=='.env': errors.append(f'prohibited artifact: {name}') try: text=p.read_text(encoding='utf-8-sig') except UnicodeError: errors.append(f'non-text file requires review: {name}') continue for finding in sensitive_findings(text,host): errors.append(f'{name}: {finding}') if p.suffix=='.json': json.loads(text) if p.suffix=='.py': ast.parse(text,filename=name) if p.suffix=='.md': for link in re.findall(r'\[[^\]]*\]\(([^)]+)\)',text): link=link.split('#',1)[0] if not link or '://' in link or link.startswith('mailto:'): continue target=(p.parent/unquote(link)).resolve() if not target.is_relative_to(ROOT.resolve()) or not target.exists(): errors.append(f'{name}: invalid link {link}') # .gitignore does not protect files that were already tracked. result=subprocess.run(['git','-c',f'safe.directory={ROOT.as_posix()}','-C',str(ROOT),'ls-files','-z'],capture_output=True,text=True) if result.returncode: errors.append('git tracked-file scan failed') for name in filter(None,result.stdout.split('\0')): p=Path(name) if any(x in EXCLUDE for x in p.parts) or p.suffix.lower() in BANNED or p.name=='.env': errors.append(f'prohibited tracked file: {name}') print(json.dumps({'passed':not errors,'files_checked':len(files),'errors':errors},indent=2)) return bool(errors) if __name__=='__main__': raise SystemExit(main())