Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
cases for escaped identifiers. Optimize performance of parsing logic.
([#3627](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3627))

* Improve SQL parsing for sanitization for Unicode string literals.
([#3662](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3662))

* Sanitize the object name for SQL query text using the LOGIN or USER keywords and
remove from query summaries.
([#3663](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3663))
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
cases for escaped identifiers. Optimize performance of parsing logic.
([#3627](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3627))

* Improve SQL parsing for sanitization for Unicode string literals.
([#3662](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3662))

* Sanitize the object name for SQL query text using the LOGIN or USER keywords and
remove from query summaries.
([#3663](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/3663))
Expand Down
11 changes: 11 additions & 0 deletions src/Shared/SqlProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal static class SqlProcessor
private const char NewLineChar = '\n';
private const char CarriageReturnChar = '\r';
private const char TabChar = '\t';
private const char UnicodePrefixChar = 'N';

private static readonly ConcurrentDictionary<string, SqlStatementInfo> Cache = new();

Expand Down Expand Up @@ -672,6 +673,10 @@ private static bool SanitizeStringLiteral(ReadOnlySpan<char> sql, Span<char> buf
return true;
}

// Is the string literal of the form `N'foo'` (i.e. a Unicode literal)?
// If so, we want to skip the Unicode prefix when sanitizing.
bool isUnicode = state.ParsePosition >= 1 && sql[state.ParsePosition - 1] is UnicodePrefixChar;
Comment thread
martincostello marked this conversation as resolved.

// Use index arithmetic instead of slicing
var searchPos = state.ParsePosition + 1;
while (searchPos < sql.Length)
Expand All @@ -686,6 +691,12 @@ private static bool SanitizeStringLiteral(ReadOnlySpan<char> sql, Span<char> buf
}

// Found terminating quote
if (isUnicode)
{
// Skip the Unicode prefix by overwriting the previous position instead
state.SanitizedPosition--;
}

state.ParsePosition = searchPos + 1;
buffer[state.SanitizedPosition++] = SanitizationPlaceholder;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@
},
"expected": {
"db.query.text": [
"DECLARE @__geräteIds_0 nvarchar(?) = N?;\n\nSELECT [t].[Id]\nFROM [Tests] AS [t]\nWHERE [t].[Id] IN (\n\tSELECT [g].[value]\n\tFROM OPENJSON(@__geräteIds_0) WITH ([value] bigint ?) AS [g]\n)"
"DECLARE @__geräteIds_0 nvarchar(?) = ?;\n\nSELECT [t].[Id]\nFROM [Tests] AS [t]\nWHERE [t].[Id] IN (\n\tSELECT [g].[value]\n\tFROM OPENJSON(@__geräteIds_0) WITH ([value] bigint ?) AS [g]\n)"
],
"db.query.summary": "SELECT [Tests]"
}
Expand Down Expand Up @@ -1052,5 +1052,18 @@
],
"db.query.summary": "SELECT [Order-Details] [Customer.Info]"
}
},
{
"name": "unicode_string",
"input": {
"db.system.name": "other_sql",
"query": "SELECT * FROM USERS WHERE username = N'john.doe'"
},
"expected": {
"db.query.text": [
"SELECT * FROM USERS WHERE username = ?"
],
"db.query.summary": "SELECT USERS"
}
}
Comment thread
martincostello marked this conversation as resolved.
]
Loading