Skip to content

Commit 9ea8847

Browse files
committed
fix: check for 0 length array values and key strings
1 parent 9c60ae6 commit 9ea8847

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
@@ -605,6 +605,16 @@ NR_PHP_WRAPPER(nr_drupal94_invoke_all_with_clean) {
605605
NR_PHP_WRAPPER_END
606606
#endif // OAPI
607607

608+
static bool nr_validate_key_val_arr(nr_php_string_hash_key_t* key, zval* val) {
609+
if (NULL == key || 0 == ZEND_STRING_LEN(key)
610+
|| 0 == nr_php_is_zval_valid_array(val)
611+
|| 0 == zend_hash_num_elements(Z_ARRVAL_P(val))) {
612+
return true;
613+
} else {
614+
return false;
615+
}
616+
}
617+
608618
/*
609619
* Purpose: Instrument Drupal Attribute Hooks for Drupal 11.1+
610620
*
@@ -636,23 +646,23 @@ static bool nr_drupal_hook_attribute_instrument(zval* module_handler) {
636646

637647
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(hook_implementation_map), hook_key,
638648
hook_val) {
639-
if ((NULL == hook_key) || (0 == nr_php_is_zval_valid_array(hook_val))) {
649+
if (nr_validate_key_val_arr(hook_key, hook_val)) {
640650
nrl_warning(NRL_FRAMEWORK,
641651
"hookImplementationsMap[hook]: invalid key or value");
642652
return false;
643653
}
644654

645655
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(hook_val), class_key, class_val) {
646-
if ((NULL == class_key) || (0 == nr_php_is_zval_valid_array(class_val))) {
656+
if (nr_validate_key_val_arr(class_key, class_val)) {
647657
nrl_warning(NRL_FRAMEWORK,
648658
"hookImplementationsMap[class]: invalid key or value");
649659
return false;
650660
}
651661

652662
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(class_val), method_key,
653663
module_val) {
654-
if ((NULL == method_key)
655-
|| (0 == nr_php_is_zval_valid_string(module_val))) {
664+
if (NULL == method_key
665+
|| 0 == nr_php_is_zval_valid_string(module_val)) {
656666
nrl_warning(NRL_FRAMEWORK,
657667
"hookImplementationsMap[method]: invalid key or value");
658668
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)