LifeMate — Personal, Family & Learning Companion

feat/standalone-weekly-planner
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
py 103 lines 4.97 KB
Raw
1 #!/usr/bin/env python3
2 """Check the actual APK binary manifest; this does not verify its signature/install."""
3 import argparse
4 import hashlib
5 import json
6 import struct
7 from pathlib import Path
8 from zipfile import ZipFile
9
10
11 def manifest_tags(data):
12 def u16(offset):
13 return struct.unpack_from('<H', data, offset)[0]
14
15 def u32(offset):
16 return struct.unpack_from('<I', data, offset)[0]
17
18 def encoded_length(offset, wide=False):
19 first = u16(offset) if wide else data[offset]
20 high = 0x8000 if wide else 0x80
21 shift = 16 if wide else 8
22 step = 2 if wide else 1
23 if first & high:
24 second = u16(offset + step) if wide else data[offset + step]
25 return ((first & (high - 1)) << shift) | second, offset + 2 * step
26 return first, offset + step
27
28 if u16(0) != 3 or u32(4) != len(data):
29 raise ValueError('Expected a binary Android XML manifest')
30 strings, tags, offset = [], [], u16(2)
31 while offset < len(data):
32 kind, header_size, size = struct.unpack_from('<HHI', data, offset)
33 if size < header_size or offset + size > len(data):
34 raise ValueError('Invalid Android XML chunk')
35 if kind == 1:
36 count, _, flags, start, _ = struct.unpack_from('<IIIII', data, offset + 8)
37 for index in range(count):
38 position = offset + start + u32(offset + header_size + index * 4)
39 if flags & 0x100:
40 _, position = encoded_length(position)
41 length, position = encoded_length(position)
42 value = data[position:position + length].decode('utf-8')
43 else:
44 length, position = encoded_length(position, wide=True)
45 value = data[position:position + 2 * length].decode('utf-16-le')
46 strings.append(value)
47 elif kind == 0x102:
48 extension = offset + header_size
49 tag = strings[u32(extension + 4)]
50 attr_start, attr_size, attr_count = struct.unpack_from('<HHH', data, extension + 8)
51 attributes = {}
52 for index in range(attr_count):
53 position = extension + attr_start + index * attr_size
54 name, raw = strings[u32(position + 4)], u32(position + 8)
55 value_type, value = data[position + 15], u32(position + 16)
56 if raw != 0xffffffff:
57 decoded = strings[raw]
58 elif value_type == 3:
59 decoded = strings[value]
60 elif value_type == 18:
61 decoded = bool(value)
62 else:
63 decoded = value
64 attributes[name] = decoded
65 tags.append((tag, attributes))
66 offset += size
67 return tags
68
69
70 def check(apk, mode):
71 with ZipFile(apk) as archive:
72 tags = manifest_tags(archive.read('AndroidManifest.xml'))
73 manifest = next(attributes for tag, attributes in tags if tag == 'manifest')
74 application = next(attributes for tag, attributes in tags if tag == 'application')
75 sdk = next(attributes for tag, attributes in tags if tag == 'uses-sdk')
76 permissions = [attributes.get('name') for tag, attributes in tags if tag.startswith('uses-permission')]
77 if manifest.get('package') != 'ir.lifeguide.app':
78 raise ValueError('APK application ID must be ir.lifeguide.app')
79 if application.get('label') != 'لایف‌گاید':
80 raise ValueError('APK must display the LifeGuide Persian product label')
81 if application.get('allowBackup') is not False:
82 raise ValueError('APK must explicitly disable Android app-data backup')
83 if application.get('usesCleartextTraffic') is not False:
84 raise ValueError('APK must explicitly disable cleartext network traffic')
85 if bool(application.get('debuggable', False)) != (mode == 'debug'):
86 raise ValueError('APK debuggable flag differs from the declared build mode')
87 if 'android.permission.INTERNET' not in permissions:
88 raise ValueError('APK lacks INTERNET permission')
89 return {'result': 'PASS', 'mode': mode, 'package': manifest['package'],
90 'versionName': manifest.get('versionName'), 'versionCode': manifest.get('versionCode'),
91 'label': application['label'], 'debuggable': bool(application.get('debuggable', False)),
92 'allowBackup': False, 'usesCleartextTraffic': False, 'internetPermission': True,
93 'minSdkVersion': sdk.get('minSdkVersion'), 'targetSdkVersion': sdk.get('targetSdkVersion'),
94 'sha256': hashlib.sha256(apk.read_bytes()).hexdigest(),
95 'limitations': 'Signature is checked separately by apksigner; device install/lifecycle UNVERIFIED.'}
96
97
98 if __name__ == '__main__':
99 parser = argparse.ArgumentParser(description=__doc__)
100 parser.add_argument('apk', type=Path)
101 parser.add_argument('--mode', choices=['debug', 'release'], required=True)
102 options = parser.parse_args()
103 print(json.dumps(check(options.apk, options.mode), ensure_ascii=False, indent=2))