Fixes an issue with some characters not recognized as ASCII (dotnet/corefx#36879)
authorJim <10913919+jimdemis@users.noreply.github.com>
Tue, 23 Apr 2019 21:29:19 +0000 (00:29 +0300)
committerDavid Shulman <david.shulman@microsoft.com>
Tue, 23 Apr 2019 21:29:18 +0000 (14:29 -0700)
* Fixes an issue with some characters not recognized as ASCII

Three characters "{","|" and "}" was being wrongfully marked as NonAscii, thus making IsWellFormedUriString function return false when combined with any other Unicode character(s).

Fix dotnet/corefx#36101

* restored original solution file

* Updated test

Added "|" character to test string
Removed not needed unicode string
Added skip om Net Framework

* Added more strings to test

* Added comment and removed not needed check for escaping

Commit migrated from https://github.com/dotnet/corefx/commit/e13d1c997475e938f6e1eee8c3c73cd166033090

src/libraries/System.Private.Uri/src/System/Uri.cs
src/libraries/System.Private.Uri/tests/FunctionalTests/UriIsWellFormedUriStringTest.cs

index c710f54c2ba05926a226e3e45f7ca83308c0bf02..5e7e22aa3672240c7567d830a4f95668c49c01d1 100644 (file)
@@ -4583,7 +4583,7 @@ namespace System
                     foundEscaping = true;
                     res |= Check.ReservedFound;
                 }
-                else if (c > 'z' && c != '~')
+                else if (c > '~')
                 {
                     if (_iriParsing)
                     {
@@ -4672,6 +4672,10 @@ namespace System
                         res |= Check.NotIriCanonical;
                     }
                 }
+                else if (c >= '{' && c <= '}') // includes '{', '|', '}'
+                {
+                    needsEscaping = true;
+                }
                 else if (c == '%')
                 {
                     if (!foundEscaping) foundEscaping = true;
index 6087fa865e7c61cf70f260d72b898659bed5abb8..8238436e754a97e00d0e9d1ace89460f44f9d9d4 100644 (file)
@@ -468,5 +468,46 @@ namespace System.PrivateUri.Tests
         {
             Assert.Equal(expected, Uri.IsWellFormedUriString(uriString, UriKind.RelativeOrAbsolute));
         }
+
+        public static IEnumerable<object[]> UriIsWellFormedUnwiseStringData =>
+        new List<object[]>
+        {
+            // escaped
+            new object[] { "https://www.contoso.com/?a=%7B%7C%7D&b=%E2%80%99", true },
+            new object[] { "https://www.contoso.com/?a=%7B%7C%7D%E2%80%99", true },
+            
+            // unescaped
+            new object[] { "https://www.contoso.com/?a=}", false },
+            new object[] { "https://www.contoso.com/?a=|", false },
+            new object[] { "https://www.contoso.com/?a={", false },
+
+            // not query
+            new object[] { "https://www.%7Bcontoso.com/", false },
+            new object[] { "http%7Bs://www.contoso.com/", false },
+            new object[] { "https://www.contoso.com%7B/", false },
+            new object[] { "htt%7Cps://www.contoso.com/", false },
+            new object[] { "https://www.con%7Ctoso.com/", false },
+            new object[] { "https://www.contoso.com%7C/", false },
+            new object[] { "htt%7Dps://www.contoso.com/", false },
+            new object[] { "https://www.con%7Dtoso.com/", false },
+            new object[] { "https://www.contoso.com%7D/", false },
+            new object[] { "htt{ps://www.contoso.com/", false },
+            new object[] { "https://www.con{toso.com/", false },
+            new object[] { "https://www.contoso.com{/", false },
+            new object[] { "htt|ps://www.contoso.com/", false },
+            new object[] { "https://www.con|toso.com/", false },
+            new object[] { "https://www.contoso.com|/", false },
+            new object[] { "htt}ps://www.contoso.com/", false },
+            new object[] { "https://www.con}toso.com/", false },
+            new object[] { "https://www.contoso.com}/", false },
+        };
+
+        [Theory]
+        [MemberData(nameof(UriIsWellFormedUnwiseStringData))]
+        [SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
+        public void UriIsWellFormed_AbsoluteUnicodeWithUnwise_Success(string uriString, bool expected)
+        {
+            Assert.Equal(expected, Uri.IsWellFormedUriString(uriString, UriKind.Absolute));
+        }
     }
 }