aspnet / AspNetKatana

Microsoft's OWIN implementation, the Katana project
Apache License 2.0
959 stars 331 forks source link

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

Open ificator opened 1 year ago

ificator commented 1 year ago

This PR fixes https://github.com/aspnet/AspNetKatana/issues/393.

Currently ToUriComponent will skip encoding the % when it looks like it's part of percent-encoded character:

var ps = new PathString("/test%20name.txt");
Console.WriteLine("{0}", ps.ToUriComponent()); // Output: /test%20name.txt

However, ToUriComponent should output a correctly encoded version of the decoded path represented by the PathString:

var ps = new PathString("/test%20name.txt");
Console.WriteLine("{0}", ps.ToUriComponent()); // Output: /test%2520name.txt

Said another way, the FromUriComponent and ToUriComponent methods should be symmetrical:

string input = "/test%2520name.txt";
var ps = PathString.FromUriComponent(input);
string output = ps.ToUriComponent();
Console.WriteLine("{0}", input == output); // Expect this to output 'true'

The fix is to remove the check for percent encoding.