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

Add ActiveRecord instrumentation to detect SQLi in AppSec #4167

Merged
merged 9 commits into from
Dec 2, 2024

Conversation

y9v
Copy link
Member

@y9v y9v commented Nov 27, 2024

What does this PR do?
This PR adds ActiveRecord instrumentation to AppSec for detecting SQL injections.

Motivation:
We want to enable SQL injection detection for users using AppSec. Libddwaf already supports this, so this is just about providing required data to libddwaf.

Change log entry
Add SQL injection detection for ActiveRecord for following adapters: mysql2, postgresql, and sqlite3

Additional Notes:
This is the initial implementation - without blocking, and without passing the SQL span ID with the appsec event.
Passing the span ID of the SQL span is currently not possible without major changes, since tracing creates the SQL span at a later time point than we do our instrumentation.

How to test the change?
This feature can be tested using https://github.com/DataDog/appsec-app-generator (activerecord-sqli variant):

bin/run activerecord-sqli -R 7.2.0 -d postgresql

@y9v y9v self-assigned this Nov 27, 2024
@y9v y9v requested review from a team as code owners November 27, 2024 10:40
@github-actions github-actions bot added integrations Involves tracing integrations appsec Application Security monitoring product labels Nov 27, 2024
@codecov-commenter
Copy link

codecov-commenter commented Nov 27, 2024

Codecov Report

Attention: Patch coverage is 95.85253% with 9 lines in your changes missing coverage. Please review.

Project coverage is 97.74%. Comparing base (28675b6) to head (971f329).

Files with missing lines Patch % Lines
...og/appsec/contrib/active_record/instrumentation.rb 93.75% 2 Missing ⚠️
...ib/datadog/appsec/contrib/active_record/patcher.rb 92.30% 2 Missing ⚠️
...c/contrib/active_record/postgresql_adapter_spec.rb 95.83% 2 Missing ⚠️
...atadog/appsec/contrib/active_record/integration.rb 95.00% 1 Missing ⚠️
...ppsec/contrib/active_record/mysql2_adapter_spec.rb 97.77% 1 Missing ⚠️
...psec/contrib/active_record/sqlite3_adapter_spec.rb 97.77% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4167      +/-   ##
==========================================
- Coverage   97.76%   97.74%   -0.02%     
==========================================
  Files        1351     1357       +6     
  Lines       81733    81950     +217     
  Branches     4147     4168      +21     
==========================================
+ Hits        79904    80106     +202     
- Misses       1829     1844      +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pr-commenter
Copy link

pr-commenter bot commented Nov 27, 2024

Benchmarks

Benchmark execution time: 2024-12-02 11:12:20

Comparing candidate commit 971f329 in PR branch appsec-add-sqli with baseline commit 28675b6 in branch master.

Found 1 performance improvements and 1 performance regressions! Performance is the same for 29 metrics, 2 unstable metrics.

scenario:profiler - stack collector

  • 🟩 throughput [+182.526op/s; +184.412op/s] or [+6.563%; +6.631%]

scenario:tracing - Propagation - Datadog

  • 🟥 throughput [-3161.325op/s; -3090.602op/s] or [-9.438%; -9.227%]

@y9v y9v requested a review from anmarchenko November 27, 2024 15:28
Copy link
Member

@Strech Strech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔎 Woah, lots of the digging into internals, congrats on our first RAS(P).

I think we can slightly simplify few bits and pieces on a way.

lib/datadog/appsec/contrib/active_record/integration.rb Outdated Show resolved Hide resolved
lib/datadog/appsec/contrib/active_record/patcher.rb Outdated Show resolved Hide resolved
lib/datadog/appsec/contrib/active_record/patcher.rb Outdated Show resolved Hide resolved
spec/datadog/appsec/contrib/active_record/patcher_spec.rb Outdated Show resolved Hide resolved
spec/datadog/appsec/contrib/active_record/patcher_spec.rb Outdated Show resolved Hide resolved
@datadog-datadog-prod-us1
Copy link
Contributor

datadog-datadog-prod-us1 bot commented Nov 29, 2024

Datadog Report

Branch report: appsec-add-sqli
Commit report: 971f329
Test service: dd-trace-rb

✅ 0 Failed, 22036 Passed, 1459 Skipped, 6m 0.31s Total Time

@y9v y9v requested a review from Strech November 29, 2024 14:41
Copy link
Member

@Strech Strech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍🏼

lib/datadog/appsec.rb Outdated Show resolved Hide resolved
@y9v y9v force-pushed the appsec-add-sqli branch from 027cbaf to ee4c3a2 Compare December 2, 2024 10:29
@y9v y9v merged commit ca7cc9d into master Dec 2, 2024
318 checks passed
@y9v y9v deleted the appsec-add-sqli branch December 2, 2024 13:01
@github-actions github-actions bot added this to the 2.8.0 milestone Dec 2, 2024
@ivoanjo ivoanjo mentioned this pull request Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
appsec Application Security monitoring product integrations Involves tracing integrations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants