Skip to content

Commit aab8068

Browse files
committed
refactor: simplify interaction with Composer
Use \Composer\InstalledVersions::getAllRawData() to collect and process (format, filter, ...) all data neeeded for vulnerability management.
1 parent a7e981b commit aab8068

File tree

1 file changed

+62
-10
lines changed

1 file changed

+62
-10
lines changed

agent/lib_composer.c

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,22 @@ static bool nr_execute_handle_autoload_composer_is_initialized() {
2121
};
2222

2323
// the class is found - there's hope!
24+
#if 0
2425
if (NULL == nr_php_find_class_method(zce, "getinstalledpackages")
2526
|| NULL == nr_php_find_class_method(zce, "getversion")) {
2627
nrl_verbosedebug(
2728
NRL_INSTRUMENT,
2829
"Composer\\InstalledVersions class found, but methods not found");
2930
return false;
3031
}
32+
#else
33+
if (NULL == nr_php_find_class_method(zce, "getallrawdata")) {
34+
nrl_verbosedebug(
35+
NRL_INSTRUMENT,
36+
"Composer\\InstalledVersions class found, but methods not found");
37+
return false;
38+
}
39+
#endif
3140

3241
return true;
3342
}
@@ -93,9 +102,11 @@ static int nr_execute_handle_autoload_composer_init(const char* vendor_path) {
93102

94103
static void nr_execute_handle_autoload_composer_get_packages_information(
95104
const char* vendor_path) {
96-
zval retval;
105+
zval retval; // This is used as a return value for zend_eval_string.
106+
// It will only be set if the result of the eval is SUCCESS.
97107
int result = -1;
98108

109+
#if 0
99110
char* getpackagename
100111
= ""
101112
"(function() {"
@@ -119,6 +130,25 @@ static void nr_execute_handle_autoload_composer_get_packages_information(
119130
" return NULL;"
120131
" }"
121132
"})();";
133+
#else
134+
char* getallrawdata
135+
= ""
136+
"(function() {"
137+
" try {"
138+
" $packages = array();"
139+
" foreach (\\Composer\\InstalledVersions::getAllRawData() as $installed) { "
140+
" foreach ($installed['versions'] as $packageName => $packageData) {"
141+
" if (isset($packageData['pretty_version'])) {"
142+
" $packages[$packageName] = ltrim($packageData['pretty_version'], 'v');"
143+
" }"
144+
" }"
145+
" }"
146+
" return $packages;"
147+
" } catch (Exception $e) {"
148+
" return NULL;"
149+
" }"
150+
"})();";
151+
#endif
122152

123153
if (NR_SUCCESS != nr_execute_handle_autoload_composer_init(vendor_path)) {
124154
nrl_debug(NRL_INSTRUMENT,
@@ -131,7 +161,7 @@ static void nr_execute_handle_autoload_composer_get_packages_information(
131161
nrl_verbosedebug(NRL_INSTRUMENT, "%s - Composer runtime API available",
132162
__func__);
133163

134-
#if 1
164+
#if 0
135165
result = zend_eval_string(getpackagename, &retval,
136166
"get installed packages by name" TSRMLS_CC);
137167
if (result == SUCCESS) {
@@ -179,15 +209,37 @@ static void nr_execute_handle_autoload_composer_get_packages_information(
179209
zval_dtor(&retval);
180210
}
181211
#else
182-
zv = nr_php_call(NULL, "Composer\\InstalledVersions::getInstalledPackages",
183-
NULL);
184-
if (NULL != zv) {
185-
char strbuf[NR_EXECUTE_DEBUG_STRBUFSZ];
186-
nr_format_zval_for_debug(zv, strbuf, 0, NR_EXECUTE_DEBUG_STRBUFSZ - 1, 0);
187-
nrl_always("Composer\\InstalledVersions::getInstalledPackages()=%s",
188-
strbuf);
189-
nr_php_zval_free(&zv);
212+
result = zend_eval_string(getallrawdata, &retval, "composer_getallrawdata.php");
213+
if (SUCCESS != result) {
214+
nrl_verbosedebug(NRL_INSTRUMENT, "%s - composer_getallrawdata.php failed", __func__);
215+
return;
216+
}
217+
if (IS_ARRAY == Z_TYPE(retval)) {
218+
zend_string* package_name = NULL;
219+
zval* package_version = NULL;
220+
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL(retval), package_name, package_version) {
221+
if (NULL == package_name || NULL == package_version) {
222+
continue;
223+
}
224+
if (nr_php_is_zval_non_empty_string(package_version)) {
225+
nrl_verbosedebug(NRL_INSTRUMENT, "package %s, version %s",
226+
NRSAFESTR(ZSTR_VAL(package_name)),
227+
NRSAFESTR(Z_STRVAL_P(package_version)));
228+
if (NRINI(vulnerability_management_package_detection_enabled)) {
229+
nr_txn_add_php_package(NRPRG(txn), NRSAFESTR(ZSTR_VAL(package_name)),
230+
NRSAFESTR(Z_STRVAL_P(package_version)));
231+
}
232+
nr_fw_support_add_package_supportability_metric(
233+
NRPRG(txn), NRSAFESTR(ZSTR_VAL(package_name)),
234+
NRSAFESTR(Z_STRVAL_P(package_version)));
235+
}
236+
}
237+
ZEND_HASH_FOREACH_END();
238+
} else {
239+
nrl_verbosedebug(NRL_INSTRUMENT,
240+
"%s - installed packages is not an array", __func__);
190241
}
242+
zval_dtor(&retval);
191243
#endif
192244
}
193245

0 commit comments

Comments
 (0)