From a6aa646a29b13b2e571a6150da0218e2740c62d8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Feb 2026 11:19:39 -0600 Subject: add validation to SearchRequest --- src/slskd/Search/API/DTO/SearchRequest.cs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/API/DTO/SearchRequest.cs b/src/slskd/Search/API/DTO/SearchRequest.cs index c81b2eeb..f082243f 100644 --- a/src/slskd/Search/API/DTO/SearchRequest.cs +++ b/src/slskd/Search/API/DTO/SearchRequest.cs @@ -18,12 +18,14 @@ namespace slskd.Search.API { using System; + using System.Collections.Generic; + using System.ComponentModel.DataAnnotations; using Soulseek; /// /// A search request. /// - public class SearchRequest + public class SearchRequest : IValidatableObject { /// /// Gets or sets the unique search identifier. @@ -33,6 +35,7 @@ namespace slskd.Search.API /// /// Gets or sets the maximum number of file results to accept before the search is considered complete. (Default = 10,000). /// + [Range(0, int.MaxValue)] public int? FileLimit { get; set; } /// @@ -43,21 +46,25 @@ namespace slskd.Search.API /// /// Gets or sets the maximum queue depth a peer may have in order for a response to be processed. (Default = 1000000). /// + [Range(0, int.MaxValue)] public int? MaximumPeerQueueLength { get; set; } /// /// Gets or sets the minimum upload speed a peer must have in order for a response to be processed. (Default = 0). /// + [Range(0, int.MaxValue)] public int? MinimumPeerUploadSpeed { get; set; } /// /// Gets or sets the minimum number of files a response must contain in order to be processed. (Default = 1). /// + [Range(0, int.MaxValue)] public int? MinimumResponseFileCount { get; set; } /// /// Gets or sets the maximum number of search results to accept before the search is considered complete. (Default = 100). /// + [Range(0, int.MaxValue)] public int? ResponseLimit { get; set; } /// @@ -69,13 +76,9 @@ namespace slskd.Search.API /// Gets or sets the search timeout value, in seconds, used to determine when the search is complete. (Default = 15). /// /// The timeout duration is from the time of the last response. + [Range(5, int.MaxValue)] public int? SearchTimeout { get; set; } - /// - /// Gets or sets the search token. - /// - public int? Token { get; set; } - /// /// Maps to a new instance of . /// @@ -105,5 +108,14 @@ namespace slskd.Search.API responseReceived: responseReceived, stateChanged: stateChanged); } + + public IEnumerable Validate(ValidationContext validationContext) + { + // todo: adjust this when additional search inputs are made available + if (string.IsNullOrWhiteSpace(SearchText)) + { + yield return new ValidationResult("SearchText can not be null, empty, or consist of only whitespace"); + } + } } } \ No newline at end of file -- cgit v1.2.3 From 68a2986dc8aa06df2c5526bbdbbb3fb7815e659a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Feb 2026 11:48:35 -0600 Subject: tweak message --- src/slskd/Search/API/DTO/SearchRequest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Search/API/DTO/SearchRequest.cs b/src/slskd/Search/API/DTO/SearchRequest.cs index f082243f..32e5123c 100644 --- a/src/slskd/Search/API/DTO/SearchRequest.cs +++ b/src/slskd/Search/API/DTO/SearchRequest.cs @@ -114,7 +114,7 @@ namespace slskd.Search.API // todo: adjust this when additional search inputs are made available if (string.IsNullOrWhiteSpace(SearchText)) { - yield return new ValidationResult("SearchText can not be null, empty, or consist of only whitespace"); + yield return new ValidationResult("The field SearchText can not be null, empty, or consist of only whitespace", [nameof(SearchText)]); } } } -- cgit v1.2.3 From 8cedbcd89adfc5b9f363b49b2b4a3db655bc028e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Feb 2026 11:48:55 -0600 Subject: add extensions for formatting model binding errors --- src/slskd/Common/CommonExtensions.cs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index 815c0eb4..c189837c 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -29,6 +29,7 @@ namespace slskd using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; + using Microsoft.AspNetCore.Mvc.ModelBinding; using YamlDotNet.Serialization; using YamlDotNet.Serialization.NamingConventions; @@ -449,6 +450,39 @@ namespace slskd return sanitized; } + /// + /// Converts a ModelStateDictionary into a human-readable format. + /// + /// The ModelStateDictionary to format. + /// The formatted error string. + public static string GetReadableString(this ModelStateDictionary dictionary) + { + if (dictionary == null || dictionary.IsValid) + { + return string.Empty; + } + + return dictionary.Values + .Where(v => v.Errors.Any()) + .Select(v => + v.Errors + .Select(e => GetErrorAndOrExceptionMessage(e)) + .Aggregate((a, b) => string.Join(", ", new[] { a, b }))) + .Aggregate((a, b) => string.Join(" ", new[] { a, b })); + } + + /// + /// Returns the , the for the ModelError, or both if both are present. + /// + /// The ModelError from which to retrieve the error message. + /// The retrieved error message. + public static string GetErrorAndOrExceptionMessage(this ModelError error) + { + var ex = error?.Exception?.Message; + return string.IsNullOrEmpty(error?.ErrorMessage) ? ex : + string.IsNullOrEmpty(ex) ? error?.ErrorMessage : $"{error?.ErrorMessage} ({ex})"; + } + /// /// Determines whether the given object is a . /// -- cgit v1.2.3 From 46f76b61d2afb509ce0c891fa36f2ac9388696bd Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Feb 2026 11:49:21 -0600 Subject: remove [ApiController] to get rid of automagic behaviors --- src/slskd/Search/API/Controllers/SearchesController.cs | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index c51d537e..fb25a866 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -34,7 +34,6 @@ namespace slskd.Search.API /// [Route("api/v{version:apiVersion}/[controller]")] [ApiVersion("0")] - [ApiController] [Produces("application/json")] [Consumes("application/json")] public class SearchesController : ControllerBase -- cgit v1.2.3 From 7d20188935802375275ca4db3398072115ddd6e9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 15 Feb 2026 11:49:58 -0600 Subject: use ModelState.IsValid --- src/slskd/Search/API/Controllers/SearchesController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index fb25a866..06ff4908 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -71,9 +71,9 @@ namespace slskd.Search.API return Forbid(); } - if (string.IsNullOrWhiteSpace(request.SearchText)) + if (!ModelState.IsValid) { - return BadRequest("SearchText may not be null or empty"); + return BadRequest(ModelState.GetReadableString()); } if (!SearchRequestLimiter.Wait(0)) -- cgit v1.2.3