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 | """Bounded reads and no-overwrite publication for fictional QA files only.""" |
| 2 | |
| 3 | import os |
| 4 | from pathlib import Path |
| 5 | import stat |
| 6 | import tempfile |
| 7 | |
| 8 | from .common import RecoveryError |
| 9 | |
| 10 | |
| 11 | def read_bounded(path: Path, limit: int, *, prefix_validator=None) -> bytes: |
| 12 | """Refuse symlinks/nonregular files; never read unbounded input into memory.""" |
| 13 | path = Path(path) |
| 14 | descriptor = None |
| 15 | try: |
| 16 | before = path.lstat() |
| 17 | if not stat.S_ISREG(before.st_mode) or before.st_size > limit: |
| 18 | raise RecoveryError("invalid_input_file") |
| 19 | descriptor = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_NONBLOCK", 0)) |
| 20 | info = os.fstat(descriptor) |
| 21 | if (not stat.S_ISREG(info.st_mode) or info.st_size > limit |
| 22 | or (before.st_dev, before.st_ino) != (info.st_dev, info.st_ino)): |
| 23 | raise RecoveryError("invalid_input_file") |
| 24 | with os.fdopen(descriptor, "rb", buffering=0) as stream: |
| 25 | descriptor = None |
| 26 | prefix = stream.read(min(limit + 1, 100)) |
| 27 | if prefix_validator is not None: |
| 28 | prefix_validator(prefix) |
| 29 | data = prefix + stream.read(limit + 1 - len(prefix)) |
| 30 | if len(data) > limit: |
| 31 | raise RecoveryError("invalid_input_file") |
| 32 | return data |
| 33 | except OSError: |
| 34 | raise RecoveryError("file_io_error") from None |
| 35 | finally: |
| 36 | if descriptor is not None: |
| 37 | os.close(descriptor) |
| 38 | |
| 39 | |
| 40 | def publish_new(destination: Path, payload: bytes) -> None: |
| 41 | """Link a complete mode0600 temp file atomically; existing paths always win. |
| 42 | |
| 43 | A hard-link-capable local filesystem is required. No overwrite fallback is |
| 44 | permitted. fsync completes before publication; power-loss durability of the |
| 45 | directory and platform-specific filesystem behavior are not asserted here. |
| 46 | """ |
| 47 | destination = Path(destination) |
| 48 | temporary = None |
| 49 | descriptor = None |
| 50 | try: |
| 51 | if os.path.lexists(destination): |
| 52 | raise RecoveryError("destination_exists") |
| 53 | descriptor, name = tempfile.mkstemp(prefix=".lifeguide-qa-", dir=destination.parent) |
| 54 | temporary = Path(name) |
| 55 | with os.fdopen(descriptor, "wb") as stream: |
| 56 | descriptor = None |
| 57 | stream.write(payload) |
| 58 | stream.flush() |
| 59 | os.fsync(stream.fileno()) |
| 60 | os.link(temporary, destination) |
| 61 | except FileExistsError: |
| 62 | raise RecoveryError("destination_exists") from None |
| 63 | except OSError: |
| 64 | raise RecoveryError("file_io_error") from None |
| 65 | finally: |
| 66 | if descriptor is not None: |
| 67 | os.close(descriptor) |
| 68 | if temporary is not None: |
| 69 | temporary.unlink(missing_ok=True) |