From 2dbba0253403eb73ef0ec63dd922462ed11de00e Mon Sep 17 00:00:00 2001 From: Juan Martinez Ramirez Date: Tue, 26 Mar 2024 17:18:52 -0600 Subject: [PATCH] Added start and end symbol to match string with full regex to bypass the proxy server. --- .../IntegrationTests/SFConnectionIT.cs | 52 ++++++++++++++++++- Snowflake.Data/Core/HttpUtil.cs | 6 +++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs index 8d69fe606..897a12f07 100644 --- a/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs +++ b/Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs @@ -1540,6 +1540,56 @@ public void TestInvalidProxySettingFromConnectionString() } } + [Test] + [TestCase("*")] + [TestCase("*{0}")] + [TestCase("^*{0}$")] + [TestCase("^nonmatch*{0}$|*")] + [TestCase("*a*", "a")] + [TestCase("*la*", "la")] + public void TestNonProxyHostShouldBypassProxyServer(string regexHost, string proxyHost = null) + { + using (var conn = new SnowflakeDbConnection()) + { + var nonProxyHosts = string.Format(regexHost, $"{testConfig.host}"); + var proxyHostForConnection = proxyHost ?? "proxyserverhost"; + conn.ConnectionString = + $"{ConnectionString}USEPROXY=true;PROXYHOST={proxyHostForConnection};NONPROXYHOSTS={nonProxyHosts};PROXYPORT=3128;"; + conn.Open(); + Assert.AreEqual(ConnectionState.Open, conn.State); + } + } + + [Test] + [TestCase("invalid{0}")] + [TestCase("*invalid{0}*")] + [TestCase("^invalid{0}$")] + [TestCase("*a.b")] + [TestCase("a", "a")] + [TestCase("la", "la")] + public void TestNonProxyHostShouldNotBypassProxyServer(string regexHost, string proxyHost = null) + { + using (var conn = new SnowflakeDbConnection()) + { + var nonProxyHosts = string.Format(regexHost, $"{testConfig.host}"); + var proxyHostForConnection = proxyHost ?? "proxyserverhost"; + conn.ConnectionString = + $"{ConnectionString}connection_timeout=5;USEPROXY=true;PROXYHOST={proxyHostForConnection};NONPROXYHOSTS={nonProxyHosts};PROXYPORT=3128;"; + try + { + conn.Open(); + //Assert.Fail(); + } + catch (SnowflakeDbException e) + { + // Expected + s_logger.Debug("Failed opening connection ", e); + Assert.AreEqual(270001, e.ErrorCode); //Internal error + AssertIsConnectionFailure(e); + } + } + } + [Test] public void TestUseProxyFalseWithInvalidProxyConnectionString() { @@ -1561,7 +1611,7 @@ public void TestInvalidProxySettingWithByPassListFromConnectionString() = ConnectionString + String.Format( ";useProxy=true;proxyHost=Invalid;proxyPort=8080;nonProxyHosts={0}", - "*.foo.com %7C" + testConfig.account + ".snowflakecomputing.com|" + testConfig.host); + $"*.foo.com %7C{testConfig.account}.snowflakecomputing.com|*{testConfig.host}"); conn.Open(); // Because testConfig.host is in the bypass list, the proxy should not be used } diff --git a/Snowflake.Data/Core/HttpUtil.cs b/Snowflake.Data/Core/HttpUtil.cs index 9c5e22442..a234c3e3d 100755 --- a/Snowflake.Data/Core/HttpUtil.cs +++ b/Snowflake.Data/Core/HttpUtil.cs @@ -18,6 +18,8 @@ namespace Snowflake.Data.Core { + using System.Text.RegularExpressions; + public class HttpClientConfig { public HttpClientConfig( @@ -187,6 +189,10 @@ internal HttpMessageHandler SetupCustomHttpHandler(HttpClientConfig config) // * -> .* because * is a quantifier and need a char or group to apply to entry = entry.Replace("*", ".*"); + if (!entry.StartsWith("^") || !entry.EndsWith("$")) + { + entry = $"^{entry}$"; + } // Replace with the valid entry syntax bypassList[i] = entry;