Skip to content

Commit 0a18a7a

Browse files
committed
fix: check for 0 length array values and key strings
1 parent 1d993a0 commit 0a18a7a

File tree

5 files changed

+152
-4
lines changed

5 files changed

+152
-4
lines changed

agent/fw_drupal8.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,16 @@ NR_PHP_WRAPPER(nr_drupal94_invoke_all_with_clean) {
608608
NR_PHP_WRAPPER_END
609609
#endif // OAPI
610610

611+
static bool nr_validate_key_val_arr(nr_php_string_hash_key_t* key, zval* val) {
612+
if (NULL == key || 0 == ZEND_STRING_LEN(key)
613+
|| 0 == nr_php_is_zval_valid_array(val)
614+
|| 0 == zend_hash_num_elements(Z_ARRVAL_P(val))) {
615+
return true;
616+
} else {
617+
return false;
618+
}
619+
}
620+
611621
/*
612622
* Purpose: Instrument Drupal Attribute Hooks for Drupal 11.1+
613623
*
@@ -639,23 +649,23 @@ static bool nr_drupal_hook_attribute_instrument(zval* module_handler) {
639649

640650
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(hook_implementation_map), hook_key,
641651
hook_val) {
642-
if ((NULL == hook_key) || (0 == nr_php_is_zval_valid_array(hook_val))) {
652+
if (nr_validate_key_val_arr(hook_key, hook_val)) {
643653
nrl_warning(NRL_FRAMEWORK,
644654
"hookImplementationsMap[hook]: invalid key or value");
645655
return false;
646656
}
647657

648658
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(hook_val), class_key, class_val) {
649-
if ((NULL == class_key) || (0 == nr_php_is_zval_valid_array(class_val))) {
659+
if (nr_validate_key_val_arr(class_key, class_val)) {
650660
nrl_warning(NRL_FRAMEWORK,
651661
"hookImplementationsMap[class]: invalid key or value");
652662
return false;
653663
}
654664

655665
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(class_val), method_key,
656666
module_val) {
657-
if ((NULL == method_key)
658-
|| (0 == nr_php_is_zval_valid_string(module_val))) {
667+
if (NULL == method_key
668+
|| 0 == nr_php_is_zval_valid_string(module_val)) {
659669
nrl_warning(NRL_FRAMEWORK,
660670
"hookImplementationsMap[method]: invalid key or value");
661671
return false;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/* Verify agent behavior when key value is an empty array */
8+
9+
namespace Drupal\Core\Extension {
10+
interface ModuleHandlerInterface
11+
{
12+
public function invokeAllWith($hook_str, $callback);
13+
}
14+
class ModuleHandler implements ModuleHandlerInterface
15+
{
16+
protected array $hookImplementationsMap = array(
17+
'hookname' => array('classname' => array('methodname' => 'modulename')),
18+
'hookname_b' => array(),
19+
'hookname_c' => array('classname_c' => array('methodname_c' => 'modulename_c')),
20+
);
21+
22+
// to avoid editor warnings
23+
public function invokeAllWith($hook_str, $callback)
24+
{
25+
return null;
26+
}
27+
28+
// for debugging purposes
29+
public function dump()
30+
{
31+
var_dump($this->hookImplementationsMap);
32+
}
33+
}
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/* Verify agent behavior when key is an empty string */
8+
9+
namespace Drupal\Core\Extension {
10+
interface ModuleHandlerInterface
11+
{
12+
public function invokeAllWith($hook_str, $callback);
13+
}
14+
class ModuleHandler implements ModuleHandlerInterface
15+
{
16+
protected array $hookImplementationsMap = array(
17+
'hookname' => array('classname' => array('methodname' => 'modulename')),
18+
'hookname_b' => array('' => array('methodname_b' => 'modulename_b')),
19+
'hookname_c' => array('classname_c' => array('methodname_c' => 'modulename_c')),
20+
);
21+
22+
// to avoid editor warnings
23+
public function invokeAllWith($hook_str, $callback)
24+
{
25+
return null;
26+
}
27+
28+
// for debugging purposes
29+
public function dump()
30+
{
31+
var_dump($this->hookImplementationsMap);
32+
}
33+
}
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
Verify agent behavior when key value is an empty array
9+
*/
10+
11+
/*INI
12+
newrelic.framework = drupal8
13+
*/
14+
15+
/*EXPECT_TRACED_ERRORS null */
16+
17+
/*EXPECT_ERROR_EVENTS null */
18+
19+
/*EXPECT
20+
*/
21+
22+
require_once __DIR__ . '/mock_module_handler_empty_array.php';
23+
24+
// This specific API is needed for us to instrument the ModuleHandler
25+
class Drupal
26+
{
27+
public function moduleHandler()
28+
{
29+
return new Drupal\Core\Extension\ModuleHandler();
30+
}
31+
}
32+
33+
// Create module handler
34+
$drupal = new Drupal();
35+
$handler = $drupal->moduleHandler();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/*
3+
* Copyright 2020 New Relic Corporation. All rights reserved.
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/*DESCRIPTION
8+
Verify agent behavior when key is an empty string
9+
*/
10+
11+
/*INI
12+
newrelic.framework = drupal8
13+
*/
14+
15+
/*EXPECT_TRACED_ERRORS null */
16+
17+
/*EXPECT_ERROR_EVENTS null */
18+
19+
/*EXPECT
20+
*/
21+
22+
require_once __DIR__ . '/mock_module_handler_empty_key.php';
23+
24+
// This specific API is needed for us to instrument the ModuleHandler
25+
class Drupal
26+
{
27+
public function moduleHandler()
28+
{
29+
return new Drupal\Core\Extension\ModuleHandler();
30+
}
31+
}
32+
33+
// Create module handler
34+
$drupal = new Drupal();
35+
$handler = $drupal->moduleHandler();

0 commit comments

Comments
 (0)