Skip to content

fix: make DNS changes reversible with a snapshot-based Undo#825

Merged
laurentiu021 merged 1 commit into
mainfrom
fix/r4-dns-snapshot-undo
Jun 8, 2026
Merged

fix: make DNS changes reversible with a snapshot-based Undo#825
laurentiu021 merged 1 commit into
mainfrom
fix/r4-dns-snapshot-undo

Conversation

@laurentiu021

Copy link
Copy Markdown
Owner

What

Audit Round 4 / PR4 — closes the reversibility half of test finding #3. (The Confirm gate it also called for was already added in 1.20.0.)

DNS preset changes were only reversible via Reset to DHCP, which silently discarded any manually-configured static DNS the user had before. Now:

  • Applying a preset snapshots the servers in effect first.
  • A new Undo button on the DNS & Hosts tab restores that exact prior configuration — re-applying the previous static servers, or resetting to DHCP if that was the prior state.

Changes

  • DnsService.CaptureCurrentServersAsync — reads + validates current IPv4 servers (filters blank/non-IP noise; empty => the adapter was on DHCP).
  • DnsService.RestoreServersAsync — re-applies a snapshot (empty => DHCP reset), validating every address before interpolating it into the Set-DnsClientServerAddress script.
  • DnsHostsViewModel — snapshots before SetDnsAsync; RestorePreviousDns command is admin-gated and Confirm-gated; CanRestorePreviousDns drives the button.
  • DnsHostsView — Undo button (SecondaryButton, AutomationProperties.Name, BoolToVis visibility), shown only after a change, disabled while applying.

Reversibility (CODE QUALITY merge-blocker)

This makes the DNS mutation genuinely snapshot-reversible, satisfying the "every system modification must be reversible" contract.

Tests

DnsServiceTests cover capture (parse / non-IP filtering / DHCP-empty) and restore (re-apply exact servers / empty=>DHCP / null-throw / invalid-address-throw) — all via the IPowerShellRunner seam, no live DNS touched. All literals synthetic/public (8.8.8.8, 9.9.9.9).

Verification

  • Build: main + unit + integration tests, 0 errors / 0 warnings (Release).
  • Regression sweep: 7 files, all DNS-scoped; MainWindowViewModel builds the VM via the unchanged ctor; new service methods are additive.
  • fix: — version bumped to 1.20.5, CHANGELOG + README updated in this PR.

DNS preset changes were only reversible via 'Reset to DHCP', which silently
discarded any manually-configured static DNS the user had before. Applying a
preset now snapshots the servers in effect first, and a new 'Undo' button on
the DNS & Hosts tab restores that exact prior configuration (re-applying the
previous static servers, or resetting to DHCP if that was the prior state).

- DnsService.CaptureCurrentServersAsync reads and validates the current IPv4
  servers; RestoreServersAsync re-applies them (empty snapshot => DHCP reset),
  validating every address before interpolating it into the Set script.
- DnsHostsViewModel snapshots before SetDnsAsync, exposes RestorePreviousDns
  (admin-gated, Confirm-gated) and CanRestorePreviousDns for the button.
- DnsHostsView: Undo button (SecondaryButton, AutomationProperties.Name),
  visible only after a change, disabled while applying.
- Tests cover capture (parse/filter/DHCP-empty) and restore (re-apply/DHCP/
  null-throw/invalid-address-throw) via the IPowerShellRunner seam.

Closes the reversibility half of audit test finding #3 (the Confirm gate was
already added in 1.20.0). README + CHANGELOG updated.
Comment on lines +131 to +135
foreach (var server in servers)
{
if (!IPAddress.TryParse(server, out _))
throw new ArgumentException($"Invalid DNS address in snapshot: '{server}'", nameof(servers));
}
await RefreshDnsAsync();
Log.Information("DNS restored to previous setting ({Label})", label);
}
catch (OperationCanceledException) { }
Comment on lines +250 to +255
catch (Exception ex)
{
Application.Current?.Dispatcher?.Invoke(() =>
StatusMessage = $"Failed to restore DNS: {ex.Message}");
Log.Error(ex, "Failed to restore previous DNS");
}
@laurentiu021 laurentiu021 merged commit a3a256d into main Jun 8, 2026
4 checks passed
@laurentiu021 laurentiu021 deleted the fix/r4-dns-snapshot-undo branch June 8, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants