Skip to content

Commit

Permalink
Added start and end symbol to match string with full regex to bypass …
Browse files Browse the repository at this point in the history
…the proxy server.
  • Loading branch information
sfc-gh-jmartinezramirez committed Mar 27, 2024
1 parent ee0cdaa commit 2dbba02
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
52 changes: 51 additions & 1 deletion Snowflake.Data.Tests/IntegrationTests/SFConnectionIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions Snowflake.Data/Core/HttpUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace Snowflake.Data.Core
{
using System.Text.RegularExpressions;

public class HttpClientConfig
{
public HttpClientConfig(
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 2dbba02

Please sign in to comment.