diff --git a/AGENTS.md b/AGENTS.md index 8fe017d5..3306cd92 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -817,6 +817,12 @@ repairs phones already restored that way (entries present, Drinks never switched decides whether data shows, a plan, a log kept as JSON) **goes on the `ArchivedSettings` allowlist in the same change.** Device state stays off it: ring pairing, Health Connect grants and watermarks, notification scheduling, algorithm and scoring versions. +- **The encrypted store `pulseloop_secure` rides by key, not by file** (`SECURE_KEYS`): it mixes + the user's choices with this phone's state. The coach's whole setup is carried at the owner's + request, **API keys included** (every provider's key and model, and the custom API's address, + key and model), plus calibration, physiology, units and check-in hours. The archive is plain + JSON, so the Backup screen warns that the file exposes those keys. Don't drop the keys to make + the file "safer" without asking: losing the coach setup on a phone change is what was reported. - **The EPDS answers are deliberately not carried** (`postpartum`'s `epds` key; see postpartum mode). The life stage is. - `body_measurements` and `findings` are deliberately not in the archive either: Health Connect diff --git a/app/src/main/assets/i18n/pt-BR.json b/app/src/main/assets/i18n/pt-BR.json index c0a659a6..11ee3f47 100644 --- a/app/src/main/assets/i18n/pt-BR.json +++ b/app/src/main/assets/i18n/pt-BR.json @@ -225,6 +225,7 @@ "Backend proxy": "Proxy de backend", "Backing up…": "Fazendo backup…", "Backup & data": "Backup e dados", + "Backups include your AI coach setup and its API keys, readable by anyone with the file. Keep backup files private.": "Os backups incluem a configuração do coach de IA e as chaves de API, legíveis por quem tiver o arquivo. Guarde os arquivos de backup em local privado.", "Balance Macros (30/40/30)": "Equilibrar macros (30/40/30)", "Banana": "Banana", "Barcode": "Código de barras", diff --git a/app/src/main/java/com/pulseloop/data/ArchivedSettings.kt b/app/src/main/java/com/pulseloop/data/ArchivedSettings.kt index dea3eb20..231c078a 100644 --- a/app/src/main/java/com/pulseloop/data/ArchivedSettings.kt +++ b/app/src/main/java/com/pulseloop/data/ArchivedSettings.kt @@ -43,10 +43,51 @@ object ArchivedSettings { "rest_mode" to emptySet(), ) + /** + * The encrypted store (`pulseloop_secure`, shared by `ApiKeyStore` and + * `CoachProviderSettingsStore`). It mixes the user's choices with this phone's own state, so + * only these keys are carried, and a restore writes only these keys. + * + * The coach's setup is here at the owner's request, **its API keys included**: the provider, + * every provider's key and model, and the custom (local / own server) API's address, key, + * model and options. The archive is plain JSON, so those keys are readable by anyone holding + * the backup file; the Backup screen says so. + */ + private const val SECURE_FILE = "pulseloop_secure" + private val SECURE_KEYS: Set = setOf( + // AI coach: switches, provider, keys, models, the custom API. + "coach_enabled", "coach_model", "openai_api_key", "coach_provider_mode", + "gemini_api_key", "gemini_model", "openrouter_api_key", "openrouter_model", + "minimax_api_key", "minimax_model", "openrouter_privacy_routing", "openrouter_provider_sort", + "coach_reasoning_effort", "coach_image_input_enabled", "web_search_enabled", + "write_tools_enabled", "environment_context_enabled", + "local_llm_base_url", "local_llm_api_key", "local_llm_model", "local_llm_tool_calling", + "local_llm_structured_output", "local_llm_max_tokens", "local_llm_timeout_seconds", + // Coach check-ins and their hours. + "notifications_enabled", "morning_hour", "evening_hour", + // Calibration and physiology change how stored readings are read. + "bp_adjust_systolic", "bp_adjust_diastolic", "glucose_offset_mgdl", "glucose_ref_mgdl", + "athlete_mode", "altitude_meters", "uses_beta_blockers", "has_known_lung_condition", + // Units and the other switches the user set. + "unit_system", "preferred_glucose_unit", "battery_alerts_enabled", "live_measurements_enabled", + ) + fun export(context: Context): Map> = - FILES.mapValues { (file, skipped) -> + (FILES.mapValues { (file, skipped) -> prefs(context, file).all.filterKeys { it !in skipped }.mapNotNull { (k, v) -> encode(v)?.let { k to it } }.toMap() - }.filterValues { it.isNotEmpty() } + } + (SECURE_FILE to secureValues(context))).filterValues { it.isNotEmpty() } + + private fun secureValues(context: Context): Map = runCatching { + secure(context).all.filterKeys { it in SECURE_KEYS }.mapNotNull { (k, v) -> encode(v)?.let { k to it } }.toMap() + }.getOrDefault(emptyMap()) + + private fun secure(context: Context): SharedPreferences = androidx.security.crypto.EncryptedSharedPreferences.create( + SECURE_FILE, + androidx.security.crypto.MasterKeys.getOrCreate(androidx.security.crypto.MasterKeys.AES256_GCM_SPEC), + context.applicationContext, + androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM, + ) /** * Writes the archive's values back. Only files on the allowlist are touched, and each is @@ -62,6 +103,16 @@ object ArchivedSettings { values.filterKeys { it !in skipped }.forEach { (k, v) -> put(e, k, v) } e.commit() } + // The secure file keeps this phone's own keys: only the carried ones are replaced. + settings[SECURE_FILE]?.let { values -> + runCatching { + val p = secure(context) + val e = p.edit() + SECURE_KEYS.filter { it in p.all && it !in values }.forEach { e.remove(it) } + values.filterKeys { it in SECURE_KEYS }.forEach { (k, v) -> put(e, k, v) } + e.commit() + } + } } private fun prefs(context: Context, file: String): SharedPreferences = diff --git a/app/src/main/java/com/pulseloop/ui/screens/SettingsSubScreens.kt b/app/src/main/java/com/pulseloop/ui/screens/SettingsSubScreens.kt index 84948a73..5164bff4 100644 --- a/app/src/main/java/com/pulseloop/ui/screens/SettingsSubScreens.kt +++ b/app/src/main/java/com/pulseloop/ui/screens/SettingsSubScreens.kt @@ -2482,6 +2482,13 @@ fun PrivacyDataSettingsScreen( style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, ) + Spacer(Modifier.height(8.dp)) + // The archive carries the coach's API keys (data/ArchivedSettings.kt) in plain JSON. + Text( + tr("Backups include your AI coach setup and its API keys, readable by anyone with the file. Keep backup files private."), + style = MaterialTheme.typography.bodySmall, + color = com.pulseloop.ui.theme.PulseColors.warning, + ) Spacer(Modifier.height(12.dp)) Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) { Button(