#!/usr/bin/env python3 """Check publishable source files without Flutter or private dependencies. Uses tracked and non-ignored untracked files, leaving local inputs untouched. Credential checks cover recognizable formats, not arbitrary passwords or history. """ from pathlib import Path import re import subprocess from urllib.parse import unquote, urlsplit ROOT = Path(__file__).resolve().parent.parent LOCAL_DIRECTORIES = { '.local', '.dart_tool', '.gradle', '.build', '.swiftpm', '__pycache__', 'build', 'coverage', 'Pods', 'ephemeral', 'xcuserdata', } PRIVATE_SUFFIXES = {'.pem', '.key', '.p8', '.p12', '.pfx', '.keystore', '.jks', '.mobileprovision', '.provisionprofile'} CREDENTIALS = re.compile( r'-----BEGIN (?:RSA |EC |OPENSSH |DSA )?PRIVATE KEY-----|' r'gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|' r'sk-[A-Za-z0-9_-]{24,}|AKIA[0-9A-Z]{16}' ) MARKDOWN_LINK = re.compile(r'!?\[[^\]]*\]\(([^\s)]+)\)') def git_files(root, *arguments): result = subprocess.check_output(['git', 'ls-files', '-z', *arguments], cwd=root) return sorted(set(result.decode('utf-8').split('\0')) - {''}) def check_files(root, names): errors = [] root = root.resolve() for name in names: path = root / name if not path.exists() and not path.is_symlink(): continue # Worktree deletions are absent from the next source snapshot. if not path.resolve().is_relative_to(root): errors.append(f'{name}: symlink escapes repository') continue if not path.is_file(): continue if (LOCAL_DIRECTORIES.intersection(path.relative_to(root).parts) or name.startswith(('validation/', 'test-results/', 'dist/')) or any(part.endswith('.cutdex') for part in path.relative_to(root).parts) or path.name in {'.DS_Store', 'pubspec_overrides.yaml', 'key.properties'} or path.suffix in PRIVATE_SUFFIXES or path.name == '.env' or (path.name.startswith('.env.') and not path.name.endswith(('.example', '.sample')))): errors.append(f'{name}: local artifact or credential configuration') if path.stat().st_size > 50 * 1024 * 1024: errors.append(f'{name}: source file exceeds 50 MiB') continue try: source = path.read_text(encoding='utf-8') except UnicodeError: continue for number, line in enumerate(source.splitlines(), 1): if CREDENTIALS.search(line): # Never echo the matched credential. errors.append(f'{name}:{number}: possible credential; review locally') if path.suffix == '.md': for match in MARKDOWN_LINK.finditer(source): target = urlsplit(match.group(1).strip('<>')) if target.scheme or target.netloc or not target.path: continue linked = (path.parent / unquote(target.path)).resolve() if not linked.is_relative_to(root) or not linked.exists(): errors.append(f'{name}: missing or external local link {target.path}') return errors def main(): names = git_files(ROOT, '-co', '--exclude-standard') errors = check_files(ROOT, names) for name in git_files(ROOT, '-ci', '--exclude-standard'): if (ROOT / name).exists(): errors.append(f'{name}: ignored file is already tracked') if errors: raise SystemExit('\n'.join(errors)) print(f'Public repository source checks: passed ({len(names)} paths considered)') if __name__ == '__main__': main()