Skip to content

Commit c52df11

Browse files
committed
preserve capitalization and white space when normalizing IN statement
1 parent 0c7fd10 commit c52df11

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

instrumentation-api-incubator/src/main/jflex/SqlSanitizer.jflex

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ WHITESPACE = [ \t\r\n]+
5454
// max length of the sanitized statement - SQLs longer than this will be trimmed
5555
static final int LIMIT = 32 * 1024;
5656

57-
// Match on "IN(?, ?, ...)"
58-
private static final Pattern IN_STATEMENT_PATTERN = Pattern.compile("(\\sin\\s*)\\(\\s*\\?\\s*(,\\s*\\?\\s*)*+\\)", Pattern.CASE_INSENSITIVE);
59-
private static final String IN_STATEMENT_NORMALIZED = " in(?)";
57+
// Match on strings like "IN(?, ?, ...)"
58+
private static final Pattern IN_STATEMENT_PATTERN = Pattern.compile("(\\sIN\\s*)\\(\\s*\\?\\s*(?:,\\s*\\?\\s*)*+\\)", Pattern.CASE_INSENSITIVE);
59+
private static final String IN_STATEMENT_NORMALIZED = "$1(?)";
6060

6161
private final StringBuilder builder = new StringBuilder();
6262

instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlStatementSanitizerTest.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void longInStatementDoesntCauseStackOverflow() {
123123

124124
String sanitized = SqlStatementSanitizer.create(true).sanitize(s.toString()).getFullStatement();
125125

126-
assertThat(sanitized).isEqualTo("select col from table where col in(?)");
126+
assertThat(sanitized).isEqualTo("select col from table where col in (?)");
127127
}
128128

129129
static class SqlArgs implements ArgumentsProvider {
@@ -284,11 +284,11 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) th
284284
Arguments.of("select col from table1 as t1, table2 as t2", expect("SELECT", null)),
285285
Arguments.of(
286286
"select col from table where col in (1, 2, 3)",
287-
expect("select col from table where col in(?)", "SELECT", "table")),
287+
expect("select col from table where col in (?)", "SELECT", "table")),
288288
Arguments.of(
289-
"select 'a' IN(x, 'b') from table where col in(1) and z IN( '3', '4' )",
289+
"select 'a' IN(x, 'b') from table where col in (1) and z IN( '3', '4' )",
290290
expect(
291-
"select ? IN(x, ?) from table where col in(?) and z in(?)", "SELECT", "table")),
291+
"select ? IN(x, ?) from table where col in (?) and z IN(?)", "SELECT", "table")),
292292
Arguments.of("select col from table order by col, col2", expect("SELECT", "table")),
293293
Arguments.of("select ąś∂ń© from źćļńĶ order by col, col2", expect("SELECT", "źćļńĶ")),
294294
Arguments.of("select 12345678", expect("select ?", "SELECT", null)),
@@ -316,8 +316,8 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) th
316316
Arguments.of(
317317
"delete from \"my table\" where something something", expect("DELETE", "my table")),
318318
Arguments.of(
319-
"delete from foo where x IN(1, 2, 3)",
320-
expect("delete from foo where x in(?)", "DELETE", "foo")),
319+
"delete from foo where x IN (1,2,3)",
320+
expect("delete from foo where x IN (?)", "DELETE", "foo")),
321321
Arguments.of("delete from 12345678", expect("delete from ?", "DELETE", null)),
322322
Arguments.of("delete (((", expect("delete (((", "DELETE", null)),
323323

@@ -328,8 +328,11 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) th
328328
"update `my table` set answer=42",
329329
expect("update `my table` set answer=?", "UPDATE", "my table")),
330330
Arguments.of(
331-
"update `my table` set answer=42 where x IN('a', 'b')",
332-
expect("update `my table` set answer=? where x in(?)", "UPDATE", "my table")),
331+
"update `my table` set answer=42 where x IN('a', 'b') AND y In ('a', 'b')",
332+
expect(
333+
"update `my table` set answer=? where x IN(?) AND y In (?)",
334+
"UPDATE",
335+
"my table")),
333336
Arguments.of(
334337
"update \"my table\" set answer=42",
335338
expect("update \"my table\" set answer=?", "UPDATE", "my table")),

0 commit comments

Comments
 (0)