Skip to content

Commit

Permalink
Fix: Fix false changed results in delta reports (#2098)
Browse files Browse the repository at this point in the history
When there are multiple results with the same VT, host, port,
and severity but with different descriptions, delta results are
now chosen so they match the original result with the same
description if one exists.
Without this, the identical results would falsely be shown as
changed.
  • Loading branch information
a-h-abdelsalam authored Oct 26, 2023
1 parent 27d1bb4 commit 5a76ab8
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -27485,6 +27485,22 @@ init_v2_delta_iterator (report_t report, iterator_t *results, report_t delta,
"nvts_cols");

extra_with = g_strdup_printf(" comparison AS ("
" WITH r1 as (SELECT results.id, description, host, report, port,"
" severity, nvt, results.qod, results.uuid, hostname,"
" path, r1_lateral.new_severity as new_severity "
" FROM results "
" LEFT JOIN (SELECT cvss_base, oid AS nvts_oid from nvts)"
" AS nvts_cols"
" ON nvts_cols.nvts_oid = results.nvt"
" %s, LATERAL %s AS r1_lateral"
" WHERE report = %llu),"
" r2 as (SELECT results.*, r2_lateral.new_severity AS new_severity"
" FROM results"
" LEFT JOIN (SELECT cvss_base, oid AS nvts_oid from nvts)"
" AS nvts_cols"
" ON nvts_cols.nvts_oid = results.nvt"
" %s, LATERAL %s AS r2_lateral"
" WHERE report = %llu)"
" SELECT r1.id AS result1_id,"
" r2.id AS result2_id,"
" compare_results("
Expand Down Expand Up @@ -27513,28 +27529,25 @@ init_v2_delta_iterator (report_t report, iterator_t *results, report_t delta,
RESULT_HOSTNAME_SQL("r2.hostname", "r2.host", "r2.report")
" AS delta_hostname,"
" r2.nvt_version AS delta_nvt_version"
" FROM (SELECT results.id, description, host, report, port,"
" severity, nvt, results.qod, results.uuid, hostname,"
" path, r1_lateral.new_severity as new_severity "
" FROM results "
" LEFT JOIN (SELECT cvss_base, oid AS nvts_oid from nvts)"
" AS nvts_cols"
" ON nvts_cols.nvts_oid = results.nvt"
" %s, LATERAL %s AS r1_lateral"
" WHERE report = %llu)"
" AS r1"
" FULL OUTER JOIN (SELECT results.*, r2_lateral.new_severity AS new_severity"
" FROM results"
" LEFT JOIN (SELECT cvss_base, oid AS nvts_oid from nvts)"
" AS nvts_cols"
" ON nvts_cols.nvts_oid = results.nvt"
" %s, LATERAL %s AS r2_lateral"
" WHERE report = %llu)"
" AS r2"
" FROM r1"
" FULL OUTER JOIN r2"
" ON r1.host = r2.host"
" AND normalize_port(r1.port) = normalize_port(r2.port)"
" AND r1.nvt = r2.nvt "
" AND (r1.new_severity = 0) = (r2.new_severity = 0)"
" AND (r1.description = r2.description"
" OR NOT EXISTS (SELECT * FROM r2"
" WHERE r1.description = r2.description"
" AND r1.host = r2.host"
" AND normalize_port(r1.port) = normalize_port(r2.port)"
" AND r1.nvt = r2.nvt"
" AND (r1.new_severity = 0) = (r2.new_severity = 0))"
" OR NOT EXISTS (SELECT * FROM r1"
" WHERE r1.description = r2.description"
" AND r1.host = r2.host"
" AND normalize_port(r1.port) = normalize_port(r2.port)"
" AND r1.nvt = r2.nvt"
" AND (r1.new_severity = 0) = (r2.new_severity = 0)))"
" )",
opts_tables,
with_lateral,
Expand Down

0 comments on commit 5a76ab8

Please sign in to comment.