From 2e4bdb5fc8ea37ca10a54c7009ef8b39a7bc5dc9 Mon Sep 17 00:00:00 2001 From: endercodezz Date: Sun, 22 Nov 2026 04:10:31 +0200 Subject: [PATCH] Reject backups with null server groups or contributors Validate checked every routing list but not Settings.ServerGroups or Settings.Contributors, so a null list passed and Merge import then threw a NullReferenceException (Replace persisted the null). Reject null lists and malformed group entries. Closes #511 --- src/Bydlo.Core/Backup/BackupArchive.cs | 6 ++++ .../BackupSettingsValidationTests.cs | 33 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Bydlo.Core/Backup/BackupArchive.cs b/src/Bydlo.Core/Backup/BackupArchive.cs index 653470d0..168f48de 100644 --- a/src/Bydlo.Core/Backup/BackupArchive.cs +++ b/src/Bydlo.Core/Backup/BackupArchive.cs @@ -271,6 +271,12 @@ internal static void Validate(BackupData data) if (data.Version != 1 || data.Settings is null || data.Profiles is null || data.Subscriptions is null || data.ToolSecrets?.Values is null || data.Chains is null) { throw new InvalidDataException("Invalid backup."); } var s = data.Settings; + // Every collection the import path enumerates must be present: a null list passed validation + // and threw later in ImportSettings or after it was persisted. + if (s.ServerGroups is null || s.Contributors is null || + s.ServerGroups.Any(g => g is null || g.Id == Guid.Empty || g.Name is null || g.ProfileIds is null || g.ProbeUrl is null) || + s.Contributors.Any(c => c is null || c.Login is null)) + { throw new InvalidDataException("Invalid settings."); } if (s.Routing is null || s.DpiDefaults is null || s.Zapret is null || s.TgWsProxy is null || s.EnginePaths is null || s.Routing.DirectDomains is null || s.Routing.ProxyDomains is null || s.Routing.BlockDomains is null || s.Routing.DirectCidrs is null || s.Routing.DirectProcesses is null || s.Routing.OrderedRules is null || diff --git a/tests/Bydlo.Tests/BackupSettingsValidationTests.cs b/tests/Bydlo.Tests/BackupSettingsValidationTests.cs index 1f36a11b..795c3ea2 100644 --- a/tests/Bydlo.Tests/BackupSettingsValidationTests.cs +++ b/tests/Bydlo.Tests/BackupSettingsValidationTests.cs @@ -19,6 +19,39 @@ public void Rejects_null_dns_collections(string property) Assert.Throws(() => BackupArchive.Validate(data)); } + [Fact] + public void Rejects_null_server_groups_and_contributors() + { + // A null list used to pass validation and throw later in ImportSettings (Merge) or after saving. + var data = new BackupData(); + data.Settings.ServerGroups = null!; + Assert.Throws(() => BackupArchive.Validate(data)); + Assert.Throws(() => BackupArchive.Encrypt(data, "a long backup password")); + + data = new BackupData(); + data.Settings.ServerGroups = [null!]; + Assert.Throws(() => BackupArchive.Validate(data)); + + data = new BackupData(); + data.Settings.ServerGroups = [new ServerGroup { Name = "g", ProfileIds = null! }]; + Assert.Throws(() => BackupArchive.Validate(data)); + + data = new BackupData(); + data.Settings.Contributors = null!; + Assert.Throws(() => BackupArchive.Validate(data)); + } + + [Fact] + public void Valid_server_groups_still_merge() + { + var data = new BackupData(); + var group = new ServerGroup { Name = "g" }; + data.Settings.ServerGroups = [group]; + BackupArchive.Validate(data); + var merged = BackupArchive.ImportSettings(data, new AppSettings { ServerGroups = [new ServerGroup { Name = "local" }] }, BackupImportMode.Merge); + Assert.Equal(2, merged.ServerGroups.Count); + } + [Theory] [InlineData("RemoteDnsTransport")] [InlineData("DirectDnsTransport")]