From d8b4645a4d44aae7e79adfda2f9508342a4252d4 Mon Sep 17 00:00:00 2001 From: ificator Date: Sat, 10 Jun 2023 16:36:50 -0700 Subject: [PATCH] Make sure 'FromUriComponent' and 'ToUriComponent' are symmetric --- src/Microsoft.Owin/PathString.cs | 15 +++------------ tests/Microsoft.Owin.Tests/PathStringTests.cs | 6 +++--- tests/Microsoft.Owin.Tests/UriTests.cs | 2 +- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.Owin/PathString.cs b/src/Microsoft.Owin/PathString.cs index 67b0d46a1..4a213b5df 100644 --- a/src/Microsoft.Owin/PathString.cs +++ b/src/Microsoft.Owin/PathString.cs @@ -81,8 +81,7 @@ public string ToUriComponent() while (i < _value.Length) { - var isPercentEncodedChar = PathStringHelper.IsPercentEncodedChar(_value, i); - if (PathStringHelper.IsValidPathChar(_value[i]) || isPercentEncodedChar) + if (PathStringHelper.IsValidPathChar(_value[i])) { if (requiresEscaping) { @@ -99,16 +98,8 @@ public string ToUriComponent() count = 0; } - if (isPercentEncodedChar) - { - count += 3; - i += 3; - } - else - { - count++; - i++; - } + count++; + i++; } else { diff --git a/tests/Microsoft.Owin.Tests/PathStringTests.cs b/tests/Microsoft.Owin.Tests/PathStringTests.cs index bf6608765..da7fc509f 100644 --- a/tests/Microsoft.Owin.Tests/PathStringTests.cs +++ b/tests/Microsoft.Owin.Tests/PathStringTests.cs @@ -170,11 +170,11 @@ public void EscapingIsCorrectWhenUserDefinedPathHasValueWhichHappensToBeAnEscape singleEscapedPath.Value.ShouldBe("/one%2Ftwo"); var doubleEscapedString = singleEscapedPath.ToUriComponent(); - doubleEscapedString.ShouldBe("/one%2Ftwo"); + doubleEscapedString.ShouldBe("/one%252Ftwo"); var recreatedPath = PathString.FromUriComponent(doubleEscapedString); - recreatedPath.Value.ShouldBe("/one/two"); - recreatedPath.ToUriComponent().ShouldBe("/one/two"); + recreatedPath.Value.ShouldBe("/one%2Ftwo"); + recreatedPath.ToUriComponent().ShouldBe("/one%252Ftwo"); } [Theory] diff --git a/tests/Microsoft.Owin.Tests/UriTests.cs b/tests/Microsoft.Owin.Tests/UriTests.cs index 68194bf92..6e278a2f5 100644 --- a/tests/Microsoft.Owin.Tests/UriTests.cs +++ b/tests/Microsoft.Owin.Tests/UriTests.cs @@ -23,7 +23,7 @@ public class UriTests [InlineData("/a%.+#?", "/z", "a#b", "http://host:1/a%25.+%23%3F/z?a%23b")] // Note: Http.Sys will not accept any characters in the path that it cannot un-escape, // so this double escaping is not a problem in production. - [InlineData("", "/%20", "%20", "http://host:1/%20?%20")] + [InlineData("", "/%20", "%20", "http://host:1/%2520?%20")] public void UriReconstruction(string pathBase, string path, string query, string expected) { IOwinRequest request = CreateRequest(pathBase, path, query);