From 438e3199ded5a99a1b59b3e583cb0d5fdf47fd30 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 25 Aug 2013 21:01:03 -0700 Subject: [PATCH] fixed input validation for indexers --- NzbDrone.Api/Indexers/IndexerModule.cs | 17 ++++++++------ NzbDrone.Core/Indexers/IIndexerSetting.cs | 11 --------- .../Indexers/Newznab/NewznabSettings.cs | 23 ++++++++++--------- NzbDrone.Core/Indexers/NullSetting.cs | 14 +++++++++++ .../Indexers/Omgwtfnzbs/OmgwtfnzbsSettings.cs | 19 ++++++++------- NzbDrone.Core/NzbDrone.Core.csproj | 1 + 6 files changed, 48 insertions(+), 37 deletions(-) create mode 100644 NzbDrone.Core/Indexers/NullSetting.cs diff --git a/NzbDrone.Api/Indexers/IndexerModule.cs b/NzbDrone.Api/Indexers/IndexerModule.cs index 3c3601c25..08fc8518a 100644 --- a/NzbDrone.Api/Indexers/IndexerModule.cs +++ b/NzbDrone.Api/Indexers/IndexerModule.cs @@ -64,7 +64,7 @@ private IndexerResource UpdateIndexer(IndexerResource indexerResource) indexer.InjectFrom(indexerResource); indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields); - ValidateSetting(indexer.Settings); + ValidateIndexer(indexer); indexer = _indexerService.Update(indexer); @@ -75,13 +75,16 @@ private IndexerResource UpdateIndexer(IndexerResource indexerResource) } - private static void ValidateSetting(IIndexerSetting setting) + private static void ValidateIndexer(Indexer indexer) { - var validationResult = setting.Validate(); - - if (!validationResult.IsValid) + if (indexer.Enable) { - throw new ValidationException(validationResult.Errors); + var validationResult = indexer.Settings.Validate(); + + if (!validationResult.IsValid) + { + throw new ValidationException(validationResult.Errors); + } } } @@ -100,7 +103,7 @@ private Indexer GetIndexer(IndexerResource indexerResource) indexer.InjectFrom(indexerResource); indexer.Settings = SchemaDeserializer.DeserializeSchema(indexer.Settings, indexerResource.Fields); - ValidateSetting(indexer.Settings); + ValidateIndexer(indexer); return indexer; } diff --git a/NzbDrone.Core/Indexers/IIndexerSetting.cs b/NzbDrone.Core/Indexers/IIndexerSetting.cs index 078b18ff5..4f9cf16de 100644 --- a/NzbDrone.Core/Indexers/IIndexerSetting.cs +++ b/NzbDrone.Core/Indexers/IIndexerSetting.cs @@ -6,15 +6,4 @@ public interface IIndexerSetting { ValidationResult Validate(); } - - - public class NullSetting : IIndexerSetting - { - public static readonly NullSetting Instance = new NullSetting(); - - public ValidationResult Validate() - { - return new ValidationResult(); - } - } } diff --git a/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs b/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs index 7cac534ec..c83623ace 100644 --- a/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs +++ b/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs @@ -7,12 +7,22 @@ namespace NzbDrone.Core.Indexers.Newznab { + public class NewznabSettingsValidator : AbstractValidator + { + public NewznabSettingsValidator() + { + RuleFor(c => c.Url).ValidRootUrl(); + } + } + + public class NewznabSettings : IIndexerSetting { + private static readonly NewznabSettingsValidator Validator = new NewznabSettingsValidator(); + public NewznabSettings() { Categories = new[] { 5030, 5040 }; - //RuleFor(c => c.Url).ValidRootUrl(); } [FieldDefinition(0, Label = "URL")] @@ -23,18 +33,9 @@ public NewznabSettings() public IEnumerable Categories { get; set; } - public bool IsValid - { - get - { - return !string.IsNullOrWhiteSpace(Url); - } - } - public ValidationResult Validate() { - return new ValidationResult(); - //return Validate(this); + return Validator.Validate(this); } } } \ No newline at end of file diff --git a/NzbDrone.Core/Indexers/NullSetting.cs b/NzbDrone.Core/Indexers/NullSetting.cs new file mode 100644 index 000000000..a7013335a --- /dev/null +++ b/NzbDrone.Core/Indexers/NullSetting.cs @@ -0,0 +1,14 @@ +using FluentValidation.Results; + +namespace NzbDrone.Core.Indexers +{ + public class NullSetting : IIndexerSetting + { + public static readonly NullSetting Instance = new NullSetting(); + + public ValidationResult Validate() + { + return new ValidationResult(); + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Indexers/Omgwtfnzbs/OmgwtfnzbsSettings.cs b/NzbDrone.Core/Indexers/Omgwtfnzbs/OmgwtfnzbsSettings.cs index db0ac9400..778b3a48e 100644 --- a/NzbDrone.Core/Indexers/Omgwtfnzbs/OmgwtfnzbsSettings.cs +++ b/NzbDrone.Core/Indexers/Omgwtfnzbs/OmgwtfnzbsSettings.cs @@ -1,18 +1,22 @@ using System; using FluentValidation; using FluentValidation.Results; -using Newtonsoft.Json; using NzbDrone.Core.Annotations; namespace NzbDrone.Core.Indexers.Omgwtfnzbs { + public class OmgwtfnzbsSettingsValidator : AbstractValidator + { + public OmgwtfnzbsSettingsValidator() + { + RuleFor(c => c.Username).NotEmpty(); + RuleFor(c => c.ApiKey).NotEmpty(); + } + } + public class OmgwtfnzbsSettings : IIndexerSetting { -// public OmgwtfnzbsSettings() -// { -// RuleFor(c => c.Username).NotEmpty(); -// RuleFor(c => c.ApiKey).NotEmpty(); -// } + private static readonly OmgwtfnzbsSettingsValidator Validator = new OmgwtfnzbsSettingsValidator(); [FieldDefinition(0, Label = "Username")] public String Username { get; set; } @@ -22,8 +26,7 @@ public class OmgwtfnzbsSettings : IIndexerSetting public ValidationResult Validate() { - return new ValidationResult(); - //return Validate(this); + return Validator.Validate(this); } } } diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 7adf989f7..15ded7729 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -210,6 +210,7 @@ +