aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-01-11 18:44:04 -0600
committerJP Dillingham <jp@dillingham.me>2026-01-11 18:44:04 -0600
commite7e0cdaf762b10027e8417ebc985d621ddb87eca (patch)
tree413bf520ad0ff1f5005a661a12810bae254feda6 /src/slskd/Common/Middleware/UrlEncodingModelBinder.cs
parent8849d78cbe7057dacad9c00c471691af8e367cd9 (diff)
downloadslskd-e7e0cdaf762b10027e8417ebc985d621ddb87eca.tar.gz
use raw url, before any decoding
Diffstat (limited to 'src/slskd/Common/Middleware/UrlEncodingModelBinder.cs')
-rw-r--r--src/slskd/Common/Middleware/UrlEncodingModelBinder.cs49
1 files changed, 37 insertions, 12 deletions
diff --git a/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs b/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs
index 2c6f341f..9ec52302 100644
--- a/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs
+++ b/src/slskd/Common/Middleware/UrlEncodingModelBinder.cs
@@ -20,6 +20,7 @@ namespace slskd;
using System;
using System.Linq;
using System.Threading.Tasks;
+using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata;
@@ -42,27 +43,51 @@ public class UrlEncodingModelBinder : IModelBinder
/// <inheritdoc/>
public Task BindModelAsync(ModelBindingContext bindingContext)
{
+ /*
+ given the information in the provided ModelBindingContext,
+
+ 1. determine which 'model' (named route parameter) we're trying to bind a value for (e.g. 'foo')
+ 2. from the full route template (e.g. api/v{version:apiVersion}/foo/{bar}), determine the index of
+ the model we are looking for in the template, segmented by forward slashes (e.g. 3rd in example above)
+ 3. from the raw, unmanipulated request URL, extract the Nth forward-slash-separated value; this will be our
+ url encoded value as provided by the client
+ 4. decode this value and assign it to ModelBindingContext.Result
+
+
+ template: api/v{version:apiVersion}/foo/{bar}
+ model: bar
+ index: 3
+ raw url: api/v0/foo/hello%20world
+ raw value: 'hello%20world' (3rd index in split string)
+ result: 'hello world'
+ */
try
{
- // get the raw value that was passed, if any
- var potentiallyUrlEncodedResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
+ var model = $"{{{bindingContext.ModelName}}}";
+ var template = bindingContext.ActionContext.ActionDescriptor.AttributeRouteInfo?.Template ?? string.Empty;
+ var index = template
+ .Split('/', StringSplitOptions.RemoveEmptyEntries)
+ .Select((segment, index) => new { Segment = segment, Index = index })
+ .Single(x => x.Segment.Equals(model, StringComparison.OrdinalIgnoreCase)).Index;
- if (potentiallyUrlEncodedResult == ValueProviderResult.None)
- {
- return Task.CompletedTask;
- }
+ // get the raw URL, *before* any sort of processing or default url-decoding is applied
+ // this is necessary to avoid double-decoding issues; using HttpContext.Request.Path gives us
+ // a value that's already had one pass of url decoding applied
+ var rawUrl = bindingContext.HttpContext.Features.Get<IHttpRequestFeature>()?.RawTarget ?? string.Empty;
- var value = potentiallyUrlEncodedResult.FirstValue;
+ var request = bindingContext.HttpContext.Request;
+ var rawValue = new Uri($"{request.Scheme}://{request.Host}{rawUrl}").AbsolutePath // discard query string
+ .Split('/', StringSplitOptions.RemoveEmptyEntries)
+ .ElementAtOrDefault(index);
- if (string.IsNullOrEmpty(value))
+ if (rawValue is null)
{
+ bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, "Could not extract URL encoded value from URL");
return Task.CompletedTask;
}
- // a value was provided; decode it
- var decodedValue = Uri.UnescapeDataString(value);
-
- bindingContext.Result = ModelBindingResult.Success(decodedValue);
+ var decoded = Uri.UnescapeDataString(rawValue);
+ bindingContext.Result = ModelBindingResult.Success(decoded);
return Task.CompletedTask;
}
catch (Exception ex)