diff --git a/CHANGELOG.md b/CHANGELOG.md index dd236cb1..908c3591 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,12 @@ once we reach 1.0. join host — are written atomically (to a temp file that is then renamed into place), the way `trust.json` already was. A crash or a full disk mid-write used to be able to leave a truncated file behind, which the next start silently read as "nothing remembered". (#302) +- A failed write to `trust.json` no longer leaves the running app trusting a host certificate the + file does not record. Accepting or resetting a host's certificate updated memory first and wrote + afterwards, so if the write failed (full disk, permissions) the session kept connecting happily + while the next start re-prompted for the same host. The write now happens first and is only + adopted once it lands, and a retry after a failed reset does the work instead of silently + skipping it. (#302) - The sync server only bound IPv4 (`0.0.0.0`), so a device reachable only over IPv6 could never join a hosted incident. It now binds dual-stack, accepting both IPv4 and IPv6 on the same socket, and falls back to IPv4-only itself if the platform doesn't support IPv6. diff --git a/src/LageBuch.Sync/JsonTrustStore.cs b/src/LageBuch.Sync/JsonTrustStore.cs index ef755b2c..e44a13c3 100644 --- a/src/LageBuch.Sync/JsonTrustStore.cs +++ b/src/LageBuch.Sync/JsonTrustStore.cs @@ -34,8 +34,12 @@ public void SaveThumbprint(string hostAddress, string thumbprint) { lock (_gate) { - _cache[hostAddress] = thumbprint; - Persist(); + var updated = new Dictionary(_cache, StringComparer.Ordinal) + { + [hostAddress] = thumbprint, + }; + + Commit(updated); } } @@ -43,22 +47,36 @@ public void RemoveThumbprint(string hostAddress) { lock (_gate) { - if (_cache.Remove(hostAddress)) + var updated = new Dictionary(_cache, StringComparer.Ordinal); + if (updated.Remove(hostAddress)) { - Persist(); + Commit(updated); } } } + // Disk first, memory second. The mutators used to update _cache and then write, so a write that + // threw (disk full, permissions, an AV lock on Windows) left this instance trusting a certificate + // that trust.json does not record -- a divergence that survives for the rest of the process, and + // one that matters: the next start reads the file back and re-prompts for a host this session had + // silently accepted. Building the candidate off to the side and only adopting it once the write + // has landed means a failed write changes nothing at all, and the caller's retry sees the same + // pre-write state rather than a cache that already looks updated. + private void Commit(Dictionary updated) + { + Persist(updated); + _cache = updated; + } + // Write-then-rename: a crash between the two leaves either the old file untouched or the new one // fully in place -- never a half-written trust.json. Always called with _gate already held (both // mutators take it for the whole read-modify-write), so the temp file's fixed name is safe from a // sibling in-process write; overwrite: true also clobbers a stale *.tmp left by a process that // crashed between the write and the move on its own previous attempt. - private void Persist() + private void Persist(Dictionary trusted) { var tmpPath = _path + ".tmp"; - File.WriteAllText(tmpPath, JsonSerializer.Serialize(_cache)); + File.WriteAllText(tmpPath, JsonSerializer.Serialize(trusted)); File.Move(tmpPath, _path, overwrite: true); } diff --git a/src/LageBuch.Sync/RemoteIncidentSession.cs b/src/LageBuch.Sync/RemoteIncidentSession.cs index c3f6839c..414cf500 100644 --- a/src/LageBuch.Sync/RemoteIncidentSession.cs +++ b/src/LageBuch.Sync/RemoteIncidentSession.cs @@ -494,9 +494,6 @@ public async Task SendAsync(SyncCommand command, CancellationToken ct = default) response.EnsureSuccessStatusCode(); } - // Arrives on SignalR's receive loop. Swap the cached incident and raise Changed on the UI thread: - // the subscribers (EtbViewModel.Sync et al.) mutate Avalonia-bound collections, which Avalonia - // rejects off-thread — so a broadcast raised here would otherwise never reach the view. // Arrives on SignalR's receive loop. Swap the cached incident and raise Changed on the UI thread: // the subscribers (EtbViewModel.Sync et al.) mutate Avalonia-bound collections, which Avalonia // rejects off-thread — so a broadcast raised here would otherwise never reach the view. diff --git a/tests/LageBuch.Sync.Hosting.Tests/JsonTrustStoreTests.cs b/tests/LageBuch.Sync.Hosting.Tests/JsonTrustStoreTests.cs index 1885b6ef..a1f3bb47 100644 --- a/tests/LageBuch.Sync.Hosting.Tests/JsonTrustStoreTests.cs +++ b/tests/LageBuch.Sync.Hosting.Tests/JsonTrustStoreTests.cs @@ -151,4 +151,62 @@ public void SaveThumbprint_overwrites_a_stale_leftover_tmp_file_from_a_previous_ Assert.False(File.Exists(_path + ".tmp")); Assert.Equal("ABCD1234", new JsonTrustStore(_path).GetThumbprint("10.0.0.9")); } + + [Fact] + public void A_failed_SaveThumbprint_leaves_memory_agreeing_with_disk() + { + var store = new JsonTrustStore(_path); + store.SaveThumbprint("10.0.0.9", "ORIGINAL1"); + + using (BlockTheWrite()) + { + // SystemException, not IOException: opening a path that is a directory surfaces as + // UnauthorizedAccessException on Linux and IOException on Windows. + Assert.ThrowsAny(() => store.SaveThumbprint("10.0.0.9", "ROTATED2")); + + // The point of the fix: the failed write must not have been adopted in memory, because + // the next start reads trust.json back and would then disagree with this live session. + Assert.Equal("ORIGINAL1", store.GetThumbprint("10.0.0.9")); + } + + Assert.Equal("ORIGINAL1", new JsonTrustStore(_path).GetThumbprint("10.0.0.9")); + } + + [Fact] + public void A_failed_RemoveThumbprint_leaves_the_thumbprint_trusted_and_retryable() + { + var store = new JsonTrustStore(_path); + store.SaveThumbprint("10.0.0.9", "ABCD1234"); + + using (BlockTheWrite()) + { + Assert.ThrowsAny(() => store.RemoveThumbprint("10.0.0.9")); + Assert.Equal("ABCD1234", store.GetThumbprint("10.0.0.9")); + } + + // Remove used to consume the dictionary's return value before persisting, so after a failed + // write the entry was already gone from memory and a retry saw false and skipped the write + // entirely -- leaving it on disk forever. The retry must still do the work. + store.RemoveThumbprint("10.0.0.9"); + Assert.Null(new JsonTrustStore(_path).GetThumbprint("10.0.0.9")); + } + + /// + /// Makes the next write fail: a directory sitting where the temp file wants to go stops + /// File.WriteAllText before File.Move ever runs. + /// + private BlockedWrite BlockTheWrite() => new(_path + ".tmp"); + + private sealed class BlockedWrite : IDisposable + { + private readonly string _tmpPath; + + public BlockedWrite(string tmpPath) + { + _tmpPath = tmpPath; + Directory.CreateDirectory(_tmpPath); + } + + public void Dispose() => Directory.Delete(_tmpPath); + } }