Modern Apps git

← Modern-Apps.git

health: reject invalid FHIR dose times, guard sleep JSON

Commit
da3be06e87fb1977af4fe288cfc34bab09326dc2
Author
Vayun Mathur <[email protected]>
When
2026-11-19 02:05 UTC (17 days ago)
Parents
cded377
Two crash paths on untrusted or persisted data:

1. FhirMedication.parseTimeOfDay accepted out-of-range clock times
   from imported FHIR MedicationRequests (e.g. timeOfDay 24:00 or
   08:60) and converted them to seconds-since-midnight anyway. The
   value was stored on the MedicationSchedule and later rendered by
   ScheduleSection with LocalTime.fromSecondOfDay, which throws
   DateTimeException outside 0..86399, crashing the medication
   editor. parseTimeOfDay now rejects hours outside 0..23 and
   minutes/seconds outside 0..59, like any other unparseable value,
   and MedicalViewModel.startMedicationDraft filters already-stored
   out-of-range times (falling back to the default dose time) so
   rows imported before this fix cannot crash the editor either.
   DoseSchedule already ignored such times when arming alarms.

2. SleepStageGraph decoded the Room-persisted stagesJson with a
   bare Json.decodeFromString during composition, so an
   undecodable payload (written by another build) threw
   SerializationException and crashed the sleep details screen.
   It now uses runCatching and hides the graph, matching the guard
   the exercise segments/laps parsing already had.

Verified with ./gradlew :health:testDevUnitTest (green) and
./check health (detekt and lint green).

Diff

health/src/main/java/com/vayunmathur/health/domain/FhirMedication.kt
diff --git a/health/src/main/java/com/vayunmathur/health/domain/FhirMedication.kt b/health/src/main/java/com/vayunmathur/health/domain/FhirMedication.ktindex 7db383b6e7..ddcb5ee12c 100644--- a/health/src/main/java/com/vayunmathur/health/domain/FhirMedication.kt+++ b/health/src/main/java/com/vayunmathur/health/domain/FhirMedication.kt@@ -349,6 +349,12 @@ private fun parseTimeOfDay(value: String): Int? {     val h = parts[0].toIntOrNull() ?: return null     val m = parts[1].toIntOrNull() ?: return null     val s = parts.getOrNull(2)?.substringBefore('.')?.toIntOrNull() ?: 0+    // FHIR timings come from other systems; an out-of-range clock time such as+    // "24:00" or "08:60" is not a dose time. Returning it as seconds-since-+    // midnight would be stored on the schedule and later crash the editor's+    // LocalTime.fromSecondOfDay rendering, so reject it like any other+    // unparseable value.+    if (h !in 0..23 || m !in 0..59 || s !in 0..59) return null     return h * 3600 + m * 60 + s } 
health/src/main/java/com/vayunmathur/health/platform/MedicalViewModel.kt
diff --git a/health/src/main/java/com/vayunmathur/health/platform/MedicalViewModel.kt b/health/src/main/java/com/vayunmathur/health/platform/MedicalViewModel.ktindex 9650b6d97f..0457e01def 100644--- a/health/src/main/java/com/vayunmathur/health/platform/MedicalViewModel.kt+++ b/health/src/main/java/com/vayunmathur/health/platform/MedicalViewModel.kt@@ -80,6 +80,9 @@ class MedicalViewModel(     companion object {         /** 9am in seconds since midnight: the default time of a new reminder schedule. */         internal const val DEFAULT_DOSE_TIME_SECONDS = 9 * 60 * 60++        /** Seconds in a day: dose times are valid in `0 until SECONDS_PER_DAY`. */+        internal const val SECONDS_PER_DAY = 24 * 60 * 60     }      val vaccinations: StateFlow<List<VaccinationEntry>> = repository.getVaccinationsFlow()@@ -380,7 +383,12 @@ class MedicalViewModel(                 // what lets reactivating the medication bring its reminders back.                 remindersEnabled = schedule != null &&                     (schedule.enabled || entry.status != MedicationStatus.Active),-                times = schedule?.times?.takeIf { it.isNotEmpty() }+                // Drop out-of-range dose times stored by older imports: the+                // editor renders each with LocalTime.fromSecondOfDay, which+                // throws outside 0..86399. DoseSchedule already ignores them+                // when arming; this keeps the editor consistent with that.+                times = schedule?.times?.filter { it in 0 until SECONDS_PER_DAY }+                    ?.takeIf { it.isNotEmpty() }                     ?: listOf(DEFAULT_DOSE_TIME_SECONDS),                 repeatUnit = schedule?.repeatUnit ?: RepeatUnit.Daily,                 interval = schedule?.interval ?: 1,
health/src/main/java/com/vayunmathur/health/ui/SleepStageGraph.kt
diff --git a/health/src/main/java/com/vayunmathur/health/ui/SleepStageGraph.kt b/health/src/main/java/com/vayunmathur/health/ui/SleepStageGraph.ktindex db5ee6a837..b878f73f2e 100644--- a/health/src/main/java/com/vayunmathur/health/ui/SleepStageGraph.kt+++ b/health/src/main/java/com/vayunmathur/health/ui/SleepStageGraph.kt@@ -33,8 +33,13 @@ import kotlinx.serialization.json.Json @Composable fun SleepStageGraph(record: Record) {     val stages = remember(record) {-        record.sleepData?.stagesJson?.let { Json.decodeFromString<List<SleepStage>>(it) }-            ?: emptyList()+        // stagesJson is persisted in Room; a payload written by another build+        // (or otherwise undecodable) must hide the graph, not throw+        // SerializationException during composition — the same guard the+        // exercise segments/laps parsing already uses.+        record.sleepData?.stagesJson?.let {+            runCatching { Json.decodeFromString<List<SleepStage>>(it) }.getOrNull()+        } ?: emptyList()     }      if (stages.isEmpty()) return