-
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 ActiveRecord instrumentation to detect SQLi in AppSec
- Loading branch information
Showing
7 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
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
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
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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../integration' | ||
require_relative 'patcher' | ||
|
||
module Datadog | ||
module AppSec | ||
module Contrib | ||
module ActiveRecord | ||
# Description of ActiveRecord integration | ||
class Integration | ||
include Datadog::AppSec::Contrib::Integration | ||
|
||
MINIMUM_VERSION = Gem::Version.new('4') | ||
|
||
register_as :active_record, auto_patch: false | ||
|
||
def self.version | ||
Gem.loaded_specs['activerecord'] && Gem.loaded_specs['activerecord'].version | ||
end | ||
|
||
def self.loaded? | ||
!defined?(::ActiveRecord).nil? | ||
end | ||
|
||
def self.compatible? | ||
super && version >= MINIMUM_VERSION | ||
end | ||
|
||
def self.auto_instrument? | ||
true | ||
end | ||
|
||
def patcher | ||
Patcher | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
39 changes: 39 additions & 0 deletions
39
lib/datadog/appsec/contrib/active_record/mysql2_adapter_patch.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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module Datadog | ||
module AppSec | ||
module Contrib | ||
module ActiveRecord | ||
# AppSec module that will be prepended to ActiveRecord adapter | ||
module Mysql2AdapterPatch | ||
def internal_exec_query(sql, *args, **rest) | ||
scope = AppSec.active_scope | ||
return super unless scope | ||
|
||
ephemeral_data = { | ||
'server.db.statement' => sql, | ||
'server.db.system' => 'mysql' | ||
} | ||
|
||
result = scope.processor_context.run({}, ephemeral_data, Datadog.configuration.appsec.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 | ||
|
||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../patcher' | ||
require_relative 'sqlite3_adapter_patch' | ||
require_relative 'postgresql_adapter_patch' | ||
require_relative 'mysql2_adapter_patch' | ||
|
||
module Datadog | ||
module AppSec | ||
module Contrib | ||
module ActiveRecord | ||
# AppSec patcher module for ActiveRecord | ||
module Patcher | ||
include Datadog::AppSec::Contrib::Patcher | ||
|
||
module_function | ||
|
||
def patched? | ||
Patcher.instance_variable_get(:@patched) | ||
end | ||
|
||
def target_version | ||
Integration.version | ||
end | ||
|
||
def patch | ||
ActiveSupport.on_load :active_record do | ||
if defined? ::ActiveRecord::ConnectionAdapters::SQLite3Adapter | ||
::ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend(SQLite3AdapterPatch) | ||
end | ||
|
||
if defined? ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter | ||
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(PostgreSQLAdapterPatch) | ||
end | ||
|
||
if defined? ::ActiveRecord::ConnectionAdapters::Mysql2Adapter | ||
::ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend(Mysql2AdapterPatch) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
39 changes: 39 additions & 0 deletions
39
lib/datadog/appsec/contrib/active_record/postgresql_adapter_patch.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,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module Datadog | ||
module AppSec | ||
module Contrib | ||
module ActiveRecord | ||
# AppSec module that will be prepended to ActiveRecord adapter | ||
module PostgreSQLAdapterPatch | ||
def internal_exec_query(sql, *args, **rest) | ||
scope = AppSec.active_scope | ||
return super unless scope | ||
|
||
ephemeral_data = { | ||
'server.db.statement' => sql, | ||
'server.db.system' => 'postgresql' | ||
} | ||
|
||
result = scope.processor_context.run({}, ephemeral_data, Datadog.configuration.appsec.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, | ||
actions: result.actions, | ||
sql: sql | ||
} | ||
scope.processor_context.events << event | ||
end | ||
|
||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
40 changes: 40 additions & 0 deletions
40
lib/datadog/appsec/contrib/active_record/sqlite3_adapter_patch.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,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module Datadog | ||
module AppSec | ||
module Contrib | ||
module ActiveRecord | ||
# AppSec module that will be prepended to ActiveRecord adapter | ||
module SQLite3AdapterPatch | ||
def internal_exec_query(sql, *args, **rest) | ||
scope = AppSec.active_scope | ||
return super unless scope | ||
|
||
ephemeral_data = { | ||
'server.db.statement' => sql, | ||
'server.db.system' => 'sqlite' | ||
} | ||
|
||
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 | ||
|
||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |