LifeMate — Personal, Family & Learning Companion
feat/local-signing-preflight
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 | """Read-only signing preparation; all values and files are synthetic.""" |
| 2 | |
| 3 | from contextlib import redirect_stdout |
| 4 | import getpass |
| 5 | import hashlib |
| 6 | import io |
| 7 | import json |
| 8 | import os |
| 9 | from pathlib import Path |
| 10 | import struct |
| 11 | import subprocess |
| 12 | import tempfile |
| 13 | import unittest |
| 14 | from unittest.mock import patch |
| 15 | |
| 16 | from scripts import signing_preflight as tool |
| 17 | |
| 18 | |
| 19 | FACTS = {"schemaVersion": 1, "status": "INSPECTED", "certificateSha256": "a" * 64, |
| 20 | "debugCertificate": False, "privateKeyProof": True, |
| 21 | "certificateCurrentlyValid": True} |
| 22 | CANARY = "FICTIONAL_SECRET_NEVER_PRINT" |
| 23 | |
| 24 | |
| 25 | class SigningPreflightTest(unittest.TestCase): |
| 26 | def call(self, arguments, **patches): |
| 27 | output = io.StringIO() |
| 28 | with redirect_stdout(output): |
| 29 | result = tool.main(arguments) |
| 30 | return result, json.loads(output.getvalue()) |
| 31 | |
| 32 | def test_default_never_reads_key_or_runs_key_inspection(self): |
| 33 | with patch.object(tool, "_java_available", return_value=True), \ |
| 34 | patch.object(tool, "read_bounded", side_effect=AssertionError("private IO")), \ |
| 35 | patch.object(tool, "_run_inspector", side_effect=AssertionError("inspection")): |
| 36 | code, report = self.call([]) |
| 37 | self.assertEqual(code, 0) |
| 38 | self.assertEqual(report["status"], "CHECKED") |
| 39 | self.assertEqual(report["keyInspection"], "UNVERIFIED") |
| 40 | self.assertEqual(report["delivery"], "BLOCKED") |
| 41 | |
| 42 | def test_missing_java_is_refused(self): |
| 43 | with patch.object(tool, "_java_available", return_value=False): |
| 44 | code, report = self.call(["check"]) |
| 45 | self.assertEqual(code, 2) |
| 46 | self.assertEqual(report["reason"], "jdk17_required") |
| 47 | |
| 48 | def test_source_launcher_checks_compiler_module_without_javac_executable(self): |
| 49 | for modules, expected in ((b"java.base@21\njdk.compiler@21\n", True), |
| 50 | (b"java.base@21\n", False)): |
| 51 | with self.subTest(expected=expected), \ |
| 52 | patch.object(tool.shutil, "which", side_effect=lambda name: "/synthetic/java" if name == "java" else None), \ |
| 53 | patch.object(tool.subprocess, "run", side_effect=[ |
| 54 | subprocess.CompletedProcess([], 0, b'openjdk version "21.0.1"\n'), |
| 55 | subprocess.CompletedProcess([], 0, modules), |
| 56 | subprocess.CompletedProcess([], 2, b'{"schemaVersion":1,"status":"REFUSED","reason":"invalid_request"}')]): |
| 57 | self.assertEqual(tool._java_available(), expected) |
| 58 | |
| 59 | def test_compiler_module_without_source17_support_is_refused(self): |
| 60 | with patch.object(tool.shutil, "which", return_value="/synthetic/java"), \ |
| 61 | patch.object(tool.subprocess, "run", side_effect=[ |
| 62 | subprocess.CompletedProcess([], 0, b'openjdk version "21.0.1"\n'), |
| 63 | subprocess.CompletedProcess([], 0, b'jdk.compiler@21\n'), |
| 64 | subprocess.CompletedProcess([], 1, b'unsupported source release')]): |
| 65 | self.assertFalse(tool._java_available()) |
| 66 | |
| 67 | def test_acknowledgement_before_private_io_or_prompt(self): |
| 68 | with patch.object(tool, "read_bounded", side_effect=AssertionError("private IO")), \ |
| 69 | patch.object(tool, "_passwords", side_effect=AssertionError("prompt")): |
| 70 | code, report = self.call(["inspect", "--keystore", CANARY, "--alias", CANARY]) |
| 71 | self.assertEqual(code, 2) |
| 72 | self.assertEqual(report["reason"], "owner_local_acknowledgement_required") |
| 73 | self.assertNotIn(CANARY, json.dumps(report)) |
| 74 | |
| 75 | def test_noninteractive_before_private_io_or_helper(self): |
| 76 | with patch.object(tool.sys.stdin, "isatty", return_value=False), \ |
| 77 | patch.object(tool, "read_bounded", side_effect=AssertionError("private IO")), \ |
| 78 | patch.object(tool, "_run_inspector", side_effect=AssertionError("helper")): |
| 79 | code, report = self.call(["inspect", "--keystore", CANARY, "--alias", CANARY, |
| 80 | "--acknowledge-owner-local-inspection"]) |
| 81 | self.assertEqual(code, 2) |
| 82 | self.assertEqual(report["reason"], "interactive_terminal_required") |
| 83 | |
| 84 | def test_getpass_echo_fallback_refused(self): |
| 85 | with patch.object(tool.sys.stdin, "isatty", return_value=True), \ |
| 86 | patch.object(tool.getpass, "getpass", side_effect=getpass.GetPassWarning(CANARY)): |
| 87 | with self.assertRaises(tool.PreflightError) as failure: |
| 88 | tool._passwords() |
| 89 | self.assertEqual(failure.exception.code, "interactive_terminal_required") |
| 90 | |
| 91 | def test_invalid_jdk_refused_before_password_prompt_or_private_io(self): |
| 92 | with patch.object(tool.sys.stdin, "isatty", return_value=True), \ |
| 93 | patch.object(tool, "_java_available", return_value=False), \ |
| 94 | patch.object(tool, "_passwords", side_effect=AssertionError("password prompt")), \ |
| 95 | patch.object(tool, "read_bounded", side_effect=AssertionError("private IO")): |
| 96 | code, report = self.call(["inspect", "--keystore", CANARY, "--alias", CANARY, |
| 97 | "--acknowledge-owner-local-inspection"]) |
| 98 | self.assertEqual(code, 2) |
| 99 | self.assertEqual(report["reason"], "jdk17_required") |
| 100 | |
| 101 | def test_hidden_password_and_same_key_password(self): |
| 102 | with patch.object(tool.sys.stdin, "isatty", return_value=True), \ |
| 103 | patch.object(tool.getpass, "getpass", side_effect=[CANARY, ""]): |
| 104 | self.assertEqual(tool._passwords(), (CANARY, CANARY)) |
| 105 | |
| 106 | def test_separate_hidden_key_password(self): |
| 107 | with patch.object(tool.sys.stdin, "isatty", return_value=True), \ |
| 108 | patch.object(tool.getpass, "getpass", side_effect=[CANARY, CANARY + "2"]): |
| 109 | self.assertEqual(tool._passwords(), (CANARY, CANARY + "2")) |
| 110 | |
| 111 | def test_invalid_arguments_never_echo_values(self): |
| 112 | for arguments in (["--password", CANARY], ["inspect", "--bad", CANARY], |
| 113 | ["generate-key", CANARY], ["--baseline", CANARY], |
| 114 | ["inspect", "--alias"], ["--acknowledge-owner", CANARY]): |
| 115 | with self.subTest(arguments=arguments): |
| 116 | code, report = self.call(arguments) |
| 117 | self.assertEqual(code, 2) |
| 118 | self.assertEqual(report["reason"], "arguments_invalid") |
| 119 | self.assertNotIn(CANARY, json.dumps(report)) |
| 120 | |
| 121 | def test_baseline_invalid_is_sanitized(self): |
| 122 | with patch.object(tool, "load_baseline", side_effect=ValueError(CANARY)): |
| 123 | code, report = self.call([]) |
| 124 | self.assertEqual(code, 2) |
| 125 | self.assertEqual(report["reason"], "delivered_baseline_invalid") |
| 126 | |
| 127 | def test_fixture_inspection_reports_facts_but_never_delivery(self): |
| 128 | with tempfile.TemporaryDirectory() as directory: |
| 129 | source = Path(directory) / "synthetic-only.jks" |
| 130 | source.write_bytes(b"fictional-encrypted-key-placeholder") |
| 131 | before = source.read_bytes() |
| 132 | with patch.object(tool, "_run_inspector", return_value=FACTS): |
| 133 | report = tool.inspect_key(source, CANARY, CANARY, CANARY) |
| 134 | self.assertEqual(source.read_bytes(), before) |
| 135 | self.assertEqual(report["status"], "INSPECTED") |
| 136 | self.assertEqual(report["signerCompatibility"], "DIFFERENT") |
| 137 | self.assertTrue(report["privateKeyProof"]) |
| 138 | for key, value in {"delivery": "BLOCKED", "releaseReadiness": "UNVERIFIED", |
| 139 | "dataPreservation": "UNVERIFIED", |
| 140 | "installedDeviceVerification": "UNVERIFIED"}.items(): |
| 141 | self.assertEqual(report[key], value) |
| 142 | self.assertNotIn(CANARY, json.dumps(report)) |
| 143 | |
| 144 | def test_matching_debug_signer_does_not_unlock_release(self): |
| 145 | baseline = tool.load_baseline() |
| 146 | facts = dict(FACTS, certificateSha256=baseline["certificateSha256"], debugCertificate=True) |
| 147 | report = tool._inspection_report(facts, baseline) |
| 148 | self.assertEqual(report["signerCompatibility"], "MATCH") |
| 149 | self.assertIn("debug_certificate_not_for_release", report["reasonCodes"]) |
| 150 | self.assertEqual(report["delivery"], "BLOCKED") |
| 151 | self.assertEqual(report["releaseReadiness"], "UNVERIFIED") |
| 152 | |
| 153 | def test_alias_and_password_bounds_checked_before_file_io(self): |
| 154 | for alias, store, key in (("", CANARY, CANARY), ("x" * 257, CANARY, CANARY), |
| 155 | (CANARY, "", CANARY), (CANARY, "x" * 4097, CANARY), |
| 156 | (CANARY, CANARY, "\x00"), ("x\n", CANARY, CANARY)): |
| 157 | with self.subTest(aliasLength=len(alias), storeLength=len(store)), \ |
| 158 | patch.object(tool, "read_bounded", side_effect=AssertionError("file IO")): |
| 159 | with self.assertRaises(tool.PreflightError): |
| 160 | tool.inspect_key(Path("unused"), alias, store, key) |
| 161 | |
| 162 | def test_missing_directory_and_oversized_files_refused(self): |
| 163 | with tempfile.TemporaryDirectory() as directory: |
| 164 | root = Path(directory) |
| 165 | oversized = root / "large" |
| 166 | with oversized.open("wb") as stream: |
| 167 | stream.truncate(tool.MAX_KEYSTORE_BYTES + 1) |
| 168 | for path in (root / "missing", root, oversized): |
| 169 | with self.subTest(kind=path.name), \ |
| 170 | patch.object(tool, "_run_inspector", side_effect=AssertionError("helper")): |
| 171 | with self.assertRaises(tool.PreflightError): |
| 172 | tool.inspect_key(path, CANARY, CANARY, CANARY) |
| 173 | |
| 174 | @unittest.skipUnless(os.name == "posix", "POSIX link/FIFO guard") |
| 175 | def test_symlink_and_fifo_refused_before_helper(self): |
| 176 | with tempfile.TemporaryDirectory() as directory: |
| 177 | root = Path(directory) |
| 178 | source = root / "regular" |
| 179 | source.write_bytes(b"fictional") |
| 180 | link = root / "link" |
| 181 | link.symlink_to(source) |
| 182 | fifo = root / "fifo" |
| 183 | os.mkfifo(fifo) |
| 184 | with patch.object(tool, "_run_inspector", side_effect=AssertionError("helper")): |
| 185 | for path in (link, fifo): |
| 186 | with self.assertRaises(tool.PreflightError): |
| 187 | tool.inspect_key(path, CANARY, CANARY, CANARY) |
| 188 | self.assertEqual(source.read_bytes(), b"fictional") |
| 189 | |
| 190 | def test_binary_frame_no_secrets_in_child_arguments(self): |
| 191 | process = subprocess.CompletedProcess([], 0, json.dumps(FACTS).encode()) |
| 192 | with patch.object(tool.shutil, "which", return_value="/synthetic/java"), \ |
| 193 | patch.object(tool.subprocess, "run", return_value=process) as run: |
| 194 | self.assertEqual(tool._run_inspector(b"fictional", CANARY, CANARY, CANARY), FACTS) |
| 195 | args, options = run.call_args |
| 196 | self.assertNotIn(CANARY, repr(args)) |
| 197 | self.assertTrue(options["input"].startswith(b"LGKI")) |
| 198 | frame = options["input"][4:] |
| 199 | for value in (b"fictional", CANARY.encode(), CANARY.encode(), CANARY.encode()): |
| 200 | length = struct.unpack(">I", frame[:4])[0] |
| 201 | self.assertEqual(frame[4:4 + length], value) |
| 202 | frame = frame[4 + length:] |
| 203 | self.assertEqual(frame, b"") |
| 204 | self.assertEqual(options["timeout"], 30) |
| 205 | self.assertEqual(options["stderr"], subprocess.DEVNULL) |
| 206 | |
| 207 | def test_java_injection_and_arbitrary_secrets_not_forwarded(self): |
| 208 | bad = {key: CANARY for key in ("JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "JDK_JAVA_OPTIONS", |
| 209 | "CLASSPATH", "SYNTHETIC_UNRELATED_SECRET")} |
| 210 | with patch.dict(os.environ, bad): |
| 211 | environment = tool._java_environment() |
| 212 | for key in bad: |
| 213 | self.assertNotIn(key, environment) |
| 214 | |
| 215 | def test_malformed_helper_output_refused_without_echo(self): |
| 216 | for output in (CANARY.encode(), b"{}", json.dumps(dict(FACTS, surprise=CANARY)).encode(), |
| 217 | json.dumps(dict(FACTS, privateKeyProof=False)).encode(), |
| 218 | json.dumps(dict(FACTS, debugCertificate=1)).encode(), |
| 219 | json.dumps(dict(FACTS, certificateSha256="bad")).encode(), |
| 220 | b"x" * 4097): |
| 221 | with self.subTest(length=len(output)), \ |
| 222 | patch.object(tool.shutil, "which", return_value="/synthetic/java"), \ |
| 223 | patch.object(tool.subprocess, "run", return_value=subprocess.CompletedProcess([], 0, output)): |
| 224 | with self.assertRaises(tool.PreflightError) as failure: |
| 225 | tool._run_inspector(b"fictional", CANARY, CANARY, CANARY) |
| 226 | self.assertEqual(failure.exception.code, "inspector_invalid_response") |
| 227 | self.assertNotIn(CANARY, str(failure.exception)) |
| 228 | |
| 229 | def test_helper_refusal_whitelisted_only(self): |
| 230 | for reason, expected in (("keystore_unreadable", "keystore_unreadable"), |
| 231 | (CANARY, "inspector_invalid_response")): |
| 232 | output = json.dumps({"schemaVersion": 1, "status": "REFUSED", "reason": reason}).encode() |
| 233 | with patch.object(tool.shutil, "which", return_value="/synthetic/java"), \ |
| 234 | patch.object(tool.subprocess, "run", return_value=subprocess.CompletedProcess([], 2, output)): |
| 235 | with self.assertRaises(tool.PreflightError) as failure: |
| 236 | tool._run_inspector(b"fictional", CANARY, CANARY, CANARY) |
| 237 | self.assertEqual(failure.exception.code, expected) |
| 238 | |
| 239 | def test_timeout_or_subprocess_failure_sanitized(self): |
| 240 | for failure, expected in ((subprocess.TimeoutExpired(CANARY, 30), "inspector_timeout"), |
| 241 | (OSError(CANARY), "inspector_unavailable")): |
| 242 | with patch.object(tool.shutil, "which", return_value="/synthetic/java"), \ |
| 243 | patch.object(tool.subprocess, "run", side_effect=failure): |
| 244 | with self.assertRaises(tool.PreflightError) as result: |
| 245 | tool._run_inspector(b"fictional", CANARY, CANARY, CANARY) |
| 246 | self.assertEqual(result.exception.code, expected) |
| 247 | |
| 248 | def test_wrong_exit_code_cannot_approve(self): |
| 249 | with patch.object(tool.shutil, "which", return_value="/synthetic/java"), \ |
| 250 | patch.object(tool.subprocess, "run", return_value=subprocess.CompletedProcess([], 2, json.dumps(FACTS).encode())): |
| 251 | with self.assertRaises(tool.PreflightError): |
| 252 | tool._run_inspector(b"fictional", CANARY, CANARY, CANARY) |
| 253 | |
| 254 | |
| 255 | class SyntheticCLIIntegrationTest(unittest.TestCase): |
| 256 | def test_real_local_helper_hidden_prompt_port_and_source_preservation(self): |
| 257 | from scripts.tests.signing_test_support import synthetic_keystores |
| 258 | |
| 259 | with synthetic_keystores() as fixture: |
| 260 | before = hashlib.sha256(fixture.jks_path.read_bytes()).hexdigest() |
| 261 | output = io.StringIO() |
| 262 | with redirect_stdout(output), patch.object(tool.sys.stdin, "isatty", return_value=True), \ |
| 263 | patch.object(tool.getpass, "getpass", side_effect=[fixture.store_password, fixture.key_password]): |
| 264 | code = tool.main(["inspect", "--keystore", str(fixture.jks_path), "--alias", fixture.alias, |
| 265 | "--acknowledge-owner-local-inspection"]) |
| 266 | self.assertEqual(code, 0) |
| 267 | report = json.loads(output.getvalue()) |
| 268 | self.assertEqual(report["certificateSha256"], fixture.fingerprints[fixture.jks_path]) |
| 269 | self.assertEqual(report["delivery"], "BLOCKED") |
| 270 | self.assertEqual(report["status"], "INSPECTED") |
| 271 | self.assertEqual(hashlib.sha256(fixture.jks_path.read_bytes()).hexdigest(), before) |
| 272 | for private in (fixture.store_password, fixture.key_password, fixture.alias, str(fixture.jks_path)): |
| 273 | self.assertNotIn(private, output.getvalue()) |
| 274 | # File remains reopenable; independent second keypair proof, no edits. |
| 275 | repeated = tool.inspect_key(fixture.jks_path, fixture.alias, fixture.store_password, fixture.key_password) |
| 276 | self.assertEqual(repeated["certificateSha256"], report["certificateSha256"]) |
| 277 | self.assertEqual(repeated["privateKeyProof"], True) |
| 278 | |
| 279 | |
| 280 | if __name__ == "__main__": |
| 281 | unittest.main() |