| 1 | #!/usr/bin/env python3 |
| 2 | """Equipo's only device/Keychain mutation boundary is lifetime-guarded commit.""" |
| 3 | from pathlib import Path |
| 4 | import re |
| 5 | |
| 6 | root = Path(__file__).resolve().parents[1] |
| 7 | sources = root / 'apple/Sources/EquipoKit' |
| 8 | account = sources / 'Pairing/DeviceAccount.swift' |
| 9 | text = account.read_text() |
| 10 | assert 'func commit(' in text, 'DeviceAccounts must expose one guarded commit function' |
| 11 | start = text.index('{', text.index('func commit(')) |
| 12 | depth = 1 |
| 13 | end = start + 1 |
| 14 | while depth: |
| 15 | depth += (text[end] == '{') - (text[end] == '}') |
| 16 | end += 1 |
| 17 | commit = text[start:end] |
| 18 | outside = text[:start] + text[end:] |
| 19 | assert not re.search(r'store\.(?:write|remove)\s*\(', outside), 'persistence outside commit' |
| 20 | assert 'try lifetimes.require(lifetime)' in commit, 'commit must reject an ended or foreign lifetime' |
| 21 | assert commit.index('try lifetimes.require(lifetime)') < commit.index('store.write('), 'guard must precede persistence' |
| 22 | for path in [*sources.rglob('*.swift'), *(root / 'ios').rglob('*.swift'), *(root / 'macos').rglob('*.swift')]: |
| 23 | value = path.read_text() |
| 24 | if path != account: |
| 25 | assert not re.search(r'\b(?:accounts|self\.accounts)\.(?:save|remove)\s*\(', value), f'unguarded account write: {path}' |
| 26 | assert not re.search(r'\.write\s*\(', value), f'raw secret-store write: {path}' |
| 27 | assert not re.search(r'\b\w*(?:store|Store)\.remove\s*\(', value), f'raw secret-store removal: {path}' |
| 28 | if path.name != 'SecretStore.swift': |
| 29 | assert not re.search(r'\bSecItem(?:Add|Update|Delete)\s*\(', value), f'raw Keychain write: {path}' |
| 30 | assert 'private let store: SecretStore' in text, 'raw store must stay private' |
| 31 | assert not re.search(r'func (?:save|remove)\(', text), 'do not add a persistence bypass' |
| 32 | print('Sign-in lifetime: device and Keychain writes use the guarded commit') |
| 33 | |