LifeMate — Personal, Family & Learning Companion
ci: verify runtime database privileges, web expiry and APK identity
Codex committed
Oct 9, 2026 at 06:23 UTC
7c4ca78ffe55ce37d0b94984c205b6ba592cf722
2 files changed
+140
.github/workflows/ci.yml
+37
@@ -50,6 +50,27 @@ jobs:
50
for test in backend/tests/*schema_test.sql; do psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f "$test"; done
51
- run: npm test && npm run check && npm audit --audit-level=high
52
working-directory: backend
53
+ - name: Verify isolated database roles and runtime family scenario
54
+ working-directory: backend
55
+ env:
56
+ PG_CONTAINER: ${{ job.services.postgres.id }}
57
+ PGHOST: 127.0.0.1
58
+ PGPORT: 5432
59
+ PGUSER: postgres
60
+ PGDATABASE: lifemate_ci
61
+ run: |
62
+ export API_DB_PASSWORD=$(node -p "require('crypto').randomBytes(32).toString('hex')")
63
+ export MIGRATOR_DB_PASSWORD=$(node -p "require('crypto').randomBytes(32).toString('hex')")
64
+ export BACKUP_DB_PASSWORD=$(node -p "require('crypto').randomBytes(32).toString('hex')")
65
+ export ROLE_TEST_STATE_FILE="$RUNNER_TEMP/lifeguide-role-state.json"
66
+ trap 'if [ -f "$ROLE_TEST_STATE_FILE" ]; then node scripts/test-db-roles.js --cleanup; fi' EXIT
67
+ node scripts/test-db-roles.js --synthetic-only
68
+ source_db=$(node -p "JSON.parse(require('fs').readFileSync(process.env.ROLE_TEST_STATE_FILE)).source_db")
69
+ target_db=$(node -p "JSON.parse(require('fs').readFileSync(process.env.ROLE_TEST_STATE_FILE)).target_db")
70
+ PGPASSWORD="$BACKUP_DB_PASSWORD" docker exec -e PGPASSWORD "$PG_CONTAINER" pg_dump -h 127.0.0.1 -U lifeguide_backup -d "$source_db" --format=custom --no-owner --no-privileges --file=/tmp/lifeguide-roles.dump
71
+ docker exec "$PG_CONTAINER" pg_restore --list /tmp/lifeguide-roles.dump >/dev/null
72
+ PGPASSWORD="$MIGRATOR_DB_PASSWORD" docker exec -e PGPASSWORD "$PG_CONTAINER" pg_restore -h 127.0.0.1 -U lifeguide_migrator -d "$target_db" --clean --if-exists --no-owner --no-privileges --exit-on-error /tmp/lifeguide-roles.dump
73
+ node scripts/test-db-roles.js --verify-restored
74
- name: Backup and restore synthetic database
75
env:
76
PG_CONTAINER: ${{ job.services.postgres.id }}
@@ -84,6 +105,13 @@ jobs:
105
npm install --no-save --no-package-lock playwright@1.62.1
106
npx playwright install --with-deps chromium
107
node tool/test_web_runtime.cjs build/web
108
+ - name: Check exclusive web instance before session and cache access
109
+ run: |
110
+ node tool/test_web_instance.cjs
111
+ CHROME_EXECUTABLE="$(node -p 'require("playwright").chromium.executablePath()')" flutter test --platform chrome test/web/instance_guard_browser.dart --reporter expanded
112
+ node tool/test_web_instance.cjs --compiled build/web
113
+ - name: Preserve unsent changes across forced session expiry
114
+ run: node tool/test_web_session_expiry.cjs --artifact=build/web
115
- name: Record checked prebuilt web provenance
116
run: node ../../scripts/prebuilt-web-check.mjs . --stamp-after-build
117
- uses: actions/upload-artifact@v4
@@ -92,5 +120,14 @@ jobs:
120
run: |
121
flutter build apk --debug
122
cp build/app/outputs/flutter-apk/app-debug.apk build/LifeGuide-debug.apk
123
+ - name: Inspect the actual APK manifest and debug signature
124
+ run: |
125
+ python3 tool/check_apk_manifest.py build/LifeGuide-debug.apk --mode debug
126
+ sdk_root="${ANDROID_SDK_ROOT:-${ANDROID_HOME:-}}"
127
+ test -n "$sdk_root"
128
+ signer=$(find "$sdk_root/build-tools" -mindepth 2 -maxdepth 2 -type f -name apksigner | sort -V | tail -n 1)
129
+ test -n "$signer"
130
+ "$signer" verify --verbose --print-certs build/LifeGuide-debug.apk | tee build/debug-signature-check.txt
131
+ grep -F 'CN=Android Debug' build/debug-signature-check.txt
132
- uses: actions/upload-artifact@v4
133
with: {name: LifeGuide-debug-apk, path: apps/lifemate/build/LifeGuide-debug.apk, if-no-files-found: error}
apps/lifemate/tool/check_apk_manifest.py
new
+103
@@ -0,0 +1,103 @@
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))