From 707b8b9754b6db4bbf6c6912be343bc0b2ffe7c9 Mon Sep 17 00:00:00 2001 From: Andy Kelk Date: Wed, 14 Jan 2026 17:53:19 +1100 Subject: Fix UrlEncodingModelBinder to account for PathBase When the application runs with a base path (e.g., /slskd via UsePathBase), the UrlEncodingModelBinder was incorrectly calculating the URL segment index. It was using the route template (which doesn't include the base path) to determine the index, but applying it to the raw URL (which does include the base path), causing it to extract the wrong segment. This fix strips the PathBase from the absolute path before splitting it into segments, ensuring the correct parameter value is extracted regardless of whether a base path is configured. Fixes regression introduced in PR #1596 (commits 8849d78c and e7e0cdaf) where [UrlEncoded] attribute was added to username parameters in v0.24.2. Also adds comprehensive unit tests to verify the fix works correctly with and without a base path, including edge cases like query strings and special characters in usernames. --- src/slskd/Common/Middleware/UrlEncodingModelBinder.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs b/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs index 9592e75e..6729b374 100644 --- a/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs +++ b/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs @@ -76,7 +76,17 @@ public class UrlEncodingModelBinder : IModelBinder var rawUrl = bindingContext.HttpContext.Features.Get()?.RawTarget ?? string.Empty; var request = bindingContext.HttpContext.Request; - var rawValue = new Uri($"{request.Scheme}://{request.Host}{rawUrl}").AbsolutePath // discard query string + var absolutePath = new Uri($"{request.Scheme}://{request.Host}{rawUrl}").AbsolutePath; // discard query string + + // if the application is running with a base path (e.g., /slskd), we need to remove it + // from the absolute path before splitting, as the route template doesn't include it + var pathBase = request.PathBase.Value ?? string.Empty; + if (!string.IsNullOrEmpty(pathBase) && absolutePath.StartsWith(pathBase, StringComparison.OrdinalIgnoreCase)) + { + absolutePath = absolutePath.Substring(pathBase.Length); + } + + var rawValue = absolutePath .Split('/', StringSplitOptions.RemoveEmptyEntries) .ElementAtOrDefault(index); -- cgit v1.2.3