LifeMate — Personal, Family & Learning Companion
docs/offline-data-rescue-options
chore/phase1-repository-cleanup
deploy/stable-web-apk
deployment-static
design/mobile-accessibility-v2
design/mobile-navigation-review
docs/child-education-access-decision
docs/offline-data-rescue-options
docs/owner-guide-and-delivery-plan
docs/phase1-policy-options
docs/public-review-live-2026-10-15
feat/aparat-learning-library
feat/child-device-safety
feat/child-identity-schema
feat/flutter-mobile-navigation
feat/learning-content-operations
feat/local-signing-preflight
feat/online-family-stage0
feat/personal-encrypted-recovery
feat/private-planner-series-api
feat/public-review-page
feat/standalone-weekly-planner
feat/weekly-planner-android
final-feature-1/iran-family-school
fix/auth-abuse-expiry-safety
fix/auth-expiry-after-lock
fix/backend-request-safety
fix/lifeguide-bootstrap-naming-ci
fix/lifeguide-pages-qa-base-preflight
fix/lifeguide-qa-pages-env-deployment
fix/local-task-persistence
fix/managed-child-session-boundary
fix/personal-jalali-dates
fix/personal-notification-controls
fix/personal-upgrade-delivery-guard
fix/sms-approval-gates
fix/study-duration-display
infra/cloudflare-neon-migration
infra/lifeguide-cloudflare-qa
infra/lifeguide-github-pages-qa
infra/lifeguide-supabase-qa
main
phase-1/foundation-completion
phase-2/product-foundation-identity
phase-3/planner-school-family-core
phase-4/ai-learning-wellbeing
phase-5/hardening-observability-release
test/android-reminder-acceptance
test/personal-notification-settings
test/personal-recovery-acceptance
test/role-entry-apk
| 1 | #!/usr/bin/env python3 |
| 2 | """Check necessary APK update compatibility; data-preserving delivery stays blocked.""" |
| 3 | import argparse |
| 4 | import hashlib |
| 5 | import json |
| 6 | import re |
| 7 | from pathlib import Path |
| 8 | from zipfile import ZipFile |
| 9 | |
| 10 | from check_apk_manifest import manifest_tags |
| 11 | from test_standalone_android import certificate_digest |
| 12 | |
| 13 | BASELINE_PATH = Path(__file__).resolve().parents[3] / 'docs/operations/personal-delivered-baseline.json' |
| 14 | PACKAGE = 'ir.lifeguide.app.device' |
| 15 | |
| 16 | |
| 17 | def _digest(value): |
| 18 | return isinstance(value, str) and re.fullmatch(r'[0-9a-f]{64}', value) is not None |
| 19 | |
| 20 | |
| 21 | def load_baseline(): |
| 22 | """Only reviewed repository source supplies the public delivered baseline.""" |
| 23 | baseline = json.loads(BASELINE_PATH.read_text(encoding='utf-8')) |
| 24 | if (not isinstance(baseline, dict) or baseline.get('schemaVersion') != 1 or |
| 25 | baseline.get('status') != 'DELIVERED' or baseline.get('package') != PACKAGE or |
| 26 | type(baseline.get('versionCode')) is not int or baseline['versionCode'] < 1 or |
| 27 | not _digest(baseline.get('certificateSha256')) or not _digest(baseline.get('apkSha256')) or |
| 28 | baseline.get('installedDeviceVerification') != 'UNVERIFIED' or |
| 29 | not isinstance(baseline.get('source'), dict) or |
| 30 | type(baseline['source'].get('artifactId')) is not int): |
| 31 | raise ValueError('delivered_baseline_invalid') |
| 32 | return baseline |
| 33 | |
| 34 | |
| 35 | def _report(reasons): |
| 36 | return {'result': 'BLOCKED' if reasons else 'PASS', 'reasonCodes': reasons, |
| 37 | 'dataPreservation': 'UNVERIFIED', 'installedDeviceVerification': 'UNVERIFIED', |
| 38 | 'delivery': 'BLOCKED', 'deliveryReasonCode': 'default_database_upgrade_acceptance_missing'} |
| 39 | |
| 40 | |
| 41 | def validate_compatibility(evidence, baseline): |
| 42 | """Pure policy over independently inspected evidence, never an APK verifier. |
| 43 | |
| 44 | The command-line path below obtains these values from the actual APK and |
| 45 | apksigner. Passing these checks cannot establish database preservation. |
| 46 | """ |
| 47 | reasons = [] |
| 48 | if evidence.get('package') != baseline['package']: |
| 49 | reasons.append('package_mismatch') |
| 50 | certificate = evidence.get('certificateSha256') |
| 51 | if not _digest(certificate): |
| 52 | reasons.append('certificate_invalid') |
| 53 | elif certificate != baseline['certificateSha256']: |
| 54 | reasons.append('certificate_mismatch') |
| 55 | version = evidence.get('versionCode') |
| 56 | if type(version) is not int or version < 1: |
| 57 | reasons.append('version_code_invalid') |
| 58 | elif version <= baseline['versionCode']: |
| 59 | reasons.append('version_code_not_incremented') |
| 60 | if not _digest(evidence.get('apkSha256')): |
| 61 | reasons.append('apk_digest_invalid') |
| 62 | return _report(reasons) |
| 63 | |
| 64 | |
| 65 | def check(apk): |
| 66 | try: |
| 67 | baseline = load_baseline() |
| 68 | except Exception: |
| 69 | return _report(['delivered_baseline_invalid']) |
| 70 | try: |
| 71 | apk = Path(apk) |
| 72 | with ZipFile(apk) as archive: |
| 73 | if archive.namelist().count('AndroidManifest.xml') != 1: |
| 74 | raise ValueError('ambiguous_manifest') |
| 75 | tags = manifest_tags(archive.read('AndroidManifest.xml')) |
| 76 | manifests = [attributes for tag, attributes in tags if tag == 'manifest'] |
| 77 | if len(manifests) != 1: |
| 78 | raise ValueError('ambiguous_manifest') |
| 79 | evidence = {key: manifests[0].get(key) for key in ('package', 'versionCode')} |
| 80 | evidence['apkSha256'] = hashlib.sha256(apk.read_bytes()).hexdigest() |
| 81 | except Exception: |
| 82 | return _report(['candidate_manifest_invalid']) |
| 83 | try: |
| 84 | # Existing verifier requires a successful apksigner process and exactly |
| 85 | # one certificate digest. No CLI flag/report can supply a trusted signer. |
| 86 | evidence['certificateSha256'] = certificate_digest(apk) |
| 87 | except Exception: |
| 88 | return _report(['candidate_signature_unverified']) |
| 89 | return validate_compatibility(evidence, baseline) |
| 90 | |
| 91 | |
| 92 | class _Parser(argparse.ArgumentParser): |
| 93 | def error(self, message): |
| 94 | raise ValueError('arguments_invalid') |
| 95 | |
| 96 | |
| 97 | def main(argv=None): |
| 98 | parser = _Parser(description=__doc__) |
| 99 | parser.add_argument('apk', type=Path) |
| 100 | try: |
| 101 | options = parser.parse_args(argv) |
| 102 | except ValueError: |
| 103 | result = _report(['arguments_invalid']) |
| 104 | else: |
| 105 | result = check(options.apk) |
| 106 | print(json.dumps(result, sort_keys=True)) |
| 107 | return 0 if result['result'] == 'PASS' else 1 |
| 108 | |
| 109 | |
| 110 | if __name__ == '__main__': |
| 111 | raise SystemExit(main()) |