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

Blocking from a hook is not stopping code execution #2836

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

estringana
Copy link
Contributor

Description

Blocking a request from Appsec should stop customer code execution. However, when this blocking happens within a tracer hook, it does not stop executing customer code execution.

Reviewer checklist

  • Test coverage seems ok.
  • Appropriate labels assigned.

@estringana estringana changed the title Replicate issue on a PHPT tests Blocking from a hook is not stopping code execution Sep 5, 2024
@codecov-commenter
Copy link

codecov-commenter commented Sep 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 51.21%. Comparing base (4cc2897) to head (7fd4a50).
Report is 2 commits behind head on master.

❗ There is a different number of reports uploaded between BASE (4cc2897) and HEAD (7fd4a50). Click for more details.

HEAD has 4 uploads less than BASE
Flag BASE (4cc2897) HEAD (7fd4a50)
tracer-php 11 8
appsec-extension 1 0
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##             master    #2836       +/-   ##
=============================================
- Coverage     72.75%   51.21%   -21.54%     
+ Complexity     2750     2745        -5     
=============================================
  Files           138      111       -27     
  Lines         15038    10895     -4143     
  Branches       1020        0     -1020     
=============================================
- Hits          10941     5580     -5361     
- Misses         3543     5315     +1772     
+ Partials        554        0      -554     
Flag Coverage Δ
appsec-extension ?
tracer-php 51.21% <ø> (-23.36%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

see 60 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4cc2897...7fd4a50. Read the comment docs.

@bwoebi
Copy link
Collaborator

bwoebi commented Sep 5, 2024

I see, the tracer sandboxing is sandboxing the bailout away :-)
I suppose some it would be ideal to signal the tracer "please bailout again after catching this" :-D

@estringana estringana force-pushed the estringana/blocking-within-tracer-hook branch from c5d1d67 to 1d93a16 Compare September 6, 2024 13:09
@estringana estringana force-pushed the estringana/blocking-within-tracer-hook branch from 1d93a16 to 78a05a9 Compare October 7, 2024 13:23
@estringana estringana force-pushed the estringana/blocking-within-tracer-hook branch 2 times, most recently from dedee17 to 3438fca Compare November 28, 2024 11:47
@pr-commenter
Copy link

pr-commenter bot commented Nov 29, 2024

Benchmarks [ tracer ]

Benchmark execution time: 2024-12-16 15:43:32

Comparing candidate commit 7fd4a50 in PR branch estringana/blocking-within-tracer-hook with baseline commit 4cc2897 in branch master.

Found 3 performance improvements and 3 performance regressions! Performance is the same for 172 metrics, 0 unstable metrics.

scenario:EmptyFileBench/benchEmptyFileBaseline

  • 🟥 execution_time [+113.262µs; +257.598µs] or [+3.961%; +9.008%]

scenario:MessagePackSerializationBench/benchMessagePackSerialization

  • 🟩 execution_time [-6.103µs; -4.277µs] or [-3.509%; -2.460%]

scenario:PDOBench/benchPDOBaseline

  • 🟩 execution_time [-15.087µs; -13.651µs] or [-7.765%; -7.026%]

scenario:PDOBench/benchPDOBaseline-opcache

  • 🟩 execution_time [-14.684µs; -9.580µs] or [-7.664%; -5.000%]

scenario:SamplingRuleMatchingBench/benchRegexMatching4-opcache

  • 🟥 execution_time [+139.095ns; +461.905ns] or [+2.023%; +6.718%]

scenario:TraceFlushBench/benchFlushTrace

  • 🟥 execution_time [+1000.000ns; +1000.000ns] or [+100.000%; +100.000%]

@estringana estringana force-pushed the estringana/blocking-within-tracer-hook branch 2 times, most recently from 8322005 to 1602d2c Compare November 29, 2024 16:12
Copy link
Contributor

@cataphract cataphract left a comment

Choose a reason for hiding this comment

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

I think it's fine although it's a mystery to me why sandbox.{h,c} are written the way they are.

inline void zai_sandbox_bailout(zai_sandbox *sandbox) {
if (!zai_sandbox_timed_out()) {
if (!zai_sandbox_timed_out() && !zai_is_request_blocked()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know why sandbox.h is written this way, with just C inline functions, but I guess that in the context of its strangeness it's fine.

@estringana estringana force-pushed the estringana/blocking-within-tracer-hook branch from 1602d2c to ee5bff3 Compare December 12, 2024 11:39
@@ -356,8 +375,25 @@ inline bool zai_sandbox_timed_out(void) {
return false;
}

inline bool zai_is_request_blocked(void)
Copy link
Contributor

Choose a reason for hiding this comment

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

Both implementations of this function are exactly the same, perhaps you can define it only once?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They are the same yes. However, this file is full of this duplication. I did it that way so follow the same approach

Copy link
Contributor

Choose a reason for hiding this comment

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

Unless there's a good technical reason, don't duplicate the function.

return false;
}

if (strcmp("Datadog blocked the request and presented a static error page", ZSTR_VAL(PG(last_error_message))) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

My main concern with this approach is that if someone changes the error message in appsec, it would stop working. Can you make changes on the appsec side to:

  • Make sure the error messages are validated at compile time?
  • Add comments specifying that the error message must not be changed.

return true;
}

if (strcmp("Datadog blocked the request, but the response has already been partially committed", ZSTR_VAL(PG(last_error_message))) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively you could check the prefix, which is always Datadog blocked the request

@@ -218,8 +220,25 @@ inline bool zai_sandbox_timed_out(void) {
return false;
}

inline bool zai_is_request_blocked(void)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is missing the redirection message as well.

@estringana estringana force-pushed the estringana/blocking-within-tracer-hook branch from ee5bff3 to 7fd4a50 Compare December 16, 2024 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants