|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'datadog/appsec/spec_helper' |
| 4 | +require 'active_record' |
| 5 | + |
| 6 | +require 'spec/datadog/tracing/contrib/rails/support/deprecation' |
| 7 | + |
| 8 | +if PlatformHelpers.jruby? |
| 9 | + require 'activerecord-jdbc-adapter' |
| 10 | +else |
| 11 | + require 'mysql2' |
| 12 | +end |
| 13 | + |
| 14 | +RSpec.describe 'AppSec ActiveRecord integration for Mysql2 adapter' do |
| 15 | + let(:telemetry) { instance_double(Datadog::Core::Telemetry::Component) } |
| 16 | + let(:ruleset) { Datadog::AppSec::Processor::RuleLoader.load_rules(ruleset: :recommended, telemetry: telemetry) } |
| 17 | + let(:processor) { Datadog::AppSec::Processor.new(ruleset: ruleset, telemetry: telemetry) } |
| 18 | + let(:context) { processor.new_context } |
| 19 | + |
| 20 | + let(:span) { Datadog::Tracing::SpanOperation.new('root') } |
| 21 | + let(:trace) { Datadog::Tracing::TraceOperation.new } |
| 22 | + |
| 23 | + let!(:user_class) do |
| 24 | + stub_const('User', Class.new(ActiveRecord::Base)).tap do |klass| |
| 25 | + klass.establish_connection(db_config) |
| 26 | + |
| 27 | + klass.connection.create_table 'users', force: :cascade do |t| |
| 28 | + t.string :name, null: false |
| 29 | + t.string :email, null: false |
| 30 | + t.timestamps |
| 31 | + end |
| 32 | + |
| 33 | + # prevent internal sql requests from showing up |
| 34 | + klass.count |
| 35 | + klass.first |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + let(:db_config) do |
| 40 | + { |
| 41 | + adapter: 'mysql2', |
| 42 | + database: ENV.fetch('TEST_MYSQL_DB', 'mysql'), |
| 43 | + host: ENV.fetch('TEST_MYSQL_HOST', '127.0.0.1'), |
| 44 | + password: ENV.fetch('TEST_MYSQL_ROOT_PASSWORD', 'root'), |
| 45 | + port: ENV.fetch('TEST_MYSQL_PORT', '3306') |
| 46 | + } |
| 47 | + end |
| 48 | + |
| 49 | + before do |
| 50 | + Datadog.configure do |c| |
| 51 | + c.appsec.enabled = true |
| 52 | + c.appsec.instrument :active_record |
| 53 | + end |
| 54 | + |
| 55 | + Datadog::AppSec::Scope.activate_scope(trace, span, processor) |
| 56 | + |
| 57 | + raise_on_rails_deprecation! |
| 58 | + end |
| 59 | + |
| 60 | + after do |
| 61 | + Datadog.configuration.reset! |
| 62 | + |
| 63 | + Datadog::AppSec::Scope.deactivate_scope |
| 64 | + processor.finalize |
| 65 | + end |
| 66 | + |
| 67 | + it 'calls waf with correct arguments when querying using .where' do |
| 68 | + expect(Datadog::AppSec.active_scope.processor_context).to( |
| 69 | + receive(:run).with( |
| 70 | + {}, |
| 71 | + { |
| 72 | + 'server.db.statement' => "SELECT `users`.* FROM `users` WHERE `users`.`name` = 'Bob'", |
| 73 | + 'server.db.system' => 'mysql2' |
| 74 | + }, |
| 75 | + Datadog.configuration.appsec.waf_timeout |
| 76 | + ).and_call_original |
| 77 | + ) |
| 78 | + |
| 79 | + User.where(name: 'Bob').to_a |
| 80 | + end |
| 81 | + |
| 82 | + it 'calls waf with correct arguments when querying using .find_by_sql' do |
| 83 | + expect(Datadog::AppSec.active_scope.processor_context).to( |
| 84 | + receive(:run).with( |
| 85 | + {}, |
| 86 | + { |
| 87 | + 'server.db.statement' => "SELECT * FROM users WHERE name = 'Bob'", |
| 88 | + 'server.db.system' => 'mysql2' |
| 89 | + }, |
| 90 | + Datadog.configuration.appsec.waf_timeout |
| 91 | + ).and_call_original |
| 92 | + ) |
| 93 | + |
| 94 | + User.find_by_sql("SELECT * FROM users WHERE name = 'Bob'").to_a |
| 95 | + end |
| 96 | + |
| 97 | + it 'adds an event to processor context if waf status is :match' do |
| 98 | + expect(Datadog::AppSec.active_scope.processor_context).to( |
| 99 | + receive(:run).and_return(instance_double(Datadog::AppSec::WAF::Result, status: :match, actions: {})) |
| 100 | + ) |
| 101 | + |
| 102 | + expect(Datadog::AppSec.active_scope.processor_context.events).to receive(:<<).and_call_original |
| 103 | + |
| 104 | + User.where(name: 'Bob').to_a |
| 105 | + end |
| 106 | +end |
0 commit comments