Explorar el Código

HttpSys Http/2 requests should not be upgradable (#28041)

Chris Ross hace 5 años
padre
commit
5ed14d8104

+ 1 - 1
src/Servers/HttpSys/src/RequestProcessing/Request.cs

@@ -254,7 +254,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
         public string Scheme => IsHttps ? Constants.HttpsScheme : Constants.HttpScheme;
 
         // HTTP.Sys allows you to upgrade anything to opaque unless content-length > 0 or chunked are specified.
-        internal bool IsUpgradable => !HasEntityBody && ComNetOS.IsWin8orLater;
+        internal bool IsUpgradable => ProtocolVersion < HttpVersion.Version20 && !HasEntityBody && ComNetOS.IsWin8orLater;
 
         internal WindowsPrincipal User { get; }
 

+ 3 - 1
src/Servers/HttpSys/test/FunctionalTests/Http2Tests.cs

@@ -29,8 +29,10 @@ namespace Microsoft.AspNetCore.Server.HttpSys.FunctionalTests
         {
             using var server = Utilities.CreateDynamicHttpsServer(out var address, httpContext =>
             {
-                // Default 200
+                var feature = httpContext.Features.Get<IHttpUpgradeFeature>();
+                Assert.False(feature.IsUpgradableRequest);
                 Assert.False(httpContext.Request.CanHaveBody());
+                // Default 200
                 return Task.CompletedTask;
             });
 

+ 31 - 1
src/Servers/HttpSys/test/FunctionalTests/OpaqueUpgradeTests.cs

@@ -57,6 +57,7 @@ namespace Microsoft.AspNetCore.Server.HttpSys
                 }
                 catch (Exception ex)
                 {
+                    httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
                     return httpContext.Response.WriteAsync(ex.ToString());
                 }
                 return Task.FromResult(0);
@@ -267,7 +268,36 @@ namespace Microsoft.AspNetCore.Server.HttpSys
                 }
             }))
             {
-                await Assert.ThrowsAsync<InvalidOperationException>(async () => await SendOpaqueRequestAsync(method, address, extraHeader));
+                var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () => await SendOpaqueRequestAsync(method, address, extraHeader));
+                Assert.Equal("The response status code was incorrect: HTTP/1.1 200 OK", ex.Message);
+            }
+        }
+
+        [ConditionalFact]
+        [MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win8)]
+        public async Task OpaqueUpgrade_PostWithBodyAndUpgradeHeaders_Accepted()
+        {
+            using (Utilities.CreateHttpServer(out string address, async httpContext =>
+            {
+                try
+                {
+                    var opaqueFeature = httpContext.Features.Get<IHttpUpgradeFeature>();
+                    Assert.NotNull(opaqueFeature);
+                    Assert.False(opaqueFeature.IsUpgradableRequest);
+                }
+                catch (Exception ex)
+                {
+                    await httpContext.Response.WriteAsync(ex.ToString());
+                }
+            }))
+            {
+                using var client = new HttpClient();
+
+                var request = new HttpRequestMessage(HttpMethod.Post, address);
+                request.Headers.Connection.Add("upgrade");
+                request.Content = new StringContent("Hello World");
+                var response = await client.SendAsync(request);
+                response.EnsureSuccessStatusCode();
             }
         }