Skip to content

Commit

Permalink
Change: Adjust loading of CPEs to new JSON API
Browse files Browse the repository at this point in the history
CPEs can now be loaded from JSON files based on the NVD API.

As some fields in the old XML differ the JSON API, they are replaced
by similar fields:
"nvd_id" is replaced by "cpe_name_id" and "status" is replaced by
"deprecated".

The "raw_data" will no longer be available after switching to JSON,
so the references are and "deprecated_by" element are handled explictly.

These changes are made because the XML-based data feeds have been
deprecated by NVD.
  • Loading branch information
timopollmeier committed Sep 30, 2024
1 parent 6589a19 commit 00dbe56
Show file tree
Hide file tree
Showing 7 changed files with 650 additions and 83 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ include (CPack)

set (GVMD_DATABASE_VERSION 256)

set (GVMD_SCAP_DATABASE_VERSION 21)
set (GVMD_SCAP_DATABASE_VERSION 22)

set (GVMD_CERT_DATABASE_VERSION 8)

Expand Down
35 changes: 27 additions & 8 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -13421,24 +13421,33 @@ handle_get_info (gmp_parser_t *gmp_parser, GError **error)
"<title>%s</title>",
cpe_info_iterator_title (&info));
xml_string_append (result,
"<nvd_id>%s</nvd_id>"
"<cpe_name_id>%s</cpe_name_id>"
"<severity>%s</severity>"
"<cve_refs>%s</cve_refs>"
"<status>%s</status>",
cpe_info_iterator_nvd_id (&info)
? cpe_info_iterator_nvd_id (&info)
"<deprecated>%s</deprecated>",
cpe_info_iterator_cpe_name_id (&info)
? cpe_info_iterator_cpe_name_id (&info)

Check warning on line 13429 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13428-L13429

Added lines #L13428 - L13429 were not covered by tests
: "",
cpe_info_iterator_severity (&info)
? cpe_info_iterator_severity (&info)
: "",
cpe_info_iterator_cve_refs (&info),
cpe_info_iterator_status (&info)
? cpe_info_iterator_status (&info)
: "");
cpe_info_iterator_deprecated (&info)
? cpe_info_iterator_deprecated (&info)

Check warning on line 13436 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13435-L13436

Added lines #L13435 - L13436 were not covered by tests
: "0");

if (get_info_data->details == 1)
{
iterator_t cves;
const char *deprecated_by_id
= cpe_info_iterator_deprecated_by_id (&info);

Check warning on line 13442 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13442

Added line #L13442 was not covered by tests
if (deprecated_by_id && strcmp (deprecated_by_id, ""))
{
xml_string_append (result,

Check warning on line 13445 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13445

Added line #L13445 was not covered by tests
"<deprecated_by>%s</deprecated_by>",
deprecated_by_id);
}

iterator_t cves, refs;
g_string_append (result, "<cves>");
init_cpe_cve_iterator (&cves, get_iterator_name (&info), 0, NULL);
while (next (&cves))
Expand Down Expand Up @@ -13466,6 +13475,16 @@ handle_get_info (gmp_parser_t *gmp_parser, GError **error)
: "");
cleanup_iterator (&cves);
g_string_append (result, "</cves>");

g_string_append (result, "<references>");
init_cpe_reference_iterator (&refs, get_iterator_name (&info));

Check warning on line 13480 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13479-L13480

Added lines #L13479 - L13480 were not covered by tests
while (next (&refs))
xml_string_append (result,

Check warning on line 13482 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13482

Added line #L13482 was not covered by tests
"<reference href=\"%s\">%s</reference>",
cpe_reference_iterator_href (&refs),
cpe_reference_iterator_type (&refs));
cleanup_iterator (&refs);
g_string_append (result, "</references>");

Check warning on line 13487 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13486-L13487

Added lines #L13486 - L13487 were not covered by tests
}
}
else if (g_strcmp0 ("cve", get_info_data->type) == 0)
Expand Down
18 changes: 14 additions & 4 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -3355,23 +3355,33 @@ const char*
cpe_info_iterator_title (iterator_t*);

const char*
cpe_info_iterator_status (iterator_t*);
cpe_info_iterator_deprecated (iterator_t*);

const char *
cpe_info_iterator_severity (iterator_t*);

const char*
cpe_info_iterator_deprecated_by_id (iterator_t*);
cpe_info_iterator_cve_refs (iterator_t*);

const char*
cpe_info_iterator_cve_refs (iterator_t*);
cpe_info_iterator_cpe_name_id (iterator_t*);

const char*
cpe_info_iterator_nvd_id (iterator_t*);
cpe_info_iterator_deprecated_by_id (iterator_t*);

gchar *
cpe_details_xml (const char*);

void
init_cpe_reference_iterator (iterator_t *, const char *);

const char*
cpe_reference_iterator_href (iterator_t *);

const char*
cpe_reference_iterator_type (iterator_t *);


/* CVE. */

const char*
Expand Down
12 changes: 10 additions & 2 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -3525,10 +3525,18 @@ manage_db_init (const gchar *name)
" modification_time integer,"
" title text,"
" status text,"
" deprecated_by_id INTEGER,"
" deprecated_by_id TEXT,"
" severity DOUBLE PRECISION DEFAULT 0,"
" cve_refs INTEGER DEFAULT 0,"
" nvd_id text);");
" nvd_id text,"
" deprecated integer,"
" cpe_name_id text);");

sql ("CREATE TABLE scap2.cpe_refs"

Check warning on line 3535 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L3535

Added line #L3535 was not covered by tests
" (id SERIAL PRIMARY KEY,"
" cpe INTEGER,"
" ref TEXT,"
" type TEXT);");

sql ("CREATE TABLE scap2.cpe_match_nodes"
" (id SERIAL PRIMARY KEY,"
Expand Down
Loading

0 comments on commit 00dbe56

Please sign in to comment.