From 71b44207b43d921639990a25d6089e2f8c320217 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 28 Aug 2024 12:51:40 +0200 Subject: [PATCH 1/2] Protect the connection string tags extractor from an invalid connection string --- .../src/Datadog.Trace/Util/DbCommandCache.cs | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/tracer/src/Datadog.Trace/Util/DbCommandCache.cs b/tracer/src/Datadog.Trace/Util/DbCommandCache.cs index 6968867e7103..513372d559ab 100644 --- a/tracer/src/Datadog.Trace/Util/DbCommandCache.cs +++ b/tracer/src/Datadog.Trace/Util/DbCommandCache.cs @@ -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 (as seen in Error Tracking) + // 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) From 76f8db90ae0cd5a20afc5265da80ef8341ce8713 Mon Sep 17 00:00:00 2001 From: Tony Redondo Date: Wed, 28 Aug 2024 13:58:20 +0200 Subject: [PATCH 2/2] Update tracer/src/Datadog.Trace/Util/DbCommandCache.cs Co-authored-by: Andrew Lock --- tracer/src/Datadog.Trace/Util/DbCommandCache.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracer/src/Datadog.Trace/Util/DbCommandCache.cs b/tracer/src/Datadog.Trace/Util/DbCommandCache.cs index 513372d559ab..86534b7c231f 100644 --- a/tracer/src/Datadog.Trace/Util/DbCommandCache.cs +++ b/tracer/src/Datadog.Trace/Util/DbCommandCache.cs @@ -110,7 +110,7 @@ private static TagsCacheItem ExtractTagsFromConnectionString(string connectionSt } catch (Exception) { - // DbConnectionStringBuilder can throw exceptions if the connection string is invalid (as seen in Error Tracking) + // 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; }