Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 24 additions & 6 deletions src/LageBuch.Sync/JsonTrustStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,49 @@ public void SaveThumbprint(string hostAddress, string thumbprint)
{
lock (_gate)
{
_cache[hostAddress] = thumbprint;
Persist();
var updated = new Dictionary<string, string>(_cache, StringComparer.Ordinal)
{
[hostAddress] = thumbprint,
};

Commit(updated);
}
}

public void RemoveThumbprint(string hostAddress)
{
lock (_gate)
{
if (_cache.Remove(hostAddress))
var updated = new Dictionary<string, string>(_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<string, string> 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<string, string> trusted)
{
var tmpPath = _path + ".tmp";
File.WriteAllText(tmpPath, JsonSerializer.Serialize(_cache));
File.WriteAllText(tmpPath, JsonSerializer.Serialize(trusted));
File.Move(tmpPath, _path, overwrite: true);
}

Expand Down
3 changes: 0 additions & 3 deletions src/LageBuch.Sync/RemoteIncidentSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
58 changes: 58 additions & 0 deletions tests/LageBuch.Sync.Hosting.Tests/JsonTrustStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SystemException>(() => 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<SystemException>(() => 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"));
}

/// <summary>
/// Makes the next write fail: a directory sitting where the temp file wants to go stops
/// <c>File.WriteAllText</c> before <c>File.Move</c> ever runs.
/// </summary>
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);
}
}