Fixes and tests (#84630)
authorMorten Larsen <mla@specialisterne.com>
Tue, 11 Apr 2023 17:53:35 +0000 (19:53 +0200)
committerGitHub <noreply@github.com>
Tue, 11 Apr 2023 17:53:35 +0000 (10:53 -0700)
src/libraries/System.Net.Http/src/System/Net/Http/Headers/AltSvcHeaderParser.cs
src/libraries/System.Net.Http/src/System/Net/Http/Headers/AuthenticationHeaderValue.cs
src/libraries/System.Net.Http/src/System/Net/Http/Headers/EntityTagHeaderValue.cs
src/libraries/System.Net.Http/tests/UnitTests/Headers/AltSvcHeaderParserTest.cs
src/libraries/System.Net.Http/tests/UnitTests/Headers/AuthenticationHeaderParserTest.cs [new file with mode: 0644]
src/libraries/System.Net.Http/tests/UnitTests/Headers/EntityTagHeaderParserTest.cs [new file with mode: 0644]
src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj [changed mode: 0644->0755]

index 5c63f09..81da0bf 100644 (file)
@@ -56,7 +56,8 @@ namespace System.Net.Http.Headers
                 return idx - startIndex;
             }
 
-            if (idx == value.Length || value[idx++] != '=')
+            // Make sure we have at least 2 characters and first one being an '='.
+            if (idx + 1 >= value.Length || value[idx++] != '=')
             {
                 parsedValue = null;
                 return 0;
index 76c53e4..fedc310 100644 (file)
@@ -119,7 +119,7 @@ namespace System.Net.Http.Headers
 
             parsedValue = null;
 
-            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length))
+            if (string.IsNullOrEmpty(input) || (startIndex >= input.Length) || HttpRuleParser.ContainsNewLine(input, startIndex))
             {
                 return 0;
             }
index 70f740f..fb1e261 100644 (file)
@@ -148,7 +148,7 @@ namespace System.Net.Http.Headers
 
                 int tagStartIndex = current;
                 int tagLength;
-                if (HttpRuleParser.GetQuotedStringLength(input, current, out tagLength) != HttpParseResult.Parsed)
+                if (current == input.Length || HttpRuleParser.GetQuotedStringLength(input, current, out tagLength) != HttpParseResult.Parsed)
                 {
                     return 0;
                 }
index c40eef9..b298672 100644 (file)
@@ -9,6 +9,16 @@ namespace System.Net.Http.Tests
 {
     public class AltSvcHeaderParserTest
     {
+        [Fact]
+        public void TryParse_InvalidValueString_ReturnsFalse()
+        {
+            HttpHeaderParser parser = AltSvcHeaderParser.Parser;
+            string invalidInput = "a=";
+            int startIndex = 0;
+
+            Assert.False(parser.TryParseValue(invalidInput, null, ref startIndex, out var _));
+        }
+
         [Theory]
         [MemberData(nameof(SuccessfulParseData))]
         public void TryParse_Success(string value, object[] expectedServicesObj)
diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/AuthenticationHeaderParserTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/AuthenticationHeaderParserTest.cs
new file mode 100644 (file)
index 0000000..4204432
--- /dev/null
@@ -0,0 +1,22 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Net.Http.Headers;
+
+using Xunit;
+
+namespace System.Net.Http.Tests
+{
+    public class AuthenticationHeaderParserTest
+    {
+        [Fact]
+        public void TryParse_InvalidValueString_ReturnsFalse()
+        {
+            HttpHeaderParser parser = GenericHeaderParser.MultipleValueAuthenticationParser;
+            string invalidInput = "a \n";
+            int startIndex = 0;
+
+            Assert.False(parser.TryParseValue(invalidInput, null, ref startIndex, out var _));
+        }
+    }
+}
diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/EntityTagHeaderParserTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/EntityTagHeaderParserTest.cs
new file mode 100644 (file)
index 0000000..e926d95
--- /dev/null
@@ -0,0 +1,22 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Net.Http.Headers;
+
+using Xunit;
+
+namespace System.Net.Http.Tests
+{
+    public class EntityTagHeaderParserTest
+    {
+        [Fact]
+        public void TryParse_InvalidValueString_ReturnsFalse()
+        {
+            HttpHeaderParser parser = GenericHeaderParser.MultipleValueEntityTagParser;
+            string invalidInput = "w/\t\t";
+            int startIndex = 0;
+
+            Assert.False(parser.TryParseValue(invalidInput, null, ref startIndex, out var _));
+        }
+    }
+}
old mode 100644 (file)
new mode 100755 (executable)
index f2d027b..c74be90
     <Compile Include="Fakes\HttpTelemetry.cs" />
     <Compile Include="Fakes\MacProxy.cs" Condition="'$(TargetPlatformIdentifier)' == 'osx' or '$(TargetPlatformIdentifier)' == 'ios' or '$(TargetPlatformIdentifier)' == 'tvos'" />
     <Compile Include="Headers\AltSvcHeaderParserTest.cs" />
+    <Compile Include="Headers\AuthenticationHeaderParserTest.cs" />
     <Compile Include="Headers\AuthenticationHeaderValueTest.cs" />
     <Compile Include="Headers\ByteArrayHeaderParserTest.cs" />
     <Compile Include="Headers\CacheControlHeaderParserTest.cs" />
     <Compile Include="Headers\ContentDispositionHeaderValueTest.cs" />
     <Compile Include="Headers\ContentRangeHeaderValueTest.cs" />
     <Compile Include="Headers\DateHeaderParserTest.cs" />
+    <Compile Include="Headers\EntityTagHeaderParserTest.cs" />
     <Compile Include="Headers\EntityTagHeaderValueTest.cs" />
     <Compile Include="Headers\GenericHeaderParserTest\AuthenticationParserTest.cs" />
     <Compile Include="Headers\GenericHeaderParserTest\ContentRangeParserTest.cs" />