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

Protect the connection string tags extractor from an invalid connection string (#5956 -> v2) #5960

Merged
merged 1 commit into from
Aug 29, 2024
Merged
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: 17 additions & 7 deletions tracer/src/Datadog.Trace/Util/DbCommandCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,23 @@ public static TagsCacheItem GetTagsFromDbCommand(IDbCommand command)

private static TagsCacheItem ExtractTagsFromConnectionString(string connectionString)
{
// Parse the connection string
var builder = new DbConnectionStringBuilder { ConnectionString = connectionString };

return new TagsCacheItem(
dbName: GetConnectionStringValue(builder, "Database", "Initial Catalog", "InitialCatalog"),
dbUser: GetConnectionStringValue(builder, "User ID", "UserID"),
outHost: GetConnectionStringValue(builder, "Server", "Data Source", "DataSource", "Network Address", "NetworkAddress", "Address", "Addr", "Host"));
try
{
// Parse the connection string
var builder = new DbConnectionStringBuilder { ConnectionString = connectionString };

// Extract the tags
return new TagsCacheItem(
dbName: GetConnectionStringValue(builder, "Database", "Initial Catalog", "InitialCatalog"),
dbUser: GetConnectionStringValue(builder, "User ID", "UserID"),
outHost: GetConnectionStringValue(builder, "Server", "Data Source", "DataSource", "Network Address", "NetworkAddress", "Address", "Addr", "Host"));
}
catch (Exception)
{
// DbConnectionStringBuilder can throw exceptions if the connection string is invalid
// in this case we should not use the connection string and just return default
return default;
}
}

private static string? GetConnectionStringValue(DbConnectionStringBuilder builder, params string[] names)
Expand Down
Loading