Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure 'FromUriComponent' and 'ToUriComponent' are symmetric #506

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/Microsoft.Owin/PathString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -99,16 +98,8 @@ public string ToUriComponent()
count = 0;
}

if (isPercentEncodedChar)
{
count += 3;
i += 3;
}
else
{
count++;
i++;
}
count++;
i++;
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Microsoft.Owin.Tests/PathStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/Microsoft.Owin.Tests/UriTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down