From 67e9a10f919aa473d4685ddeb3cd7fc621191eb6 Mon Sep 17 00:00:00 2001 From: Brian Walker Date: Mon, 24 Feb 2020 12:28:05 -0600 Subject: [PATCH] Fixes #32230 (#32302) Use insecure proxy (http_proxy) for schemes http and ws Use secure proxy (https_proxy) to schemes https and wss --- .../SocketsHttpHandler/HttpEnvironmentProxy.cs | 2 +- .../tests/UnitTests/HttpEnvironmentProxyTest.cs | 25 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpEnvironmentProxy.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpEnvironmentProxy.cs index a2150ed..43268fe 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpEnvironmentProxy.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpEnvironmentProxy.cs @@ -266,7 +266,7 @@ namespace System.Net.Http /// public Uri GetProxy(Uri uri) { - return uri.Scheme == Uri.UriSchemeHttp ? _httpProxyUri : _httpsProxyUri; + return HttpUtilities.IsSupportedNonSecureScheme(uri.Scheme) ? _httpProxyUri : _httpsProxyUri; } /// diff --git a/src/libraries/System.Net.Http/tests/UnitTests/HttpEnvironmentProxyTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/HttpEnvironmentProxyTest.cs index dd9607e..e168d3f 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/HttpEnvironmentProxyTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/HttpEnvironmentProxyTest.cs @@ -16,6 +16,8 @@ namespace System.Net.Http.Tests private readonly ITestOutputHelper _output; private static readonly Uri fooHttp = new Uri("http://foo.com"); private static readonly Uri fooHttps = new Uri("https://foo.com"); + private static readonly Uri fooWs = new Uri("ws://foo.com"); + private static readonly Uri fooWss = new Uri("wss://foo.com"); // This will clean specific environmental variables // to be sure they do not interfere with the test. @@ -61,6 +63,11 @@ namespace System.Net.Http.Tests u = p.GetProxy(fooHttps); Assert.True(u != null && u.Host == "1.1.1.1"); + u = p.GetProxy(fooWs); + Assert.True(u != null && u.Host == "1.1.1.1"); + u = p.GetProxy(fooWss); + Assert.True(u != null && u.Host == "1.1.1.1"); + Environment.SetEnvironmentVariable("http_proxy", "http://1.1.1.2:3001"); Assert.True(HttpEnvironmentProxy.TryCreate(out p)); Assert.NotNull(p); @@ -72,6 +79,11 @@ namespace System.Net.Http.Tests u = p.GetProxy(fooHttps); Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000); + u = p.GetProxy(fooWs); + Assert.True(u != null && u.Host == "1.1.1.2" && u.Port == 3001); + u = p.GetProxy(fooWss); + Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000); + // Set https to invalid strings and use only IP & port for http. Environment.SetEnvironmentVariable("http_proxy", "1.1.1.3:3003"); Environment.SetEnvironmentVariable("https_proxy", "ab!cd"); @@ -83,6 +95,11 @@ namespace System.Net.Http.Tests u = p.GetProxy(fooHttps); Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000); + u = p.GetProxy(fooWs); + Assert.True(u != null && u.Host == "1.1.1.3" && u.Port == 3003); + u = p.GetProxy(fooWss); + Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000); + // Try valid URI with unsupported protocol. It will be ignored // to mimic curl behavior. Environment.SetEnvironmentVariable("https_proxy", "socks5://1.1.1.4:3004"); @@ -91,6 +108,9 @@ namespace System.Net.Http.Tests u = p.GetProxy(fooHttps); Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000); + u = p.GetProxy(fooWss); + Assert.True(u != null && u.Host == "1.1.1.1" && u.Port == 3000); + // Set https to valid URI but different from http. Environment.SetEnvironmentVariable("https_proxy", "http://1.1.1.5:3005"); Assert.True(HttpEnvironmentProxy.TryCreate(out p)); @@ -100,6 +120,11 @@ namespace System.Net.Http.Tests Assert.True(u != null && u.Host == "1.1.1.3" && u.Port == 3003); u = p.GetProxy(fooHttps); Assert.True(u != null && u.Host == "1.1.1.5" && u.Port == 3005); + + u = p.GetProxy(fooWs); + Assert.True(u != null && u.Host == "1.1.1.3" && u.Port == 3003); + u = p.GetProxy(fooWss); + Assert.True(u != null && u.Host == "1.1.1.5" && u.Port == 3005); }).Dispose(); } -- 2.7.4