Skip to content

Commit b9abda8

Browse files
committed
fix: package info from composer wins [WIP]
Ensure that package information obtained from composer has higher precedence over package information obtained from the package itself (legacy method). [WIP] - unit tests updates pending...
1 parent ee79ae3 commit b9abda8

File tree

5 files changed

+72
-11
lines changed

5 files changed

+72
-11
lines changed

agent/lib_composer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ static void nr_execute_handle_autoload_composer_get_packages_information(
231231
NRSAFESTR(ZSTR_VAL(package_name)),
232232
NRSAFESTR(Z_STRVAL_P(package_version)));
233233
if (NRINI(vulnerability_management_package_detection_enabled)) {
234-
nr_txn_add_php_package(NRPRG(txn), NRSAFESTR(ZSTR_VAL(package_name)),
235-
NRSAFESTR(Z_STRVAL_P(package_version)));
234+
nr_txn_add_php_package_from_source(NRPRG(txn), NRSAFESTR(ZSTR_VAL(package_name)),
235+
NRSAFESTR(Z_STRVAL_P(package_version)), NR_PHP_PACKAGE_SOURCE_COMPOSER);
236236
}
237237
nr_fw_support_add_package_supportability_metric(
238238
NRPRG(txn), NRSAFESTR(ZSTR_VAL(package_name)),

axiom/nr_php_packages.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,18 @@ typedef struct {
2323
bool package_added;
2424
} nr_php_package_json_builder_t;
2525

26-
nr_php_package_t* nr_php_package_create(char* name, char* version) {
26+
static inline const char* nr_php_package_source_priority_to_string(const nr_php_package_source_priority_t source_priority) {
27+
switch (source_priority) {
28+
case NR_PHP_PACKAGE_SOURCE_LEGACY:
29+
return "legacy";
30+
case NR_PHP_PACKAGE_SOURCE_COMPOSER:
31+
return "composer";
32+
default:
33+
return "unknown";
34+
}
35+
}
36+
37+
nr_php_package_t* nr_php_package_create_with_source(char* name, char* version, const nr_php_package_source_priority_t source_priority) {
2738
nr_php_package_t* p = NULL;
2839

2940
if (NULL == name) {
@@ -43,12 +54,17 @@ nr_php_package_t* nr_php_package_create(char* name, char* version) {
4354
PHP_PACKAGE_VERSION_UNKNOWN); // if null, version is set to an empty
4455
// string with a space according to spec
4556
}
57+
p->source_priority = source_priority;
4658

47-
nrl_verbosedebug(NRL_INSTRUMENT, "Creating PHP Package '%s', version '%s'",
48-
p->package_name, p->package_version);
59+
nrl_verbosedebug(NRL_INSTRUMENT, "Creating PHP Package '%s', version '%s', source %s",
60+
p->package_name, p->package_version, nr_php_package_source_priority_to_string(source_priority));
4961
return p;
5062
}
5163

64+
nr_php_package_t* nr_php_package_create(char* name, char* version) {
65+
return nr_php_package_create_with_source(name, version, NR_PHP_PACKAGE_SOURCE_LEGACY);
66+
}
67+
5268
void nr_php_package_destroy(nr_php_package_t* p) {
5369
if (NULL != p) {
5470
nr_free(p->package_name);
@@ -87,7 +103,7 @@ void nr_php_packages_add_package(nr_php_packages_t* h, nr_php_package_t* p) {
87103
package = (nr_php_package_t*)nr_hashmap_get(h->data, p->package_name,
88104
nr_strlen(p->package_name));
89105
if (NULL != package) {
90-
if (0 != nr_strcmp(package->package_version, p->package_version)) {
106+
if (package->source_priority <= p->source_priority && 0 != nr_strcmp(package->package_version, p->package_version)) {
91107
nr_free(package->package_version);
92108
package->package_version = nr_strdup(p->package_version);
93109
}

axiom/nr_php_packages.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,39 @@
1313

1414
#define PHP_PACKAGE_VERSION_UNKNOWN " "
1515

16+
typedef enum {
17+
NR_PHP_PACKAGE_SOURCE_LEGACY,
18+
NR_PHP_PACKAGE_SOURCE_COMPOSER
19+
} nr_php_package_source_priority_t;
20+
1621
typedef struct _nr_php_package_t {
1722
char* package_name;
1823
char* package_version;
24+
nr_php_package_source_priority_t source_priority;
1925
} nr_php_package_t;
2026

2127
typedef struct _nr_php_packages_t {
2228
nr_hashmap_t* data;
2329
} nr_php_packages_t;
2430

2531
/*
26-
* Purpose : Create a new php package. If the name is null, then no package will
32+
* Purpose : Create a new php package with desired source priority. If the name is null, then no package will
33+
* be created. If the version is null (version = NULL), then
34+
* the package will still be created and the version will be set to an
35+
* empty string with a space.
36+
*
37+
* Params : 1. Package name
38+
* 2. Package version
39+
* 3. Package source priority (legacy or composer)
40+
*
41+
* Returns : A php package that has a name and version. If
42+
* nr_php_packages_add_package() is not called, then it must be freed
43+
* by nr_php_package_destroy()
44+
*/
45+
extern nr_php_package_t* nr_php_package_create_with_source(char* name, char* version, const nr_php_package_source_priority_t source_priority);
46+
47+
/*
48+
* Purpose : Create a new php package with legacy source priority. If the name is null, then no package will
2749
* be created. If the version is null (version = NULL), then
2850
* the package will still be created and the version will be set to an
2951
* empty string with a space.

axiom/nr_txn.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3494,9 +3494,10 @@ void nr_txn_record_log_event(nrtxn_t* txn,
34943494
nr_txn_add_logging_metrics(txn, log_level_name);
34953495
}
34963496

3497-
void nr_txn_add_php_package(nrtxn_t* txn,
3497+
void nr_txn_add_php_package_from_source(nrtxn_t* txn,
34983498
char* package_name,
3499-
char* package_version) {
3499+
char* package_version,
3500+
const nr_php_package_source_priority_t source) {
35003501
nr_php_package_t* p = NULL;
35013502

35023503
if (nrunlikely(NULL == txn)) {
@@ -3507,6 +3508,13 @@ void nr_txn_add_php_package(nrtxn_t* txn,
35073508
return;
35083509
}
35093510

3510-
p = nr_php_package_create(package_name, package_version);
3511+
p = nr_php_package_create_with_source(package_name, package_version, source);
35113512
nr_php_packages_add_package(txn->php_packages, p);
35123513
}
3514+
3515+
void nr_txn_add_php_package(nrtxn_t* txn,
3516+
char* package_name,
3517+
char* package_version) {
3518+
nr_txn_add_php_package_from_source(txn, package_name, package_version,
3519+
NR_PHP_PACKAGE_SOURCE_LEGACY);
3520+
}

axiom/nr_txn.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,22 @@ static inline nr_segment_t* nr_txn_allocate_segment(nrtxn_t* txn) {
11701170
}
11711171

11721172
/*
1173-
* Purpose : Add php packages to transaction. This function should only be
1173+
* Purpose : Add php package to transaction from desired source. This function should only be
1174+
* called when Vulnerability Management is enabled.
1175+
*
1176+
* Params : 1. The transaction
1177+
* 2. Package name
1178+
* 3. Package version
1179+
* 4. Source priority
1180+
*
1181+
*/
1182+
void nr_txn_add_php_package_from_source(nrtxn_t* txn,
1183+
char* package_name,
1184+
char* package_version,
1185+
const nr_php_package_source_priority_t source);
1186+
1187+
/*
1188+
* Purpose : Add php package to transaction from legacy source. This function should only be
11741189
* called when Vulnerability Management is enabled.
11751190
*
11761191
* Params : 1. The transaction

0 commit comments

Comments
 (0)