Skip to content

Commit f9530ed

Browse files
authored
refactor(agent): improve magic file recognition performance (#970)
Speed up package detection by performing a suffix match on the 'magic' file pattern with case insensitive string comparison instead of a substring search within a lowercased filename. This is possible because all of the 'magic' file search patterns patterns are right anchored. Fixup of e11b992 with changes from 24c1c65.
1 parent aa15a2d commit f9530ed

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

agent/php_execute.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,17 @@ static size_t num_logging_frameworks
603603
typedef struct _nr_vuln_mgmt_table_t {
604604
const char* package_name;
605605
const char* file_to_check;
606+
size_t file_to_check_len;
606607
nr_vuln_mgmt_enable_fn_t enable;
607608
} nr_vuln_mgmt_table_t;
608609

609610
/* Note that all paths should be in lowercase. */
611+
// clang-format: off
610612
static const nr_vuln_mgmt_table_t vuln_mgmt_packages[] = {
611-
{"Drupal", "drupal/component/dependencyinjection/container.php", nr_drupal_version},
612-
{"Wordpress", "wp-includes/version.php", nr_wordpress_version},
613+
{"Drupal", NR_PSTR("drupal/component/dependencyinjection/container.php"), nr_drupal_version},
614+
{"Wordpress", NR_PSTR("wp-includes/version.php"), nr_wordpress_version},
613615
};
616+
// clang-format: on
614617

615618
static const size_t num_packages
616619
= sizeof(vuln_mgmt_packages) / sizeof(nr_vuln_mgmt_table_t);
@@ -990,28 +993,22 @@ static void nr_execute_handle_logging_framework(const char* filename,
990993
}
991994
}
992995

993-
#undef STR_AND_LEN
994-
995-
static void nr_execute_handle_package(const char* filename) {
996-
if (NULL == filename || 0 >= nr_strlen(filename)) {
997-
nrl_verbosedebug(NRL_FRAMEWORK, "%s: The file name is NULL",
998-
__func__);
999-
return;
1000-
}
1001-
char* filename_lower = nr_string_to_lowercase(filename);
996+
static void nr_execute_handle_package(const char* filename,
997+
const size_t filename_len) {
1002998
size_t i = 0;
1003999

10041000
for (i = 0; i < num_packages; i++) {
1005-
if (nr_stridx(filename_lower, vuln_mgmt_packages[i].file_to_check) >= 0) {
1001+
if (nr_striendswith(STR_AND_LEN(filename),
1002+
STR_AND_LEN(vuln_mgmt_packages[i].file_to_check))) {
10061003
if (NULL != vuln_mgmt_packages[i].enable) {
10071004
vuln_mgmt_packages[i].enable();
10081005
}
10091006
}
10101007
}
1011-
1012-
nr_free(filename_lower);
10131008
}
10141009

1010+
#undef STR_AND_LEN
1011+
10151012
/*
10161013
* Purpose : Detect library and framework usage from a PHP file.
10171014
*
@@ -1036,7 +1033,7 @@ static void nr_php_user_instrumentation_from_file(const char* filename,
10361033
nr_execute_handle_autoload(filename, filename_len);
10371034
nr_execute_handle_logging_framework(filename, filename_len TSRMLS_CC);
10381035
if (NRINI(vulnerability_management_package_detection_enabled)) {
1039-
nr_execute_handle_package(filename);
1036+
nr_execute_handle_package(filename, filename_len);
10401037
}
10411038
}
10421039

0 commit comments

Comments
 (0)