From 6126fd7508c7ebb503ec29db81eb9e7e17170b71 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 27 Apr 2024 18:44:04 -0500 Subject: add validation for blacklist format --- src/slskd/Core/Options.cs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index e99eb85f..afc9b07f 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1106,7 +1106,10 @@ namespace slskd public bool Swagger { get; init; } = false; } - public class BlacklistOptions + /// + /// Blacklist options. + /// + public class BlacklistOptions : IValidatableObject { /// /// Gets a value indicating whether blacklist file support should be enabled. @@ -1125,6 +1128,36 @@ namespace slskd [Description("path to blacklist file")] [FileExists(FileAccess.Read)] public string File { get; init; } + + /// + /// Extended validation. + /// + /// + /// + public IEnumerable Validate(ValidationContext validationContext) + { + var results = new List(); + + if (!Enabled) + { + return results; + } + + // loading/validating the entire list will be costly on low spec systems + // just make sure that we can detect a valid format and leave it at that + // if there's a problem with any of the entries, the load will fail and + // kill the application + try + { + _ = slskd.Blacklist.DetectFormat(File); + } + catch (FormatException ex) + { + results.Add(new ValidationResult(ex.Message)); + } + + return results; + } } /// -- cgit v1.2.3