Skip to content

Commit 82cb155

Browse files
committed
Fix ActiveRecord adapter patching for Postgres with JDBC
1 parent 320f1f1 commit 82cb155

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

lib/datadog/appsec/contrib/active_record/instrumentation.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def detect_sql_injection(sql, adapter_name)
4343
end
4444
end
4545

46-
# patch for all adapters in ActiveRecord >= 7.1
46+
# patch for mysql2, sqlite3, and postgres+jdbc adapters in ActiveRecord >= 7.1
4747
module InternalExecQueryAdapterPatch
4848
def internal_exec_query(sql, *args, **rest)
4949
Instrumentation.detect_sql_injection(sql, adapter_name)
@@ -52,37 +52,36 @@ def internal_exec_query(sql, *args, **rest)
5252
end
5353
end
5454

55-
# patch for postgres adapter in ActiveRecord < 7.1
56-
module ExecuteAndClearAdapterPatch
57-
def execute_and_clear(sql, *args, **rest)
55+
# patch for mysql2, sqlite3, and postgres+jdbc adapters in ActiveRecord < 7.1
56+
module ExecQueryAdapterPatch
57+
def exec_query(sql, *args, **rest)
5858
Instrumentation.detect_sql_injection(sql, adapter_name)
5959

6060
super
6161
end
6262
end
6363

64-
# patch for postgres adapter in ActiveRecord 4
65-
module Rails4ExecuteAndClearAdapterPatch
66-
def execute_and_clear(sql, name, binds)
64+
# patch for mysql2, sqlite3, and postgres+jdbc db adapters in ActiveRecord 4
65+
module Rails4ExecQueryAdapterPatch
66+
def exec_query(sql, *args)
6767
Instrumentation.detect_sql_injection(sql, adapter_name)
6868

6969
super
7070
end
7171
end
7272

73-
# patch for mysql2 and sqlite3 adapters in ActiveRecord < 7.1
74-
# also used for postgres adapter in ActiveRecord >= 7.1 when used together with JDBC adapter
75-
module ExecQueryAdapterPatch
76-
def exec_query(sql, *args, **rest)
73+
# patch for non-jdbc postgres adapter in ActiveRecord > 4
74+
module ExecuteAndClearAdapterPatch
75+
def execute_and_clear(sql, *args, **rest)
7776
Instrumentation.detect_sql_injection(sql, adapter_name)
7877

7978
super
8079
end
8180
end
8281

83-
# patch for mysql2 and sqlite3 db adapters in ActiveRecord 4
84-
module Rails4ExecQueryAdapterPatch
85-
def exec_query(sql, *args)
82+
# patch for non-jdbc postgres adapter in ActiveRecord 4
83+
module Rails4ExecuteAndClearAdapterPatch
84+
def execute_and_clear(sql, name, binds)
8685
Instrumentation.detect_sql_injection(sql, adapter_name)
8786

8887
super

lib/datadog/appsec/contrib/active_record/patcher.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,22 @@ def patch_mysql2_adapter
7676
end
7777

7878
def patch_postgresql_adapter
79-
jdbc_defined = defined?(::ActiveRecord::ConnectionAdapters::JdbcAdapter)
80-
81-
instrumentation_module = if jdbc_defined && ::ActiveRecord.gem_version >= Gem::Version.new('7.1')
82-
Instrumentation::InternalExecQueryAdapterPatch
83-
elsif jdbc_defined && ::ActiveRecord.gem_version.segments.first == 4
79+
instrumentation_module = if ::ActiveRecord.gem_version.segments.first == 4
8480
Instrumentation::Rails4ExecuteAndClearAdapterPatch
8581
else
8682
Instrumentation::ExecuteAndClearAdapterPatch
8783
end
8884

85+
if defined?(::ActiveRecord::ConnectionAdapters::JdbcAdapter)
86+
instrumentation_module = if ::ActiveRecord.gem_version >= Gem::Version.new('7.1')
87+
Instrumentation::InternalExecQueryAdapterPatch
88+
elsif ::ActiveRecord.gem_version.segments.first == 4
89+
Instrumentation::Rails4ExecQueryAdapterPatch
90+
else
91+
Instrumentation::ExecQueryAdapterPatch
92+
end
93+
end
94+
8995
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(instrumentation_module)
9096
end
9197
end

0 commit comments

Comments
 (0)