Skip to content

Commit 0d833f9

Browse files
committed
Skip sampling if the request is already blocked
1 parent e58a241 commit 0d833f9

File tree

5 files changed

+17
-8
lines changed

5 files changed

+17
-8
lines changed

appsec/src/extension/commands_helpers.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "logging.h"
1212
#include "msgpack_helpers.h"
1313
#include "request_abort.h"
14+
#include "request_lifecycle.h"
1415
#include "tags.h"
1516
#include "telemetry.h"
1617
#include "user_tracking.h"
@@ -593,13 +594,13 @@ static dd_result _command_process_actions(
593594
res = dd_should_block;
594595
_command_process_block_parameters(
595596
&ctx->block_params, mpack_node_array_at(action, 1));
596-
dd_tags_add_blocked();
597+
dd_req_lifecycle_set_blocked();
597598
} else if (dd_mpack_node_lstr_eq(verdict, "redirect") &&
598599
res != dd_should_redirect) {
599600
res = dd_should_redirect;
600601
_command_process_redirect_parameters(
601602
&ctx->block_params, mpack_node_array_at(action, 1));
602-
dd_tags_add_blocked();
603+
dd_req_lifecycle_set_blocked();
603604
} else if (dd_mpack_node_lstr_eq(verdict, "record") &&
604605
res == dd_success) {
605606
res = dd_should_record;

appsec/src/extension/request_lifecycle.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,18 @@ static THREAD_LOCAL_ON_ZTS zend_string *nullable _client_ip;
5050
static THREAD_LOCAL_ON_ZTS zval _blocking_function;
5151
static THREAD_LOCAL_ON_ZTS bool _shutdown_done_on_commit;
5252
static THREAD_LOCAL_ON_ZTS bool _empty_service_or_env;
53+
static THREAD_LOCAL_ON_ZTS bool _request_blocked;
5354
#define MAX_LENGTH_OF_REM_CFG_PATH 31
5455
static THREAD_LOCAL_ON_ZTS char
5556
_last_rem_cfg_path[MAX_LENGTH_OF_REM_CFG_PATH + 1];
5657
#define CLIENT_IP_LOOKUP_FAILED ((zend_string *)-1)
5758

5859
bool dd_req_is_user_req(void) { return _enabled_user_req; }
5960

61+
void dd_req_lifecycle_set_blocked(void) { _request_blocked = true; }
62+
63+
bool dd_req_lifecycle_is_blocked(void) { return _request_blocked; }
64+
6065
void dd_req_lifecycle_startup(void)
6166
{
6267
// we assume that frankenphp is running in worker mode because
@@ -427,6 +432,7 @@ static void _reset_globals(void)
427432
ZVAL_UNDEF(&_blocking_function);
428433

429434
_shutdown_done_on_commit = false;
435+
_request_blocked = false;
430436
dd_tags_rshutdown();
431437
dd_rasp_reset_globals();
432438
}
@@ -924,6 +930,11 @@ static uint64_t _calc_sampling_key(zend_object *root_span, int status_code)
924930
return 0;
925931
}
926932

933+
if (_request_blocked) {
934+
mlog_g(dd_log_debug, "Request was blocked; not sampling for API security");
935+
return 0;
936+
}
937+
927938
if (!root_span) {
928939
return 0;
929940
}

appsec/src/extension/request_lifecycle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
extern ddtrace_user_req_listeners dd_user_req_listeners;
99

1010
bool dd_req_is_user_req(void);
11+
void dd_req_lifecycle_set_blocked(void);
12+
bool dd_req_lifecycle_is_blocked(void);
1113
void dd_req_lifecycle_startup(void);
1214
void dd_req_lifecycle_rinit(bool force);
1315
void dd_req_lifecycle_rshutdown(bool ignore_verdict, bool force);

appsec/src/extension/tags.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ static THREAD_LOCAL_ON_ZTS bool _user_event_triggered;
142142
static THREAD_LOCAL_ON_ZTS bool _appsec_json_frags_inited;
143143
static THREAD_LOCAL_ON_ZTS zend_llist _appsec_json_frags;
144144
static THREAD_LOCAL_ON_ZTS zend_string *nullable _event_user_id;
145-
static THREAD_LOCAL_ON_ZTS bool _blocked;
146145

147146
static void _init_relevant_headers(void);
148147
static zend_string *_concat_json_fragments(void);
@@ -352,7 +351,6 @@ void dd_tags_rinit(void)
352351

353352
// Just in case...
354353
_event_user_id = NULL;
355-
_blocked = false;
356354
}
357355

358356
void dd_tags_add_appsec_json_frag(zend_string *nonnull zstr)
@@ -440,8 +438,6 @@ void dd_tags_add_tags(
440438
}
441439
}
442440

443-
void dd_tags_add_blocked(void) { _blocked = true; }
444-
445441
static void _zend_string_release_indirect(void *s)
446442
{
447443
zend_string_release(*(zend_string **)s);
@@ -830,7 +826,7 @@ static void _dd_event_user_id(zend_array *meta_ht)
830826

831827
static void _dd_appsec_blocked(zend_array *meta_ht)
832828
{
833-
if (_blocked) {
829+
if (dd_req_lifecycle_is_blocked()) {
834830
_add_new_zstr_to_meta(
835831
meta_ht, _dd_tag_blocked_zstr, _true_zstr, true, false);
836832
}

appsec/src/extension/tags.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ void dd_tags_shutdown(void);
1818
void dd_tags_rinit(void);
1919
void dd_tags_rshutdown(void);
2020
void dd_tags_add_tags(zend_object *nonnull span, zend_array *nullable superglob_equiv);
21-
void dd_tags_add_blocked(void);
2221
void dd_tags_set_user_event_triggered(void);
2322

2423
// Copies (or increases refcount) of zstr

0 commit comments

Comments
 (0)