From 271266b10ac51ee6dd7a7024d346b631bd5397c2 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sun, 21 Jan 2024 16:11:28 +0200 Subject: [PATCH] Fix possible NullRef in Email Encryption migration --- .../Migration/201_email_encryptionFixture.cs | 29 +++++++++++++++++++ .../Migration/201_email_encryption.cs | 4 +-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/201_email_encryptionFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/201_email_encryptionFixture.cs index 7a6cee1ca..3a0f28ee2 100644 --- a/src/NzbDrone.Core.Test/Datastore/Migration/201_email_encryptionFixture.cs +++ b/src/NzbDrone.Core.Test/Datastore/Migration/201_email_encryptionFixture.cs @@ -81,6 +81,35 @@ namespace NzbDrone.Core.Test.Datastore.Migration items.First().ConfigContract.Should().Be("EmailSettings"); items.First().Settings.UseEncryption.Should().Be((int)EmailEncryptionType.Always); } + + [Test] + public void should_use_defaults_when_settings_are_empty() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("Notifications").Row(new + { + OnGrab = true, + OnDownload = true, + OnUpgrade = true, + OnHealthIssue = true, + IncludeHealthWarnings = true, + OnRename = true, + Name = "Mail Sonarr", + Implementation = "Email", + Tags = "[]", + Settings = new { }.ToJson(), + ConfigContract = "EmailSettings" + }); + }); + + var items = db.Query("SELECT * FROM \"Notifications\""); + + items.Should().HaveCount(1); + items.First().Implementation.Should().Be("Email"); + items.First().ConfigContract.Should().Be("EmailSettings"); + items.First().Settings.UseEncryption.Should().Be((int)EmailEncryptionType.Preferred); + } } public class NotificationDefinition201 diff --git a/src/NzbDrone.Core/Datastore/Migration/201_email_encryption.cs b/src/NzbDrone.Core/Datastore/Migration/201_email_encryption.cs index 93ee7a962..5f5f0c737 100644 --- a/src/NzbDrone.Core/Datastore/Migration/201_email_encryption.cs +++ b/src/NzbDrone.Core/Datastore/Migration/201_email_encryption.cs @@ -31,7 +31,7 @@ namespace NzbDrone.Core.Datastore.Migration var id = reader.GetInt32(0); var settings = Json.Deserialize(reader.GetString(1)); - settings["useEncryption"] = settings["requireEncryption"].ToObject() ? 1 : 0; + settings["useEncryption"] = settings.Value("requireEncryption") ? 1 : 0; settings["requireEncryption"] = null; updated.Add(new @@ -43,7 +43,7 @@ namespace NzbDrone.Core.Datastore.Migration } } - var updateSql = $"UPDATE \"Notifications\" SET \"Settings\" = @Settings WHERE \"Id\" = @Id"; + var updateSql = "UPDATE \"Notifications\" SET \"Settings\" = @Settings WHERE \"Id\" = @Id"; conn.Execute(updateSql, updated, transaction: tran); } }