diff --git a/AGENTS.md b/AGENTS.md index 3ddeb3a..9fede4d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -108,7 +108,10 @@ manifest disagree. `AutomationPropertiesTests` walks every helper's page. A helper's view model is created once per process and kept while the user visits other helpers, so a running timer keeps running. A helper whose state must survive the process being killed persists it itself, through -`HelperServices.Settings` (`IHelperSettings`). +`HelperServices.Settings` (`IHelperSettings`). Einsatz data, such as the +Hydrantenkarte's Einsatzstelle, goes through `HelperServices.DeviceOnlySettings` +instead: Android's backup copies `Settings` into the user's Google account, and +`BackupRulesTests` holds the line that it never copies the device-only file. ## Mobile diff --git a/changelog.d/+backup-einsatzstelle.security.md b/changelog.d/+backup-einsatzstelle.security.md new file mode 100644 index 0000000..9c501db --- /dev/null +++ b/changelog.d/+backup-einsatzstelle.security.md @@ -0,0 +1 @@ +The Hydrantenkarte's Einsatzstelle and the downloaded Kartenpakete no longer go into Android's cloud backup in the user's Google account. diff --git a/src/FwAssist.App.Android/AndroidManifest.xml b/src/FwAssist.App.Android/AndroidManifest.xml index 4ce8554..284540f 100644 --- a/src/FwAssist.App.Android/AndroidManifest.xml +++ b/src/FwAssist.App.Android/AndroidManifest.xml @@ -36,7 +36,14 @@ + + + + + diff --git a/src/FwAssist.App.Android/Resources/xml/data_extraction_rules.xml b/src/FwAssist.App.Android/Resources/xml/data_extraction_rules.xml new file mode 100644 index 0000000..d447eee --- /dev/null +++ b/src/FwAssist.App.Android/Resources/xml/data_extraction_rules.xml @@ -0,0 +1,11 @@ + + + + + + + + diff --git a/src/FwAssist.App.Android/Services/AndroidHelperSettings.cs b/src/FwAssist.App.Android/Services/AndroidHelperSettings.cs index 98de5e2..df1b7b7 100644 --- a/src/FwAssist.App.Android/Services/AndroidHelperSettings.cs +++ b/src/FwAssist.App.Android/Services/AndroidHelperSettings.cs @@ -3,17 +3,28 @@ namespace FwAssist.App.Android.Services; -/// Helper settings in the app's private SharedPreferences. Nothing leaves the phone. +/// +/// Helper settings in one of the app's private SharedPreferences files. The app sends none of it +/// anywhere; Android's own backup may copy , but never . +/// internal sealed class AndroidHelperSettings : IHelperSettings { - private const string FileName = "helper-settings"; + /// Small preferences that Android's backup may carry to a new phone. + public const string BackedUp = "helper-settings"; + + /// + /// Einsatz data. Resources/xml/data_extraction_rules.xml and backup_rules.xml exclude this file + /// by name, as helper-settings-device.xml; rename it only together with them. + /// + public const string DeviceOnly = "helper-settings-device"; private readonly ISharedPreferences _preferences; - public AndroidHelperSettings(Context context) + public AndroidHelperSettings(Context context, string fileName) { ArgumentNullException.ThrowIfNull(context); - _preferences = context.GetSharedPreferences(FileName, FileCreationMode.Private) + ArgumentException.ThrowIfNullOrEmpty(fileName); + _preferences = context.GetSharedPreferences(fileName, FileCreationMode.Private) ?? throw new InvalidOperationException("Android returned no SharedPreferences."); } diff --git a/src/FwAssist.AppLogic/Helpers/HelperServices.cs b/src/FwAssist.AppLogic/Helpers/HelperServices.cs index 728ca24..84761d7 100644 --- a/src/FwAssist.AppLogic/Helpers/HelperServices.cs +++ b/src/FwAssist.AppLogic/Helpers/HelperServices.cs @@ -15,6 +15,10 @@ namespace FwAssist.AppLogic.Helpers; /// The phone's live position; null where the platform offers none. /// Which way the phone points; null where the platform offers none. /// A tone and vibration for something the user must notice; null stays quiet. +/// +/// Like , for values that must never leave the phone, not even into +/// Android's backup: Einsatz data such as the Hydrantenkarte's Einsatzstelle. Null keeps nothing. +/// public sealed record HelperServices( TimeProvider Time, ITicker Ticker, @@ -22,4 +26,5 @@ public sealed record HelperServices( IRegionPackLibrary? Maps = null, ILocationSource? Location = null, ICompass? Compass = null, - IAttentionSignal? Signal = null); + IAttentionSignal? Signal = null, + IHelperSettings? DeviceOnlySettings = null); diff --git a/src/Helpers/Hydranten/FwAssist.Helpers.Hydranten/HydrantenModule.cs b/src/Helpers/Hydranten/FwAssist.Helpers.Hydranten/HydrantenModule.cs index 158027a..8d745ea 100644 --- a/src/Helpers/Hydranten/FwAssist.Helpers.Hydranten/HydrantenModule.cs +++ b/src/Helpers/Hydranten/FwAssist.Helpers.Hydranten/HydrantenModule.cs @@ -1,5 +1,6 @@ using CommunityToolkit.Mvvm.ComponentModel; using FwAssist.AppLogic.Helpers; +using FwAssist.AppLogic.Services; namespace FwAssist.Helpers.Hydranten; @@ -20,6 +21,25 @@ public sealed class HydrantenModule : IHelperModule public ObservableObject CreateViewModel(HelperServices services) { ArgumentNullException.ThrowIfNull(services); - return new HydrantenkarteViewModel(services.Maps, services.Settings, services.Location, services.Compass, services.Time, services.Signal); + MoveEinsatzstelleOutOfBackup(services.Settings, services.DeviceOnlySettings); + return new HydrantenkarteViewModel(services.Maps, services.DeviceOnlySettings, services.Location, services.Compass, services.Time, services.Signal); + } + + // Up to 0.2.1 the Einsatzstelle sat in the settings Android backs up. A copy found there moves + // to the device-only settings, unless those already hold a newer one, and is blanked either way: + // IHelperSettings cannot remove a key, and empty is what the view model writes for "none". + private static void MoveEinsatzstelleOutOfBackup(IHelperSettings? backedUp, IHelperSettings? deviceOnly) + { + if (backedUp?.Get(HydrantenkarteViewModel.EinsatzstelleKey) is not { Length: > 0 } old) + { + return; + } + + if (deviceOnly is not null && string.IsNullOrEmpty(deviceOnly.Get(HydrantenkarteViewModel.EinsatzstelleKey))) + { + deviceOnly.Set(HydrantenkarteViewModel.EinsatzstelleKey, old); + } + + backedUp.Set(HydrantenkarteViewModel.EinsatzstelleKey, string.Empty); } } diff --git a/tests/FwAssist.Acceptance.Tests/BackupRulesTests.cs b/tests/FwAssist.Acceptance.Tests/BackupRulesTests.cs new file mode 100644 index 0000000..8fe7ca5 --- /dev/null +++ b/tests/FwAssist.Acceptance.Tests/BackupRulesTests.cs @@ -0,0 +1,49 @@ +using System.Xml.Linq; + +namespace FwAssist.Acceptance.Tests; + +// Android's Auto Backup copies an app's preferences and files into the user's Google account. Two +// things must stay out: the device-only preferences, where the Hydrantenkarte keeps the Einsatzstelle, +// and the Kartenpakete, which are large enough to push the app past the backup quota, after which +// Android backs up nothing at all. Android 12+ reads data_extraction_rules, older versions backup_rules. +public class BackupRulesTests +{ + private const string DeviceOnlyPreferences = "helper-settings-device.xml"; + private const string Kartenpakete = "maps/"; + + private static readonly XNamespace Android = "http://schemas.android.com/apk/res/android"; + + private static XDocument Load(string name) => XDocument.Load(Path.Join(AppContext.BaseDirectory, name)); + + private static List<(string Domain, string Path)> Excluded(XElement rules) => + rules.Elements("exclude") + .Select(e => ((string?)e.Attribute("domain") ?? string.Empty, (string?)e.Attribute("path") ?? string.Empty)) + .ToList(); + + [Fact] + public void The_manifest_names_both_rule_files() + { + var application = Assert.Single(Load("AndroidManifest.xml").Descendants("application")); + + Assert.Equal("@xml/data_extraction_rules", (string?)application.Attribute(Android + "dataExtractionRules")); + Assert.Equal("@xml/backup_rules", (string?)application.Attribute(Android + "fullBackupContent")); + } + + [Fact] + public void The_cloud_backup_of_android_12_and_newer_leaves_out_the_einsatzstelle_and_the_kartenpakete() + { + var cloud = Assert.Single(Load("data_extraction_rules.xml").Elements("data-extraction-rules").Elements("cloud-backup")); + + Assert.Contains(("sharedpref", DeviceOnlyPreferences), Excluded(cloud)); + Assert.Contains(("file", Kartenpakete), Excluded(cloud)); + } + + [Fact] + public void The_backup_of_android_11_and_older_leaves_out_the_einsatzstelle_and_the_kartenpakete() + { + var rules = Assert.Single(Load("backup_rules.xml").Elements("full-backup-content")); + + Assert.Contains(("sharedpref", DeviceOnlyPreferences), Excluded(rules)); + Assert.Contains(("file", Kartenpakete), Excluded(rules)); + } +} diff --git a/tests/FwAssist.Acceptance.Tests/FwAssist.Acceptance.Tests.csproj b/tests/FwAssist.Acceptance.Tests/FwAssist.Acceptance.Tests.csproj index eb9cae6..99d5ef0 100644 --- a/tests/FwAssist.Acceptance.Tests/FwAssist.Acceptance.Tests.csproj +++ b/tests/FwAssist.Acceptance.Tests/FwAssist.Acceptance.Tests.csproj @@ -31,6 +31,9 @@ + + + diff --git a/tests/FwAssist.Helpers.Hydranten.Tests/FwAssist.Helpers.Hydranten.Tests.csproj b/tests/FwAssist.Helpers.Hydranten.Tests/FwAssist.Helpers.Hydranten.Tests.csproj index 9455de7..3c568cd 100644 --- a/tests/FwAssist.Helpers.Hydranten.Tests/FwAssist.Helpers.Hydranten.Tests.csproj +++ b/tests/FwAssist.Helpers.Hydranten.Tests/FwAssist.Helpers.Hydranten.Tests.csproj @@ -21,9 +21,11 @@ - + + diff --git a/tests/FwAssist.Helpers.Hydranten.Tests/HydrantenModuleTests.cs b/tests/FwAssist.Helpers.Hydranten.Tests/HydrantenModuleTests.cs new file mode 100644 index 0000000..f31aa81 --- /dev/null +++ b/tests/FwAssist.Helpers.Hydranten.Tests/HydrantenModuleTests.cs @@ -0,0 +1,69 @@ +using FwAssist.AppLogic.Helpers; +using FwAssist.AppLogic.Services; +using FwAssist.Core.Map; +using FwAssist.Testing; + +namespace FwAssist.Helpers.Hydranten.Tests; + +// The Einsatzstelle is where a real Einsatz is, so it lives in the settings Android never backs up. +public class HydrantenModuleTests +{ + private const string Key = HydrantenkarteViewModel.EinsatzstelleKey; + + private static readonly GeoPoint Marktplatz = new(48.1785, 11.2555); + private static readonly GeoPoint Bahnhof = new(48.1702, 11.2489); + + private readonly InMemoryHelperSettings _backedUp = new(); + private readonly InMemoryHelperSettings _deviceOnly = new(); + + private HydrantenkarteViewModel Open() => + Assert.IsType(new HydrantenModule().CreateViewModel( + new HelperServices(new FakeTimeProvider(), new ManualTicker(), _backedUp, DeviceOnlySettings: _deviceOnly))); + + private static string? Shown(HydrantenkarteViewModel vm) => + vm.Einsatzstelle is { } at ? HydrantenkarteViewModel.Persisted(at) : null; + + [Fact] + public void The_einsatzstelle_comes_from_the_device_only_settings() + { + _deviceOnly.Set(Key, HydrantenkarteViewModel.Persisted(Marktplatz)); + + Assert.Equal(HydrantenkarteViewModel.Persisted(Marktplatz), Shown(Open())); + } + + [Fact] + public void Clearing_the_einsatzstelle_writes_only_the_device_only_settings() + { + _deviceOnly.Set(Key, HydrantenkarteViewModel.Persisted(Marktplatz)); + var vm = Open(); + + vm.EinsatzstelleLoeschenCommand.Execute(null); + + Assert.Equal(string.Empty, _deviceOnly.Get(Key)); + Assert.Null(_backedUp.Get(Key)); + } + + [Fact] + public void An_einsatzstelle_from_before_the_update_moves_out_of_the_backed_up_settings() + { + _backedUp.Set(Key, HydrantenkarteViewModel.Persisted(Marktplatz)); + + var vm = Open(); + + Assert.Equal(HydrantenkarteViewModel.Persisted(Marktplatz), Shown(vm)); + Assert.Equal(HydrantenkarteViewModel.Persisted(Marktplatz), _deviceOnly.Get(Key)); + Assert.Equal(string.Empty, _backedUp.Get(Key)); + } + + [Fact] + public void A_device_only_einsatzstelle_wins_over_an_old_copy_which_is_still_removed() + { + _backedUp.Set(Key, HydrantenkarteViewModel.Persisted(Marktplatz)); + _deviceOnly.Set(Key, HydrantenkarteViewModel.Persisted(Bahnhof)); + + var vm = Open(); + + Assert.Equal(HydrantenkarteViewModel.Persisted(Bahnhof), Shown(vm)); + Assert.Equal(string.Empty, _backedUp.Get(Key)); + } +}