@@ -508,7 +508,6 @@ static nr_library_table_t libraries[] = {
508
508
509
509
{"Predis" , NR_PSTR ("predis/src/client.php" ), nr_predis_enable },
510
510
{"Predis" , NR_PSTR ("predis/client.php" ), nr_predis_enable },
511
- {"PHP_Package" , NR_PSTR ("vendor/autoload.php" ), nr_composer_detected },
512
511
513
512
/*
514
513
* Allow Zend Framework 1.x to be detected as a library as well as a
@@ -933,28 +932,11 @@ static void nr_execute_handle_library(const char* filename,
933
932
}
934
933
}
935
934
936
- void nr_composer_detected () {
937
- if (NULL != NRPRG (txn ) && !NRPRG (txn )-> detection_status .composer_detected ) {
938
- const char * magic_file_1
939
- = "vendor/composer/autoload_real.php" ;
940
- const char * composer_file = "vendor/composer/installed.php" ;
941
- // the below should be using the absolute path
942
- if (0 == nr_access (magic_file_1 , F_OK | R_OK )) {
943
- if (0 == nr_access (composer_file , F_OK | R_OK )) {
944
- NRPRG (txn )-> detection_status .composer_detected = 1 ;
945
- NRPRG (txn )-> detection_status .file_exists = 1 ;
946
- NRPRG (txn )-> detection_status .api_called = 0 ;
947
- NRPRG (txn )-> detection_status .inside_eval_string = 0 ;
948
- }
949
- }
950
- }
951
- }
952
-
953
935
static void nr_get_composer_package_information () {
954
936
zval retval ;
955
937
int result = -1 ;
956
938
957
- if (NRPRG (txn )-> detection_status .inside_eval_string ) {
939
+ if (NRPRG (txn )-> composer_info .inside_eval_string ) {
958
940
return ;
959
941
}
960
942
@@ -998,14 +980,14 @@ static void nr_get_composer_package_information() {
998
980
" }"
999
981
"})();" ;
1000
982
1001
- NRPRG (txn )-> detection_status .inside_eval_string = 1 ;
983
+ NRPRG (txn )-> composer_info .inside_eval_string = 1 ;
1002
984
result = zend_eval_string (check , & retval , "check if class and methods exist" TSRMLS_CC );
1003
- NRPRG (txn )-> detection_status .inside_eval_string = 0 ;
985
+ NRPRG (txn )-> composer_info .inside_eval_string = 0 ;
1004
986
1005
987
if (result == SUCCESS ) {
1006
988
if (Z_TYPE (retval ) == IS_NULL ) {
1007
989
if (NULL != NRPRG (txn )) {
1008
- NRPRG (txn )-> detection_status .api_called = 1 ;
990
+ NRPRG (txn )-> composer_info .api_called = 1 ;
1009
991
}
1010
992
nrl_verbosedebug (NRL_TXN , "IS_NULL" );
1011
993
return ;
@@ -1018,10 +1000,10 @@ static void nr_get_composer_package_information() {
1018
1000
}
1019
1001
}
1020
1002
zval_dtor (& retval );
1021
- NRPRG (txn )-> detection_status .inside_eval_string = 1 ;
1003
+ NRPRG (txn )-> composer_info .inside_eval_string = 1 ;
1022
1004
result = zend_eval_string (getpackagename , & retval ,
1023
1005
"get installed packages by name" TSRMLS_CC );
1024
- NRPRG (txn )-> detection_status .inside_eval_string = 0 ;
1006
+ NRPRG (txn )-> composer_info .inside_eval_string = 0 ;
1025
1007
if (result == SUCCESS ) {
1026
1008
if (Z_TYPE (retval ) == IS_ARRAY ) {
1027
1009
zval * value ;
@@ -1033,9 +1015,9 @@ static void nr_get_composer_package_information() {
1033
1015
ZEND_HASH_FOREACH_VAL (Z_ARRVAL (retval ), value ) {
1034
1016
if (Z_TYPE_P (value ) == IS_STRING ) {
1035
1017
buf = nr_formatf (getversion , Z_STRVAL_P (value ));
1036
- NRPRG (txn )-> detection_status .inside_eval_string = 1 ;
1018
+ NRPRG (txn )-> composer_info .inside_eval_string = 1 ;
1037
1019
result2 = zend_eval_string (buf , & retval2 , "retrieve version for packages" );
1038
- NRPRG (txn )-> detection_status .inside_eval_string = 0 ;
1020
+ NRPRG (txn )-> composer_info .inside_eval_string = 0 ;
1039
1021
nr_free (buf );
1040
1022
if (SUCCESS == result2 ) {
1041
1023
if (nr_php_is_zval_valid_string (& retval2 )) {
@@ -1050,21 +1032,60 @@ static void nr_get_composer_package_information() {
1050
1032
ZEND_HASH_FOREACH_END ();
1051
1033
} else {
1052
1034
if (NULL != NRPRG (txn )) {
1053
- NRPRG (txn )-> detection_status .api_called = 1 ;
1035
+ NRPRG (txn )-> composer_info .api_called = 1 ;
1054
1036
}
1055
1037
zval_dtor (& retval );
1056
1038
return ;
1057
1039
}
1058
1040
zval_dtor (& retval );
1059
- NRPRG (txn )-> detection_status .api_called = 1 ;
1041
+ NRPRG (txn )-> composer_info .api_called = 1 ;
1060
1042
}
1061
1043
}
1062
1044
1063
- static void nr_execute_handle_composer () {
1064
- if (NRPRG (txn )-> detection_status .file_exists ) {
1065
- if (!NRPRG (txn )-> detection_status .api_called ) {
1066
- nr_get_composer_package_information ();
1067
- }
1045
+ static bool nr_execute_autoload_is_composer (const char * filename , const size_t filename_len ) {
1046
+ #define COMPOSER_MAGIC_FILE "composer/autoload_real.php"
1047
+ char * vendor_path = NULL ; // result of dirname(filename)
1048
+ char * composer_magic_file = NULL ; // vendor_path + COMPOSER_MAGIC_FILE
1049
+ char * cp = NULL ;
1050
+ bool composer_detected = false;
1051
+
1052
+ // vendor_path = dirname(filename):
1053
+ // 1. allocate space on stack
1054
+ vendor_path = nr_alloca (filename_len + 1 );
1055
+ // 2. copy filename to vendor_path
1056
+ nr_strcpy (vendor_path , filename );
1057
+ // 3. // find last occurence of '/' in vendor_path
1058
+ cp = nr_strrchr (vendor_path , '/' );
1059
+ // 4. replace '/' with '\0' to get the directory path
1060
+ * cp = '\0' ;
1061
+
1062
+ // composer_magic_file = vendor_path + "/composer/autoload_real.php"
1063
+ composer_magic_file = nr_str_append (vendor_path , COMPOSER_MAGIC_FILE , "/" );
1064
+
1065
+ if (0 == nr_access (composer_magic_file , F_OK | R_OK )) {
1066
+ nrl_debug (NRL_INSTRUMENT , "detected composer with %s" , composer_magic_file );
1067
+ composer_detected = true;
1068
+ nr_fw_support_add_library_supportability_metric (NRPRG (txn ), "Composer" );
1069
+ }
1070
+
1071
+ nr_free (composer_magic_file );
1072
+ return composer_detected ;
1073
+ }
1074
+
1075
+ static void nr_execute_handle_autoload (const char * filename , const size_t filename_len ) {
1076
+ #define AUTOLOAD_MAGIC_FILE "vendor/autoload.php"
1077
+ #define AUTOLOAD_MAGIC_FILE_LEN (sizeof(AUTOLOAD_MAGIC_FILE) - 1)
1078
+
1079
+ if (NRPRG (txn )-> composer_info .autoload_detected ) {
1080
+ return ;
1081
+ }
1082
+
1083
+ if (nr_striendswith (STR_AND_LEN (filename ), AUTOLOAD_MAGIC_FILE , AUTOLOAD_MAGIC_FILE_LEN )) {
1084
+ nrl_debug (NRL_INSTRUMENT , "detected autoload with %s, which ends with %s" , filename , AUTOLOAD_MAGIC_FILE );
1085
+ NRPRG (txn )-> composer_info .autoload_detected = true;
1086
+ nr_fw_support_add_library_supportability_metric (NRPRG (txn ), "Autoloader" );
1087
+
1088
+ NRPRG (txn )-> composer_info .composer_detected = nr_execute_autoload_is_composer (filename , filename_len );
1068
1089
}
1069
1090
}
1070
1091
@@ -1136,7 +1157,7 @@ static void nr_php_user_instrumentation_from_file(const char* filename,
1136
1157
nr_execute_handle_framework (all_frameworks , num_all_frameworks , filename ,
1137
1158
filename_len TSRMLS_CC );
1138
1159
nr_execute_handle_library (filename , filename_len TSRMLS_CC );
1139
- nr_execute_handle_composer (filename , filename_len );
1160
+ nr_execute_handle_autoload (filename , filename_len );
1140
1161
nr_execute_handle_logging_framework (filename , filename_len TSRMLS_CC );
1141
1162
if (NRINI (vulnerability_management_package_detection_enabled )) {
1142
1163
nr_execute_handle_package (filename );
@@ -1175,8 +1196,8 @@ static void nr_php_execute_file(const zend_op_array* op_array,
1175
1196
1176
1197
nr_php_add_user_instrumentation (TSRMLS_C );
1177
1198
1178
- if (NRPRG (txn )-> detection_status .composer_detected && !NRPRG (txn )-> detection_status .api_called ) {
1179
- nr_execute_handle_composer ( filename , filename_len );
1199
+ if (NRPRG (txn )-> composer_info .composer_detected && !NRPRG (txn )-> composer_info .api_called ) {
1200
+ nr_get_composer_package_information ( );
1180
1201
}
1181
1202
}
1182
1203
0 commit comments