-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add patching for older rails versions
- Loading branch information
Showing
5 changed files
with
81 additions
and
124 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
lib/datadog/appsec/contrib/active_record/instrumentation.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
module Datadog | ||
module AppSec | ||
module Contrib | ||
module ActiveRecord | ||
# AppSec module that will be prepended to ActiveRecord adapter | ||
module Instrumentation | ||
module_function | ||
|
||
def detect_sqli(sql, adapter_name) | ||
scope = AppSec.active_scope | ||
return unless scope | ||
|
||
ephemeral_data = { | ||
'server.db.statement' => sql, | ||
'server.db.system' => adapter_name.downcase.gsub(/\d{1}\z/, '') | ||
} | ||
|
||
waf_timeout = Datadog.configuration.appsec.waf_timeout | ||
result = scope.processor_context.run({}, ephemeral_data, waf_timeout) | ||
|
||
if result.status == :match | ||
Datadog::AppSec::Event.tag_and_keep!(scope, result) | ||
|
||
event = { | ||
waf_result: result, | ||
trace: scope.trace, | ||
span: scope.service_entry_span, | ||
sql: sql, | ||
actions: result.actions | ||
} | ||
scope.processor_context.events << event | ||
end | ||
end | ||
|
||
# patch for all adapters in ActiveRecord >= 7.1 | ||
module InternalExecQueryAdapterPatch | ||
def internal_exec_query(sql, *args, **rest) | ||
Instrumentation.detect_sqli(sql, adapter_name) | ||
|
||
super | ||
end | ||
end | ||
|
||
# patch for postgres adapter in ActiveRecord < 7.1 | ||
module ExecuteAndClearAdapterPatch | ||
def execute_and_clear(sql, *args, **rest) | ||
Instrumentation.detect_sqli(sql, adapter_name) | ||
|
||
super | ||
end | ||
end | ||
|
||
# patch for mysql2 and sqlite3 adapters in ActiveRecord < 7.1 | ||
module ExecQueryAdapterPatch | ||
def exec_query(sql, *args, **rest) | ||
Instrumentation.detect_sqli(sql, adapter_name) | ||
|
||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
39 changes: 0 additions & 39 deletions
39
lib/datadog/appsec/contrib/active_record/mysql2_adapter_patch.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
lib/datadog/appsec/contrib/active_record/postgresql_adapter_patch.rb
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
lib/datadog/appsec/contrib/active_record/sqlite3_adapter_patch.rb
This file was deleted.
Oops, something went wrong.