diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-01-14 18:55:53 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-01-14 18:55:53 -0600 |
| commit | 35fb847f92350a7751cfe0c7aca2e6ea252a9573 (patch) | |
| tree | da0b88e10d774d602d46b48193d36944e4db2baf | |
| parent | de27bb420022be4d9a23034cb9adfb72cd740933 (diff) | |
| parent | d7b59050ecc39448c34741552fb489f88457e346 (diff) | |
| download | slskd-35fb847f92350a7751cfe0c7aca2e6ea252a9573.tar.gz | |
Merge pull request #1601 from slskd/path
Fix Regression in 0.24.2: API endpoints return 404 when running with base path
| -rw-r--r-- | src/slskd/Common/Middleware/UrlEncodingModelBinder.cs | 11 | ||||
| -rw-r--r-- | tests/slskd.Tests.Unit/Common/Middleware/UrlEncodingModelBinderTests.cs | 173 |
2 files changed, 183 insertions, 1 deletions
diff --git a/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs b/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs index 9592e75e..27ea3258 100644 --- a/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs +++ b/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs @@ -63,8 +63,18 @@ public class UrlEncodingModelBinder : IModelBinder */ try { + var request = bindingContext.HttpContext.Request; + var model = $"{{{bindingContext.ModelName}}}"; var template = bindingContext.ActionContext.ActionDescriptor.AttributeRouteInfo?.Template ?? string.Empty; + + // if the application is running with a base path (e.g., /slskd), we need to add it to the template + // as it isn't included out of the box + if (request.PathBase.HasValue) + { + template = $"{request.PathBase.Value.Trim('/')}/{template}"; + } + var index = template .Split('/', StringSplitOptions.RemoveEmptyEntries) .Select((segment, index) => new { Segment = segment, Index = index }) @@ -75,7 +85,6 @@ public class UrlEncodingModelBinder : IModelBinder // a value that's already had one pass of url decoding applied var rawUrl = bindingContext.HttpContext.Features.Get<IHttpRequestFeature>()?.RawTarget ?? string.Empty; - var request = bindingContext.HttpContext.Request; var rawValue = new Uri($"{request.Scheme}://{request.Host}{rawUrl}").AbsolutePath // discard query string .Split('/', StringSplitOptions.RemoveEmptyEntries) .ElementAtOrDefault(index); diff --git a/tests/slskd.Tests.Unit/Common/Middleware/UrlEncodingModelBinderTests.cs b/tests/slskd.Tests.Unit/Common/Middleware/UrlEncodingModelBinderTests.cs new file mode 100644 index 00000000..ea0bd746 --- /dev/null +++ b/tests/slskd.Tests.Unit/Common/Middleware/UrlEncodingModelBinderTests.cs @@ -0,0 +1,173 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; +using Microsoft.AspNetCore.Routing; +using Xunit; + +namespace slskd.Tests.Unit.Common.Middleware; + +public class UrlEncodingModelBinderTests +{ + [Theory] + [InlineData("api/v{version:apiVersion}/users/{username}/directory", "username", "http", "example.com", "/api/v0/users/test%20user/directory", "", "test user")] + [InlineData("api/v{version:apiVersion}/users/{username}/directory", "username", "https", "example.com", "/api/v0/users/user%2Fwith%2Fslash/directory", "", "user/with/slash")] + [InlineData("api/v{version:apiVersion}/users/{username}/directory", "username", "https", "example.com", "/api/v0/users/user%40special/directory", "", "user@special")] + public async Task BindModelAsync_WithoutPathBase_ExtractsCorrectValue( + string routeTemplate, + string modelName, + string scheme, + string host, + string rawUrl, + string pathBase, + string expectedValue) + { + var binder = new UrlEncodingModelBinder(); + var context = CreateModelBindingContext(routeTemplate, modelName, scheme, host, rawUrl, pathBase); + + await binder.BindModelAsync(context); + + Assert.True(context.Result.IsModelSet); + Assert.Equal(expectedValue, context.Result.Model); + } + + [Theory] + [InlineData("api/v{version:apiVersion}/users/{username}/directory", "username", "https", "media.example.com", "/slskd/api/v0/users/username/directory", "/slskd", "username")] + [InlineData("api/v{version:apiVersion}/users/{username}/directory", "username", "https", "example.com", "/myapp/api/v0/users/test%20user/directory", "/myapp", "test user")] + [InlineData("api/v{version:apiVersion}/users/{username}/directory", "username", "https", "example.com", "/base/api/v0/users/user%2Fwith%2Fslash/directory", "/base", "user/with/slash")] + [InlineData("api/v{version:apiVersion}/users/{username}/info", "username", "https", "example.com", "/prefix/api/v0/users/special%40char/info", "/prefix", "special@char")] + public async Task BindModelAsync_WithPathBase_ExtractsCorrectValue( + string routeTemplate, + string modelName, + string scheme, + string host, + string rawUrl, + string pathBase, + string expectedValue) + { + var binder = new UrlEncodingModelBinder(); + var context = CreateModelBindingContext(routeTemplate, modelName, scheme, host, rawUrl, pathBase); + + await binder.BindModelAsync(context); + + Assert.True(context.Result.IsModelSet); + Assert.Equal(expectedValue, context.Result.Model); + } + + [Fact] + public async Task BindModelAsync_WithQueryString_DiscardsQueryString() + { + var binder = new UrlEncodingModelBinder(); + var context = CreateModelBindingContext( + "api/v{version:apiVersion}/users/{username}/directory", + "username", + "https", + "example.com", + "/api/v0/users/testuser/directory?foo=bar&baz=qux", + ""); + + await binder.BindModelAsync(context); + + Assert.True(context.Result.IsModelSet); + Assert.Equal("testuser", context.Result.Model); + } + + [Fact] + public async Task BindModelAsync_WithPathBaseAndQueryString_ExtractsCorrectValue() + { + var binder = new UrlEncodingModelBinder(); + var context = CreateModelBindingContext( + "api/v{version:apiVersion}/users/{username}/directory", + "username", + "https", + "example.com", + "/slskd/api/v0/users/testuser/directory?param=value", + "/slskd"); + + await binder.BindModelAsync(context); + + Assert.True(context.Result.IsModelSet); + Assert.Equal("testuser", context.Result.Model); + } + + [Fact] + public async Task BindModelAsync_WithInvalidIndex_AddsModelError() + { + var binder = new UrlEncodingModelBinder(); + var context = CreateModelBindingContext( + "api/v{version:apiVersion}/users/{username}/directory", + "username", + "https", + "example.com", + "/api/v0", + ""); + + await binder.BindModelAsync(context); + + Assert.False(context.Result.IsModelSet); + Assert.True(context.ModelState.ContainsKey("username")); + Assert.Single(context.ModelState["username"].Errors); + } + + private ModelBindingContext CreateModelBindingContext( + string routeTemplate, + string modelName, + string scheme, + string host, + string rawUrl, + string pathBase) + { + var requestFeature = new HttpRequestFeature + { + Scheme = scheme, + Path = rawUrl.Split('?')[0], + PathBase = pathBase, + QueryString = rawUrl.Contains('?') ? rawUrl.Substring(rawUrl.IndexOf('?')) : string.Empty, + RawTarget = rawUrl, + Headers = new HeaderDictionary { { "Host", host } } + }; + + var featureCollection = new FeatureCollection(); + featureCollection.Set<IHttpRequestFeature>(requestFeature); + + var httpContext = new DefaultHttpContext(featureCollection); + httpContext.Request.Host = new HostString(host); + + var actionDescriptor = new ActionDescriptor(); + actionDescriptor.AttributeRouteInfo = new Microsoft.AspNetCore.Mvc.Routing.AttributeRouteInfo + { + Template = routeTemplate + }; + + var actionContext = new ActionContext( + httpContext, + new RouteData(), + actionDescriptor); + + var modelMetadata = new EmptyModelMetadataProvider().GetMetadataForType(typeof(string)); + + var valueProvider = new SimpleValueProvider(); + + var bindingContext = new DefaultModelBindingContext + { + ActionContext = actionContext, + ModelName = modelName, + ModelMetadata = modelMetadata, + ValueProvider = valueProvider, + ModelState = new ModelStateDictionary() + }; + + return bindingContext; + } + + private class SimpleValueProvider : IValueProvider + { + public bool ContainsPrefix(string prefix) => false; + public ValueProviderResult GetValue(string key) => ValueProviderResult.None; + } +} |
