Skip to content

Commit 7f47b8d

Browse files
committed
Add schema extraction enabling flag
1 parent 96ce878 commit 7f47b8d

File tree

7 files changed

+103
-1
lines changed

7 files changed

+103
-1
lines changed

appsec/src/extension/commands/client_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static dd_result _pack_command(
169169

170170
double se_sample_rate = get_global_DD_API_SECURITY_REQUEST_SAMPLE_RATE();
171171
if (se_sample_rate >= MIN_SE_SAMPLE_RATE) {
172-
mpack_write_bool(w, true);
172+
mpack_write_bool(w, get_global_DD_EXPERIMENTAL_API_SECURITY_ENABLED());
173173

174174
dd_mpack_write_lstr(w, "sample_rate");
175175
mpack_write(w, se_sample_rate);

appsec/src/extension/configuration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ extern bool runtime_config_first_init;
6262
CONFIG(CUSTOM(STRING), DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING, "safe", .parser = dd_parse_automated_user_events_tracking) \
6363
CONFIG(STRING, DD_APPSEC_HTTP_BLOCKED_TEMPLATE_HTML, "") \
6464
CONFIG(STRING, DD_APPSEC_HTTP_BLOCKED_TEMPLATE_JSON, "") \
65+
CONFIG(BOOL, DD_EXPERIMENTAL_API_SECURITY_ENABLED, "false") \
6566
CONFIG(DOUBLE, DD_API_SECURITY_REQUEST_SAMPLE_RATE, "0.1")
6667
// clang-format on
6768

appsec/tests/extension/inc/mock_helper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ function get_commands() {
139139
}
140140
}
141141

142+
function get_command($command) {
143+
$commands = $this->get_commands();
144+
foreach($commands as $c) {
145+
if ($c[0] == $command) {
146+
return $c;
147+
}
148+
}
149+
150+
return [];
151+
}
152+
142153
function print_commands($sort = true) {
143154
$commands = $this->get_commands();
144155
if (!is_array($commands)) {
-1 Bytes
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Default schema extraction configurations
3+
--FILE--
4+
<?php
5+
use function datadog\appsec\testing\{rinit,rshutdown,get_formatted_runtime_id};
6+
7+
include __DIR__ . '/inc/mock_helper.php';
8+
9+
$helper = Helper::createInitedRun([
10+
response_list(response_request_init(['ok', []]))
11+
]);
12+
13+
var_dump(rinit());
14+
var_dump(rshutdown());
15+
16+
$clientInit = $helper->get_command('client_init');
17+
18+
var_dump($clientInit[1][5]['schema_extraction']);
19+
?>
20+
--EXPECTF--
21+
bool(true)
22+
bool(true)
23+
array(2) {
24+
["enabled"]=>
25+
bool(false)
26+
["sample_rate"]=>
27+
float(0.1)
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Schema extraction configured
3+
--ENV--
4+
DD_EXPERIMENTAL_API_SECURITY_ENABLED=true
5+
DD_API_SECURITY_REQUEST_SAMPLE_RATE=0.5
6+
--FILE--
7+
<?php
8+
use function datadog\appsec\testing\{rinit,rshutdown,get_formatted_runtime_id};
9+
10+
include __DIR__ . '/inc/mock_helper.php';
11+
12+
$helper = Helper::createInitedRun([
13+
response_list(response_request_init(['ok', []]))
14+
]);
15+
16+
var_dump(rinit());
17+
var_dump(rshutdown());
18+
19+
$clientInit = $helper->get_command('client_init');
20+
21+
var_dump($clientInit[1][5]['schema_extraction']);
22+
?>
23+
--EXPECTF--
24+
bool(true)
25+
bool(true)
26+
array(2) {
27+
["enabled"]=>
28+
bool(true)
29+
["sample_rate"]=>
30+
float(0.5)
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Schema extraction is disabled when sample rate is 0
3+
--ENV--
4+
DD_EXPERIMENTAL_API_SECURITY_ENABLED=true
5+
DD_API_SECURITY_REQUEST_SAMPLE_RATE=0
6+
--FILE--
7+
<?php
8+
use function datadog\appsec\testing\{rinit,rshutdown,get_formatted_runtime_id};
9+
10+
include __DIR__ . '/inc/mock_helper.php';
11+
12+
$helper = Helper::createInitedRun([
13+
response_list(response_request_init(['ok', []]))
14+
]);
15+
16+
var_dump(rinit());
17+
var_dump(rshutdown());
18+
19+
$clientInit = $helper->get_command('client_init');
20+
21+
var_dump($clientInit[1][5]['schema_extraction']);
22+
?>
23+
--EXPECTF--
24+
bool(true)
25+
bool(true)
26+
array(2) {
27+
["enabled"]=>
28+
bool(false)
29+
["sample_rate"]=>
30+
float(0)
31+
}

0 commit comments

Comments
 (0)