Fix index out of range and add reg tests

This commit is contained in:
Bond_009 2021-06-08 15:35:49 +02:00
parent ed2f08d05f
commit 2fc14375f8
2 changed files with 32 additions and 2 deletions

View File

@ -55,7 +55,7 @@ namespace Jellyfin.Server.Middleware
// Unencode and re-parse querystring. // Unencode and re-parse querystring.
var unencodedKey = HttpUtility.UrlDecode(key); var unencodedKey = HttpUtility.UrlDecode(key);
if (string.Equals(unencodedKey, key, System.StringComparison.Ordinal)) if (string.Equals(unencodedKey, key, StringComparison.Ordinal))
{ {
// Don't do anything if it's not encoded. // Don't do anything if it's not encoded.
_store = value; _store = value;
@ -72,7 +72,7 @@ namespace Jellyfin.Server.Middleware
if (i == -1) if (i == -1)
{ {
// encoded is an equals. // encoded is an equals.
pairs.Add(pair[..i].ToString(), StringValues.Empty); pairs.Add(pair.ToString(), StringValues.Empty);
continue; continue;
} }

View File

@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.Linq;
using Jellyfin.Server.Middleware;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Primitives;
using Xunit;
namespace Jellyfin.Server.Tests
{
public static class UrlDecodeQueryFeatureTests
{
[Theory]
[InlineData("e0a72cb2a2c7", "e0a72cb2a2c7")] // isn't encoded
[InlineData("random+test", "random test")] // encoded
[InlineData("random%20test", "random test")] // encoded
public static void EmptyValueTest(string query, string key)
{
var dict = new Dictionary<string, StringValues>
{
{ query, StringValues.Empty }
};
var test = new UrlDecodeQueryFeature(new QueryFeature(new QueryCollection(dict)));
Assert.Single(test.Query);
var (k, v) = test.Query.First();
Assert.Equal(k, key);
Assert.Empty(v);
}
}
}