aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-02-15 11:57:43 -0600
committerGitHub <noreply@github.com>2026-02-15 11:57:43 -0600
commitc11f9e2fb8aa43ee7921f292079aea9a852768e5 (patch)
tree86251b9db98ae2b9f2af36285508e2b0f1f256c3 /src
parent8ab17908255a71469ce38332e2ad2b2952309ff4 (diff)
parent7d20188935802375275ca4db3398072115ddd6e9 (diff)
downloadslskd-c11f9e2fb8aa43ee7921f292079aea9a852768e5.tar.gz
Merge pull request #1640 from slskd/search-args
Add explicit request validation for POST /searches
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/CommonExtensions.cs34
-rw-r--r--src/slskd/Search/API/Controllers/SearchesController.cs5
-rw-r--r--src/slskd/Search/API/DTO/SearchRequest.cs24
3 files changed, 54 insertions, 9 deletions
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;
@@ -450,6 +451,39 @@ namespace slskd
}
/// <summary>
+ /// Converts a ModelStateDictionary into a human-readable format.
+ /// </summary>
+ /// <param name="dictionary">The ModelStateDictionary to format.</param>
+ /// <returns>The formatted error string.</returns>
+ 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 }));
+ }
+
+ /// <summary>
+ /// Returns the <see cref="ModelError.ErrorMessage"/>, the <see cref="Exception.Message"/> for the ModelError, or both if both are present.
+ /// </summary>
+ /// <param name="error">The ModelError from which to retrieve the error message.</param>
+ /// <returns>The retrieved error message.</returns>
+ 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})";
+ }
+
+ /// <summary>
/// Determines whether the given object is a <see cref="Dictionary{TKey, TValue}"/>.
/// </summary>
/// <param name="obj">The object to check.</param>
diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs
index c51d537e..06ff4908 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
/// </summary>
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion("0")]
- [ApiController]
[Produces("application/json")]
[Consumes("application/json")]
public class SearchesController : ControllerBase
@@ -72,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))
diff --git a/src/slskd/Search/API/DTO/SearchRequest.cs b/src/slskd/Search/API/DTO/SearchRequest.cs
index c81b2eeb..32e5123c 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;
/// <summary>
/// A search request.
/// </summary>
- public class SearchRequest
+ public class SearchRequest : IValidatableObject
{
/// <summary>
/// Gets or sets the unique search identifier.
@@ -33,6 +35,7 @@ namespace slskd.Search.API
/// <summary>
/// Gets or sets the maximum number of file results to accept before the search is considered complete. (Default = 10,000).
/// </summary>
+ [Range(0, int.MaxValue)]
public int? FileLimit { get; set; }
/// <summary>
@@ -43,21 +46,25 @@ namespace slskd.Search.API
/// <summary>
/// Gets or sets the maximum queue depth a peer may have in order for a response to be processed. (Default = 1000000).
/// </summary>
+ [Range(0, int.MaxValue)]
public int? MaximumPeerQueueLength { get; set; }
/// <summary>
/// Gets or sets the minimum upload speed a peer must have in order for a response to be processed. (Default = 0).
/// </summary>
+ [Range(0, int.MaxValue)]
public int? MinimumPeerUploadSpeed { get; set; }
/// <summary>
/// Gets or sets the minimum number of files a response must contain in order to be processed. (Default = 1).
/// </summary>
+ [Range(0, int.MaxValue)]
public int? MinimumResponseFileCount { get; set; }
/// <summary>
/// Gets or sets the maximum number of search results to accept before the search is considered complete. (Default = 100).
/// </summary>
+ [Range(0, int.MaxValue)]
public int? ResponseLimit { get; set; }
/// <summary>
@@ -69,14 +76,10 @@ namespace slskd.Search.API
/// Gets or sets the search timeout value, in seconds, used to determine when the search is complete. (Default = 15).
/// </summary>
/// <remarks>The timeout duration is from the time of the last response.</remarks>
+ [Range(5, int.MaxValue)]
public int? SearchTimeout { get; set; }
/// <summary>
- /// Gets or sets the search token.
- /// </summary>
- public int? Token { get; set; }
-
- /// <summary>
/// Maps to a new instance of <see cref="SearchOptions"/>.
/// </summary>
/// <param name="responseFilter"></param>
@@ -105,5 +108,14 @@ namespace slskd.Search.API
responseReceived: responseReceived,
stateChanged: stateChanged);
}
+
+ public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
+ {
+ // todo: adjust this when additional search inputs are made available
+ if (string.IsNullOrWhiteSpace(SearchText))
+ {
+ yield return new ValidationResult("The field SearchText can not be null, empty, or consist of only whitespace", [nameof(SearchText)]);
+ }
+ }
}
} \ No newline at end of file