LifeMate — Personal, Family & Learning Companion
feat/personal-encrypted-recovery
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 | """Synthetic-only tests of the versioned, authenticated recovery envelope.""" |
| 2 | |
| 3 | import importlib |
| 4 | import unittest |
| 5 | from unittest.mock import patch |
| 6 | |
| 7 | from cryptography.hazmat.primitives.ciphers.aead import AESGCM |
| 8 | from cryptography.hazmat.primitives.kdf.scrypt import Scrypt |
| 9 | |
| 10 | from scripts.personal_recovery.common import MAX_PAYLOAD_BYTES, RecoveryError |
| 11 | |
| 12 | |
| 13 | MAGIC = b"LGRF\x01" |
| 14 | HEADER_SIZE = 33 |
| 15 | TAG_SIZE = 16 |
| 16 | PASSWORD = "fictional recovery phrase" |
| 17 | |
| 18 | |
| 19 | class RecoveryArchiveTests(unittest.TestCase): |
| 20 | def setUp(self): |
| 21 | try: |
| 22 | self.archive = importlib.import_module("scripts.personal_recovery.archive") |
| 23 | except ModuleNotFoundError: |
| 24 | self.fail("Recovery archive implementation is missing") |
| 25 | |
| 26 | def assert_refused_before_kdf(self, operation, value, password, code): |
| 27 | with patch.object(self.archive, "Scrypt") as kdf: |
| 28 | with self.assertRaises(RecoveryError) as caught: |
| 29 | operation(value, password) |
| 30 | self.assertEqual(caught.exception.code, code) |
| 31 | self.assertEqual(str(caught.exception), code) |
| 32 | kdf.assert_not_called() |
| 33 | |
| 34 | def test_round_trip_arbitrary_bytes(self): |
| 35 | payload = b"fictional-only\x00\xff\x80" * 100 |
| 36 | encrypted = self.archive.seal(payload, PASSWORD) |
| 37 | self.assertEqual(self.archive.open_archive(encrypted, PASSWORD), payload) |
| 38 | self.assertNotIn(payload, encrypted) |
| 39 | |
| 40 | def test_empty_payload_round_trip(self): |
| 41 | encrypted = self.archive.seal(b"", PASSWORD) |
| 42 | self.assertEqual(len(encrypted), HEADER_SIZE + TAG_SIZE) |
| 43 | self.assertEqual(self.archive.open_archive(encrypted, PASSWORD), b"") |
| 44 | |
| 45 | def test_format_and_fixed_crypto_parameters_with_independent_decryption(self): |
| 46 | payload = b"fictional independent compatibility fixture" |
| 47 | encrypted = self.archive.seal(payload, PASSWORD) |
| 48 | self.assertEqual(encrypted[:5], MAGIC) |
| 49 | self.assertEqual(len(encrypted), len(payload) + HEADER_SIZE + TAG_SIZE) |
| 50 | key = Scrypt(salt=encrypted[5:21], length=32, n=32768, r=8, p=1).derive( |
| 51 | PASSWORD.encode("utf-8") |
| 52 | ) |
| 53 | self.assertEqual( |
| 54 | AESGCM(key).decrypt(encrypted[21:33], encrypted[33:], encrypted[:33]), |
| 55 | payload, |
| 56 | ) |
| 57 | |
| 58 | def test_open_independently_constructed_archive(self): |
| 59 | salt = bytes(range(16)) |
| 60 | nonce = bytes(range(12)) |
| 61 | header = MAGIC + salt + nonce |
| 62 | key = Scrypt(salt=salt, length=32, n=32768, r=8, p=1).derive( |
| 63 | PASSWORD.encode("utf-8") |
| 64 | ) |
| 65 | payload = b"fictional external envelope" |
| 66 | encrypted = header + AESGCM(key).encrypt(nonce, payload, header) |
| 67 | self.assertEqual(self.archive.open_archive(encrypted, PASSWORD), payload) |
| 68 | |
| 69 | def test_every_seal_uses_new_salt_and_nonce(self): |
| 70 | first = self.archive.seal(b"fictional", PASSWORD) |
| 71 | second = self.archive.seal(b"fictional", PASSWORD) |
| 72 | self.assertNotEqual(first[5:21], second[5:21]) |
| 73 | self.assertNotEqual(first[21:33], second[21:33]) |
| 74 | self.assertNotEqual(first, second) |
| 75 | |
| 76 | def test_wrong_password_and_tampering_share_safe_authentication_error(self): |
| 77 | encrypted = self.archive.seal(b"fictional integrity fixture", PASSWORD) |
| 78 | cases = [(encrypted, "another fictional password")] |
| 79 | for index in (5, 20, 21, 32, 33, len(encrypted) - 1): |
| 80 | changed = bytearray(encrypted) |
| 81 | changed[index] ^= 1 |
| 82 | cases.append((bytes(changed), PASSWORD)) |
| 83 | cases.append((encrypted + b"\x00", PASSWORD)) |
| 84 | cases.append((encrypted[:-1], PASSWORD)) |
| 85 | for changed, password in cases: |
| 86 | with self.subTest(case=len(changed), password_matches=password == PASSWORD): |
| 87 | with self.assertRaises(RecoveryError) as caught: |
| 88 | self.archive.open_archive(changed, password) |
| 89 | self.assertEqual(caught.exception.code, "archive_authentication_failed") |
| 90 | self.assertEqual(str(caught.exception), "archive_authentication_failed") |
| 91 | self.assertIsNone(caught.exception.__cause__) |
| 92 | self.assertTrue(caught.exception.__suppress_context__) |
| 93 | |
| 94 | def test_header_magic_and_version_rejected_before_kdf(self): |
| 95 | valid_size = MAGIC + bytes(16 + 12 + TAG_SIZE) |
| 96 | for index in range(5): |
| 97 | changed = bytearray(valid_size) |
| 98 | changed[index] ^= 1 |
| 99 | with self.subTest(index=index): |
| 100 | self.assert_refused_before_kdf( |
| 101 | self.archive.open_archive, bytes(changed), PASSWORD, "invalid_archive" |
| 102 | ) |
| 103 | |
| 104 | def test_truncated_archive_rejected_before_kdf(self): |
| 105 | for size in (0, 4, 5, 20, 21, 32, 33, 48): |
| 106 | with self.subTest(size=size): |
| 107 | self.assert_refused_before_kdf( |
| 108 | self.archive.open_archive, (MAGIC + bytes(44))[:size], |
| 109 | PASSWORD, "invalid_archive" |
| 110 | ) |
| 111 | |
| 112 | def test_oversized_archive_rejected_before_kdf(self): |
| 113 | encrypted = MAGIC + bytes(MAX_PAYLOAD_BYTES + HEADER_SIZE + TAG_SIZE - 4) |
| 114 | self.assertEqual(len(encrypted), MAX_PAYLOAD_BYTES + HEADER_SIZE + TAG_SIZE + 1) |
| 115 | self.assert_refused_before_kdf( |
| 116 | self.archive.open_archive, encrypted, PASSWORD, "invalid_archive" |
| 117 | ) |
| 118 | |
| 119 | def test_oversized_payload_rejected_before_kdf(self): |
| 120 | self.assert_refused_before_kdf( |
| 121 | self.archive.seal, bytes(MAX_PAYLOAD_BYTES + 1), PASSWORD, "invalid_payload" |
| 122 | ) |
| 123 | |
| 124 | def test_exact_payload_boundary_round_trip(self): |
| 125 | payload = b"f" * MAX_PAYLOAD_BYTES |
| 126 | encrypted = self.archive.seal(payload, PASSWORD) |
| 127 | self.assertEqual(len(encrypted), MAX_PAYLOAD_BYTES + HEADER_SIZE + TAG_SIZE) |
| 128 | self.assertEqual(self.archive.open_archive(encrypted, PASSWORD), payload) |
| 129 | |
| 130 | def test_invalid_passphrase_rejected_before_kdf_in_both_directions(self): |
| 131 | encrypted = MAGIC + bytes(16 + 12 + TAG_SIZE) |
| 132 | for password in ("", "x" * 11, "x" * 1025, "\u0628" * 513, "x" * 12 + "\ud800", None, b"fictional password"): |
| 133 | for operation, value in ((self.archive.seal, b"fixture"), (self.archive.open_archive, encrypted)): |
| 134 | with self.subTest(operation=operation.__name__, password_type=type(password).__name__): |
| 135 | self.assert_refused_before_kdf(operation, value, password, "invalid_passphrase") |
| 136 | |
| 137 | def test_minimum_character_and_maximum_utf8_passphrase_boundaries(self): |
| 138 | for password in ("x" * 12, "x" * 1024, "\u0628" * 512): |
| 139 | with self.subTest(characters=len(password)): |
| 140 | encrypted = self.archive.seal(b"fictional boundary", password) |
| 141 | self.assertEqual(self.archive.open_archive(encrypted, password), b"fictional boundary") |
| 142 | |
| 143 | def test_unicode_passphrase_is_used_without_normalization(self): |
| 144 | composed = "fictional-\u00e9-phrase" |
| 145 | decomposed = "fictional-e\u0301-phrase" |
| 146 | encrypted = self.archive.seal("ساختگی".encode("utf-8"), composed) |
| 147 | self.assertEqual(self.archive.open_archive(encrypted, composed), "ساختگی".encode("utf-8")) |
| 148 | with self.assertRaises(RecoveryError) as caught: |
| 149 | self.archive.open_archive(encrypted, decomposed) |
| 150 | self.assertEqual(caught.exception.code, "archive_authentication_failed") |
| 151 | |
| 152 | def test_character_count_not_utf8_length_controls_passphrase_minimum(self): |
| 153 | self.assert_refused_before_kdf( |
| 154 | self.archive.seal, b"fictional", "\u0628" * 6, "invalid_passphrase" |
| 155 | ) |
| 156 | |
| 157 | def test_nonbytes_payloads_and_archives_are_safely_rejected_before_kdf(self): |
| 158 | for value in (None, "fictional", bytearray(b"fictional"), memoryview(b"fictional"), 4): |
| 159 | with self.subTest(value_type=type(value).__name__): |
| 160 | self.assert_refused_before_kdf(self.archive.seal, value, PASSWORD, "invalid_payload") |
| 161 | self.assert_refused_before_kdf(self.archive.open_archive, value, PASSWORD, "invalid_archive") |
| 162 | |
| 163 | |
| 164 | if __name__ == "__main__": |
| 165 | unittest.main() |