Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1232318 fix account name validation #890

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Snowflake.Data.Tests/UnitTests/SFSessionPropertyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,36 @@ public void TestThatPropertiesAreParsed(TestCase testcase)
CollectionAssert.AreEquivalent(testcase.ExpectedProperties, properties);
}

[Test]
[TestCase("a", "a", "a.snowflakecomputing.com")]
sfc-gh-knozderko marked this conversation as resolved.
Show resolved Hide resolved
[TestCase("ab", "ab", "ab.snowflakecomputing.com")]
[TestCase("a.b", "a", "a.b.snowflakecomputing.com")]
[TestCase("a-b", "a-b", "a-b.snowflakecomputing.com")]
[TestCase("a_b", "a_b", "a-b.snowflakecomputing.com")]
[TestCase("abc", "abc", "abc.snowflakecomputing.com")]
[TestCase("xy12345.us-east-2.aws", "xy12345", "xy12345.us-east-2.aws.snowflakecomputing.com")]
public void TestValidateCorrectAccountNames(string accountName, string expectedAccountName, string expectedHost)
{
// arrange
var connectionString = $"ACCOUNT={accountName};USER=test;PASSWORD=test;";

// act
var properties = SFSessionProperties.parseConnectionString(connectionString, null);

// assert
Assert.AreEqual(expectedAccountName, properties[SFSessionProperty.ACCOUNT]);
Assert.AreEqual(expectedHost, properties[SFSessionProperty.HOST]);
}

[Test]
[TestCase("ACCOUNT=testaccount;USER=testuser;PASSWORD=testpassword;FILE_TRANSFER_MEMORY_THRESHOLD=0;", "Error: Invalid parameter value 0 for FILE_TRANSFER_MEMORY_THRESHOLD")]
[TestCase("ACCOUNT=testaccount;USER=testuser;PASSWORD=testpassword;FILE_TRANSFER_MEMORY_THRESHOLD=xyz;", "Error: Invalid parameter value xyz for FILE_TRANSFER_MEMORY_THRESHOLD")]
[TestCase("ACCOUNT=testaccount?;USER=testuser;PASSWORD=testpassword", "Error: Invalid parameter value testaccount? for ACCOUNT")]
[TestCase("ACCOUNT=complicated.long.testaccount?;USER=testuser;PASSWORD=testpassword", "Error: Invalid parameter value complicated.long.testaccount? for ACCOUNT")]
[TestCase("ACCOUNT=?testaccount;USER=testuser;PASSWORD=testpassword", "Error: Invalid parameter value ?testaccount for ACCOUNT")]
[TestCase("ACCOUNT=.testaccount;USER=testuser;PASSWORD=testpassword", "Error: Invalid parameter value .testaccount for ACCOUNT")]
[TestCase("ACCOUNT=testaccount.;USER=testuser;PASSWORD=testpassword", "Error: Invalid parameter value testaccount. for ACCOUNT")]
[TestCase("ACCOUNT=test%account;USER=testuser;PASSWORD=testpassword", "Error: Invalid parameter value test%account for ACCOUNT")]
public void TestThatItFailsForWrongConnectionParameter(string connectionString, string expectedErrorMessagePart)
{
// act
Expand Down
17 changes: 13 additions & 4 deletions Snowflake.Data/Core/Session/SFSessionProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ class SFSessionProperties : Dictionary<SFSessionProperty, String>
SFSessionProperty.PRIVATE_KEY_PWD,
SFSessionProperty.PROXYPASSWORD,
};

private const string AccountRegexString = "^\\w[\\w.-]+\\w$";

private static readonly List<string> s_accountRegexStrings = new List<string>
sfc-gh-mhofman marked this conversation as resolved.
Show resolved Hide resolved
{
"^\\w",
"\\w$",
"^[\\w.-]+$"
};

public override bool Equals(object obj)
{
Expand Down Expand Up @@ -292,8 +297,7 @@ private static void ValidateAccountDomain(SFSessionProperties properties)
var account = properties[SFSessionProperty.ACCOUNT];
if (string.IsNullOrEmpty(account))
return;
var match = Regex.Match(account, AccountRegexString, RegexOptions.IgnoreCase);
if (match.Success)
if (IsAccountRegexMatched(account))
return;
logger.Error($"Invalid account {account}");
throw new SnowflakeDbException(
Expand All @@ -303,6 +307,11 @@ private static void ValidateAccountDomain(SFSessionProperties properties)
SFSessionProperty.ACCOUNT);
}

private static bool IsAccountRegexMatched(string account) =>
s_accountRegexStrings
.Select(regex => Regex.Match(account, regex, RegexOptions.IgnoreCase))
.All(match => match.Success);

private static void checkSessionProperties(SFSessionProperties properties)
{
foreach (SFSessionProperty sessionProperty in Enum.GetValues(typeof(SFSessionProperty)))
Expand Down
Loading