LifeMate — Personal, Family & Learning Companion
test: validate private recovery APK manifest boundaries
Quinn White committed
Oct 22, 2026 at 06:14 UTC
4086a45f6055149abf23252e89251605c492ff5a
2 files changed
+140
apps/lifemate/tool/check_personal_recovery_apk.py
new
+71
@@ -0,0 +1,71 @@
1
+"""Inspect the binary manifest of the never-distributed recovery QA APK."""
2
+import argparse
3
+import hashlib
4
+import json
5
+from pathlib import Path
6
+from zipfile import ZipFile
7
+
8
+from check_apk_manifest import manifest_tags
9
+
10
+PACKAGE = 'ir.lifeguide.app.personal'
11
+LABEL = 'آزمون بازیابی'
12
+
13
+
14
+def check(apk, expected_code=None):
15
+ apk = Path(apk)
16
+ with ZipFile(apk) as archive:
17
+ tags = manifest_tags(archive.read('AndroidManifest.xml'))
18
+
19
+ def one(name):
20
+ matches = [attributes for tag, attributes in tags if tag == name]
21
+ if len(matches) != 1:
22
+ raise ValueError('Missing or ambiguous QA manifest element')
23
+ return matches[0]
24
+
25
+ manifest, application, sdk = one('manifest'), one('application'), one('uses-sdk')
26
+ code = manifest.get('versionCode')
27
+ if (manifest.get('package') != PACKAGE or type(code) is not int or code not in (10, 11)
28
+ or (expected_code is not None and code != expected_code)
29
+ or manifest.get('versionName') != f'0.7.2-recovery-qa.{code}'
30
+ or application.get('label') != LABEL):
31
+ raise ValueError('Expected the separate synthetic recovery QA edition')
32
+ if (application.get('debuggable') is not True
33
+ or application.get('allowBackup') is not False
34
+ or application.get('usesCleartextTraffic') is not False):
35
+ raise ValueError('QA debug, backup and network gates are required')
36
+ if sdk.get('minSdkVersion') != 24 or sdk.get('targetSdkVersion') != 36:
37
+ raise ValueError('Unexpected QA SDK versions')
38
+ required = {'android.permission.' + permission for permission in
39
+ ('POST_NOTIFICATIONS', 'RECEIVE_BOOT_COMPLETED', 'SCHEDULE_EXACT_ALARM')}
40
+ allowed = required | {PACKAGE + '.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION'}
41
+ permissions = {a.get('name') for tag, a in tags if tag.startswith('uses-permission')}
42
+ if not required <= permissions <= allowed:
43
+ raise ValueError('Unexpected QA permissions; INTERNET is forbidden')
44
+ activities = [a for tag, a in tags if tag == 'activity']
45
+ if not any(a.get('name') == 'ir.lifeguide.app.MainActivity' and
46
+ a.get('exported') is True for a in activities):
47
+ raise ValueError('QA must resolve the existing fully qualified activity')
48
+ receivers = [a for tag, a in tags if tag == 'receiver']
49
+ if (not any(a.get('name') == 'ir.lifeguide.app.LocalReminderReceiver'
50
+ and a.get('exported') is False for a in receivers)
51
+ or any(a.get('exported') is True for a in receivers)):
52
+ raise ValueError('QA reminder receivers must remain private')
53
+ markers = [a for tag, a in tags if tag == 'meta-data'
54
+ and a.get('name') == 'ir.lifeguide.PERSONAL_RECOVERY_QA']
55
+ if len(markers) != 1 or markers[0].get('value') is not True:
56
+ raise ValueError('Explicit synthetic recovery QA marker is required')
57
+ return {'result': 'PASS', 'package': PACKAGE, 'label': LABEL,
58
+ 'versionCode': code, 'versionName': manifest['versionName'],
59
+ 'debuggable': True, 'allowBackup': False, 'usesCleartextTraffic': False,
60
+ 'internetPermission': False, 'permissions': sorted(permissions),
61
+ 'minSdkVersion': 24, 'targetSdkVersion': 36, 'syntheticOnly': True,
62
+ 'sha256': hashlib.sha256(apk.read_bytes()).hexdigest(),
63
+ 'limitations': 'Never distribute. Signature and Android lifecycle checked separately.'}
64
+
65
+
66
+if __name__ == '__main__':
67
+ parser = argparse.ArgumentParser(description=__doc__)
68
+ parser.add_argument('apk', type=Path)
69
+ parser.add_argument('--expected-code', type=int, choices=(10, 11))
70
+ options = parser.parse_args()
71
+ print(json.dumps(check(options.apk, options.expected_code), ensure_ascii=False, indent=2))
apps/lifemate/tool/check_personal_recovery_apk_test.py
new
+69
@@ -0,0 +1,69 @@
1
+"""Synthetic manifest contracts; no actual APK installation is inferred."""
2
+import tempfile
3
+import unittest
4
+from pathlib import Path
5
+from unittest.mock import patch
6
+from zipfile import ZipFile
7
+
8
+import check_personal_recovery_apk as subject
9
+
10
+
11
+class RecoveryManifestTest(unittest.TestCase):
12
+ def setUp(self):
13
+ directory = tempfile.TemporaryDirectory()
14
+ self.addCleanup(directory.cleanup)
15
+ self.apk = Path(directory.name) / 'qa.apk'
16
+ with ZipFile(self.apk, 'w') as archive:
17
+ archive.writestr('AndroidManifest.xml', b'parser fixture')
18
+ self.tags = [
19
+ ('manifest', {'package': 'ir.lifeguide.app.personal', 'versionCode': 10,
20
+ 'versionName': '0.7.2-recovery-qa.10'}),
21
+ ('application', {'label': 'آزمون بازیابی', 'debuggable': True,
22
+ 'allowBackup': False, 'usesCleartextTraffic': False}),
23
+ ('uses-sdk', {'minSdkVersion': 24, 'targetSdkVersion': 36}),
24
+ ('activity', {'name': 'ir.lifeguide.app.MainActivity', 'exported': True}),
25
+ ('receiver', {'name': 'ir.lifeguide.app.LocalReminderReceiver', 'exported': False}),
26
+ ('meta-data', {'name': 'ir.lifeguide.PERSONAL_RECOVERY_QA', 'value': True}),
27
+ ] + [('uses-permission', {'name': 'android.permission.' + name}) for name in
28
+ ('POST_NOTIFICATIONS', 'RECEIVE_BOOT_COMPLETED', 'SCHEDULE_EXACT_ALARM')]
29
+
30
+ def check(self, code=None):
31
+ with patch.object(subject, 'manifest_tags', return_value=self.tags):
32
+ return subject.check(self.apk, expected_code=code)
33
+
34
+ def test_exact_qa_identity_and_higher_version(self):
35
+ self.assertEqual(self.check()['versionCode'], 10)
36
+ self.tags[0][1].update(versionCode=11, versionName='0.7.2-recovery-qa.11')
37
+ self.assertEqual(self.check()['versionCode'], 11)
38
+ with self.assertRaises(ValueError):
39
+ self.check(10)
40
+
41
+ def test_rejects_product_identity_permissions_and_missing_qa_marker(self):
42
+ for index, key, value in ((0, 'package', 'ir.lifeguide.app.device'),
43
+ (0, 'versionCode', 12), (1, 'label', 'لایفگاید'),
44
+ (1, 'debuggable', False), (1, 'allowBackup', True),
45
+ (1, 'usesCleartextTraffic', True), (2, 'targetSdkVersion', 35),
46
+ (2, 'minSdkVersion', 23), (3, 'name', '.MainActivity'),
47
+ (4, 'exported', True), (5, 'value', False)):
48
+ with self.subTest(key=key, value=value):
49
+ old = self.tags[index][1][key]
50
+ self.tags[index][1][key] = value
51
+ with self.assertRaises(ValueError):
52
+ self.check()
53
+ self.tags[index][1][key] = old
54
+ self.tags.append(('uses-permission', {'name': 'android.permission.INTERNET'}))
55
+ with self.assertRaises(ValueError):
56
+ self.check()
57
+ self.tags.pop()
58
+ self.tags.pop(5)
59
+ with self.assertRaises(ValueError):
60
+ self.check()
61
+
62
+ def test_duplicate_critical_manifest_tags_refused(self):
63
+ self.tags.append(('manifest', self.tags[0][1].copy()))
64
+ with self.assertRaises(ValueError):
65
+ self.check()
66
+
67
+
68
+if __name__ == '__main__':
69
+ unittest.main()