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

Don't sanitize postgre parameter markers #11388

Merged
merged 3 commits into from
May 17, 2024
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
29 changes: 15 additions & 14 deletions instrumentation-api-incubator/src/main/jflex/SqlSanitizer.jflex
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ import java.util.regex.Pattern;
%unicode
%ignorecase

COMMA = ","
OPEN_PAREN = "("
CLOSE_PAREN = ")"
OPEN_COMMENT = "/*"
CLOSE_COMMENT = "*/"
IDENTIFIER = ([:letter:] | "_") ([:letter:] | [0-9] | [_.])*
BASIC_NUM = [.+-]* [0-9] ([0-9] | [eE.+-])*
HEX_NUM = "0x" ([a-f] | [A-F] | [0-9])+
QUOTED_STR = "'" ("''" | [^'])* "'"
DOUBLE_QUOTED_STR = "\"" ("\"\"" | [^\"])* "\""
DOLLAR_QUOTED_STR = "$$" [^$]* "$$"
BACKTICK_QUOTED_STR = "`" [^`]* "`"
WHITESPACE = [ \t\r\n]+
COMMA = ","
OPEN_PAREN = "("
CLOSE_PAREN = ")"
OPEN_COMMENT = "/*"
CLOSE_COMMENT = "*/"
IDENTIFIER = ([:letter:] | "_") ([:letter:] | [0-9] | [_.])*
BASIC_NUM = [.+-]* [0-9] ([0-9] | [eE.+-])*
HEX_NUM = "0x" ([a-f] | [A-F] | [0-9])+
QUOTED_STR = "'" ("''" | [^'])* "'"
DOUBLE_QUOTED_STR = "\"" ("\"\"" | [^\"])* "\""
DOLLAR_QUOTED_STR = "$$" [^$]* "$$"
BACKTICK_QUOTED_STR = "`" [^`]* "`"
POSTGRE_PARAM_MARKER = "$"[0-9]*
WHITESPACE = [ \t\r\n]+

%{
static SqlStatementInfo sanitize(String statement, SqlDialect dialect) {
Expand Down Expand Up @@ -520,7 +521,7 @@ WHITESPACE = [ \t\r\n]+
if (isOverLimit()) return YYEOF;
}

{BACKTICK_QUOTED_STR} {
{BACKTICK_QUOTED_STR} | {POSTGRE_PARAM_MARKER} {
if (!insideComment && !extractionDone) {
extractionDone = operation.handleIdentifier();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void longInStatementDoesntCauseStackOverflow() {
static class SqlArgs implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("SELECT * FROM TABLE WHERE FIELD=1234", "SELECT * FROM TABLE WHERE FIELD=?"),
Arguments.of(
Expand Down Expand Up @@ -214,6 +214,10 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) th
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $$\\\\$$", "SELECT * FROM TABLE WHERE FIELD = ?"),

// PostgreSQL native parameter marker, we want to keep $1 instead of replacing it with ?
Arguments.of(
"SELECT * FROM TABLE WHERE FIELD = $1", "SELECT * FROM TABLE WHERE FIELD = $1"),

// Unicode, including a unicode identifier with a trailing number
Arguments.of(
"SELECT * FROM TABLEओ7 WHERE FIELD = 'ɣ'", "SELECT * FROM TABLEओ7 WHERE FIELD = ?"),
Expand All @@ -231,7 +235,7 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) th
static class CouchbaseArgs implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
// Some databases support/encourage " instead of ' with same escape rules
Arguments.of(
Expand Down Expand Up @@ -268,7 +272,7 @@ static Function<String, SqlStatementInfo> expect(
}

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
// Select
Arguments.of("SELECT x, y, z FROM schema.table", expect("SELECT", "schema.table")),
Expand Down Expand Up @@ -381,7 +385,7 @@ static Function<String, SqlStatementInfo> expect(
}

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(
Arguments.of("CREATE TABLE `table`", expect("CREATE TABLE", "table")),
Arguments.of("CREATE TABLE IF NOT EXISTS table", expect("CREATE TABLE", "table")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ private static void assertTrace() {
equalTo(DB_USER, USER_DB),
equalTo(
DB_STATEMENT,
"select value0_.id as id1_0_0_, value0_.name as name2_0_0_ from Value value0_ where value0_.id=$?"),
"select value0_.id as id1_0_0_, value0_.name as name2_0_0_ from Value value0_ where value0_.id=$1"),
equalTo(DB_OPERATION, "SELECT"),
equalTo(DB_SQL_TABLE, "Value"),
equalTo(SERVER_ADDRESS, host),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private static void assertTrace() {
equalTo(DB_USER, USER_DB),
equalTo(
DB_STATEMENT,
"select v1_0.id,v1_0.name from Value v1_0 where v1_0.id=$?"),
"select v1_0.id,v1_0.name from Value v1_0 where v1_0.id=$1"),
equalTo(DB_OPERATION, "SELECT"),
equalTo(DB_SQL_TABLE, "Value"),
equalTo(SERVER_ADDRESS, host),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ private static void assertPreparedSelect() {
.hasAttributesSatisfyingExactly(
equalTo(DB_NAME, DB),
equalTo(DB_USER, USER_DB),
equalTo(DB_STATEMENT, "select * from test where id = $?"),
equalTo(DB_STATEMENT, "select * from test where id = $1"),
equalTo(DB_OPERATION, "SELECT"),
equalTo(DB_SQL_TABLE, "test"),
equalTo(SERVER_ADDRESS, host),
Expand Down Expand Up @@ -259,7 +259,7 @@ void testBatch() throws Exception {
.hasAttributesSatisfyingExactly(
equalTo(DB_NAME, DB),
equalTo(DB_USER, USER_DB),
equalTo(DB_STATEMENT, "insert into test values ($?, $?) returning *"),
equalTo(DB_STATEMENT, "insert into test values ($1, $2) returning *"),
equalTo(DB_OPERATION, "INSERT"),
equalTo(DB_SQL_TABLE, "test"),
equalTo(SERVER_ADDRESS, host),
Expand Down Expand Up @@ -410,7 +410,7 @@ void testConcurrency() throws Exception {
.hasAttributesSatisfyingExactly(
equalTo(DB_NAME, DB),
equalTo(DB_USER, USER_DB),
equalTo(DB_STATEMENT, "select * from test where id = $?"),
equalTo(DB_STATEMENT, "select * from test where id = $1"),
equalTo(DB_OPERATION, "SELECT"),
equalTo(DB_SQL_TABLE, "test"),
equalTo(SERVER_ADDRESS, host),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void webClientAndWebFluxAndR2dbc() {
a ->
assertThat(a.get(DbIncubatingAttributes.DB_STATEMENT))
.isEqualToIgnoringCase(
"SELECT PLAYER.* FROM PLAYER WHERE PLAYER.ID = $? LIMIT ?"))
"SELECT PLAYER.* FROM PLAYER WHERE PLAYER.ID = $1 LIMIT ?"))
.hasAttribute(DbIncubatingAttributes.DB_SYSTEM, "h2")));
}
}
Loading