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 src/Bydlo.Core/Backup/BackupArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
33 changes: 33 additions & 0 deletions tests/Bydlo.Tests/BackupSettingsValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,39 @@ public void Rejects_null_dns_collections(string property)
Assert.Throws<InvalidDataException>(() => 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<InvalidDataException>(() => BackupArchive.Validate(data));
Assert.Throws<InvalidDataException>(() => BackupArchive.Encrypt(data, "a long backup password"));

data = new BackupData();
data.Settings.ServerGroups = [null!];
Assert.Throws<InvalidDataException>(() => BackupArchive.Validate(data));

data = new BackupData();
data.Settings.ServerGroups = [new ServerGroup { Name = "g", ProfileIds = null! }];
Assert.Throws<InvalidDataException>(() => BackupArchive.Validate(data));

data = new BackupData();
data.Settings.Contributors = null!;
Assert.Throws<InvalidDataException>(() => 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")]
Expand Down
Loading