Skip to content

Commit c0b83ce

Browse files
committed
Add escaping
1 parent bbcf3b5 commit c0b83ce

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Diff for: csharp/src/Drivers/Databricks/DatabricksConnection.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ public async Task ApplyServerSidePropertiesAsync()
150150

151151
foreach (var property in serverSideProperties)
152152
{
153-
string query = $"SET {property.Key}={property.Value}";
153+
if (!IsValidPropertyName(property.Key))
154+
{
155+
Debug.WriteLine($"Skipping invalid property name: {property.Key}");
156+
continue;
157+
}
158+
159+
string escapedValue = EscapeSqlString(property.Value);
160+
string query = $"SET {property.Key}={escapedValue}";
154161
statement.SqlQuery = query;
155162

156163
try
@@ -164,6 +171,19 @@ public async Task ApplyServerSidePropertiesAsync()
164171
}
165172
}
166173

174+
private bool IsValidPropertyName(string propertyName)
175+
{
176+
// Allow only alphanumeric characters and underscores in property names
177+
return System.Text.RegularExpressions.Regex.IsMatch(
178+
propertyName,
179+
@"^[a-zA-Z0-9_]+$");
180+
}
181+
182+
private string EscapeSqlString(string value)
183+
{
184+
return "`" + value.Replace("`", "``") + "`";
185+
}
186+
167187
protected override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetSchemasResp response, CancellationToken cancellationToken = default) =>
168188
Task.FromResult(response.DirectResults.ResultSetMetadata);
169189
protected override Task<TGetResultSetMetadataResp> GetResultSetMetadataAsync(TGetCatalogsResp response, CancellationToken cancellationToken = default) =>

0 commit comments

Comments
 (0)