From 2fc14375f8b935b2530c82a23621e3f001b6c05c Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Tue, 8 Jun 2021 15:35:49 +0200 Subject: [PATCH] Fix index out of range and add reg tests --- .../Middleware/UrlDecodeQueryFeature.cs | 4 +-- .../UrlDecodeQueryFeatureTests.cs | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/Jellyfin.Server.Tests/UrlDecodeQueryFeatureTests.cs diff --git a/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs b/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs index 804e58b5a..f1b47ce33 100644 --- a/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs +++ b/Jellyfin.Server/Middleware/UrlDecodeQueryFeature.cs @@ -55,7 +55,7 @@ namespace Jellyfin.Server.Middleware // Unencode and re-parse querystring. 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. _store = value; @@ -72,7 +72,7 @@ namespace Jellyfin.Server.Middleware if (i == -1) { // encoded is an equals. - pairs.Add(pair[..i].ToString(), StringValues.Empty); + pairs.Add(pair.ToString(), StringValues.Empty); continue; } diff --git a/tests/Jellyfin.Server.Tests/UrlDecodeQueryFeatureTests.cs b/tests/Jellyfin.Server.Tests/UrlDecodeQueryFeatureTests.cs new file mode 100644 index 000000000..d0eac138a --- /dev/null +++ b/tests/Jellyfin.Server.Tests/UrlDecodeQueryFeatureTests.cs @@ -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 + { + { 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); + } + } +}