| 1 | """Read the installed macOS component receipt and verify its recorded files.""" |
| 2 | import hashlib |
| 3 | import json |
| 4 | import os |
| 5 | from pathlib import Path |
| 6 | import stat |
| 7 | import subprocess |
| 8 | |
| 9 | |
| 10 | def snapshot(receipt=Path('/var/db/cage-components.json')): |
| 11 | result = {'manifest': None, 'tools': {}, 'installed': False, 'missing': []} |
| 12 | try: |
| 13 | info = receipt.lstat() |
| 14 | if not stat.S_ISREG(info.st_mode) or info.st_uid != 0 or info.st_mode & 0o022 or info.st_size > 4 << 20: |
| 15 | raise ValueError('invalid receipt') |
| 16 | value = json.loads(receipt.read_text()) |
| 17 | if (value.get('protocol') != 1 or value.get('system') != 'macos' |
| 18 | or not isinstance(value.get('files'), list) or not 0 < len(value['files']) <= 10000): |
| 19 | raise ValueError('invalid manifest') |
| 20 | for item in value['files']: |
| 21 | target = item.get('target', '') |
| 22 | if not target: |
| 23 | continue |
| 24 | if ('..' in Path(target).parts or not |
| 25 | (target.startswith('/Applications/Cage') or target.startswith('/usr/local/libexec/cage-'))): |
| 26 | raise ValueError('invalid component target') |
| 27 | file = Path(target) |
| 28 | try: |
| 29 | with file.open('rb') as stream: |
| 30 | current = hashlib.file_digest(stream, 'sha256').hexdigest() |
| 31 | if current != item['installed_sha256'] and len(result['missing']) < 20: |
| 32 | result['missing'].append(target) |
| 33 | except OSError: |
| 34 | if len(result['missing']) < 20: |
| 35 | result['missing'].append(target) |
| 36 | result['manifest'] = {key: value.get(key) for key in ('protocol', 'revision', 'app_version')} |
| 37 | if result['missing']: |
| 38 | result['manifest']['revision'] = '' |
| 39 | result['installed'] = not result['missing'] |
| 40 | except (OSError, ValueError, KeyError, TypeError): |
| 41 | result['error'] = '客机尚无可核验的组件记录,请同步环境组件。' |
| 42 | |
| 43 | |
| 44 | editor = Path('/usr/local/libexec/cage-workbench-browser-4.145.0-pw1.65.0/editor') |
| 45 | result['tools']['code_server'] = '' |
| 46 | try: |
| 47 | package = json.loads((editor / 'package.json').read_text()) |
| 48 | if package.get('name') == 'code-server' and isinstance(package.get('version'), str) and os.access(editor / 'bin/code-server', os.X_OK): |
| 49 | result['tools']['code_server'] = package['version'][:200] |
| 50 | except (OSError, ValueError, AttributeError): |
| 51 | pass |
| 52 | commands = { |
| 53 | 'chromium': ['/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '--version'], |
| 54 | 'claude': ['/usr/local/libexec/cage-cli/bin/claude', '--version'], |
| 55 | 'python': ['/usr/local/libexec/cage-cli/bin/python3', '--version'], |
| 56 | 'git': ['/usr/local/libexec/cage-cli/bin/git', '--version'], |
| 57 | 'node': ['/usr/local/libexec/cage-node-v24.22.2/bin/node', '--version'], |
| 58 | } |
| 59 | for name, command in commands.items(): |
| 60 | try: |
| 61 | response = subprocess.run(command, stdin=subprocess.DEVNULL, capture_output=True, timeout=3, |
| 62 | text=True, env={**os.environ, 'DISABLE_AUTOUPDATER': '1'}) |
| 63 | lines = response.stdout.strip().splitlines() |
| 64 | result['tools'][name] = lines[0][:200] if response.returncode == 0 and lines else '' |
| 65 | except (OSError, subprocess.SubprocessError): |
| 66 | result['tools'][name] = '' |
| 67 | return result |
| 68 | |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | print(json.dumps(snapshot(), ensure_ascii=False)) |