Skip to content

Commit 399ff7e

Browse files
fix: don't close old findings when reimport auto-creates a new test (#14396)
* fix: don't close old findings when reimport auto-creates a new test When auto_create_context=True and the test doesn't exist yet, the reimport falls back to DefaultImporter. With close_old_findings=True, this would incorrectly close findings from other tests in the same engagement/product scope because the newly created test has no prior findings to compare against. Suppress close_old_findings for this initial-import path. Fixes #14363. * test: regression test for #14363 via API serializer path Add test_reimport_auto_create_does_not_close_findings_in_existing_test to ImportReimportTestAPI. It calls the reimport endpoint with auto_create_context=True and close_old_findings=True targeting a non-existing test title, verifying that the existing test's findings are not closed when the endpoint auto-creates a new test. This test would fail if the close_old_findings=False override in the serializer were reverted.
1 parent ebe181e commit 399ff7e

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

dojo/api_v2/serializers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2711,8 +2711,11 @@ def process_scan(
27112711
# Attempt to create an engagement
27122712
logger.debug("reimport for non-existing test, using import to create new test")
27132713
context["engagement"] = auto_create_manager.get_or_create_engagement(**context)
2714+
# Do not close old findings when creating a brand new test: there are no
2715+
# existing findings to compare against, and close_old_findings would
2716+
# incorrectly close findings from other tests in the same scope.
27142717
context["test"], _, _, _, _, _, _ = self.get_importer(
2715-
**context,
2718+
**{**context, "close_old_findings": False},
27162719
).process_scan(
27172720
context.pop("scan", None),
27182721
)

unittests/test_import_reimport.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,6 +2582,63 @@ def test_reimport_set_scan_date_parser_sets_date(self):
25822582
date = findings["results"][0]["date"]
25832583
self.assertEqual(date, "2006-12-26")
25842584

2585+
def test_reimport_auto_create_does_not_close_findings_in_existing_test(self):
2586+
"""
2587+
Regression test for #14363: when reimport with auto_create_context=True creates
2588+
a brand new test, close_old_findings must not close findings from other tests in
2589+
the same engagement scope.
2590+
2591+
The serializer now forces close_old_findings=False when calling DefaultImporter
2592+
in this path. Without the fix, all 4 findings from the pre-existing test would be
2593+
incorrectly closed.
2594+
"""
2595+
product_type, _ = Product_Type.objects.get_or_create(name="PT CloseOld AutoCreate")
2596+
product, _ = Product.objects.get_or_create(
2597+
name="P CloseOld AutoCreate",
2598+
description="test",
2599+
prod_type=product_type,
2600+
)
2601+
engagement = Engagement.objects.create(
2602+
name="E CloseOld AutoCreate",
2603+
product=product,
2604+
target_start=timezone.now(),
2605+
target_end=timezone.now(),
2606+
)
2607+
2608+
acunetix_many_findings = get_unit_tests_scans_path("acunetix") / "many_findings.xml"
2609+
2610+
# Step 1: import 4 findings into an existing test (test1) in the engagement.
2611+
# minimum_severity="Info" is required to include all 4 findings in the file.
2612+
import1 = self.import_scan_with_params(
2613+
acunetix_many_findings,
2614+
scan_type=self.scan_type_acunetix,
2615+
engagement=engagement.id,
2616+
minimum_severity="Info",
2617+
)
2618+
test1_id = import1["test"]
2619+
self.assert_finding_count_json(4, self.get_test_findings_api(test1_id, active=True))
2620+
2621+
# Step 2: call the reimport endpoint with auto_create_context=True and a
2622+
# different test_title so a new test is created. close_old_findings=True
2623+
# is the value a caller would pass (and the reimport default); the serializer
2624+
# must suppress it when auto-creating a new test. The scan uses a different
2625+
# file so its hash codes don't overlap with test1's findings, meaning the
2626+
# bug would close all 4 of test1's findings if the fix were reverted.
2627+
self.reimport_scan_with_params(
2628+
None,
2629+
self.acunetix_file_name,
2630+
scan_type=self.scan_type_acunetix,
2631+
test_title="Brand New Test From Reimport",
2632+
product_name="P CloseOld AutoCreate",
2633+
engagement_name="E CloseOld AutoCreate",
2634+
product_type_name="PT CloseOld AutoCreate",
2635+
auto_create_context=True,
2636+
close_old_findings=True,
2637+
)
2638+
2639+
# Step 3: test1's 4 findings must all still be active
2640+
self.assert_finding_count_json(4, self.get_test_findings_api(test1_id, active=True))
2641+
25852642
@override_settings(
25862643
IMPORT_REIMPORT_DEDUPE_BATCH_SIZE=200,
25872644
IMPORT_REIMPORT_MATCH_BATCH_SIZE=200,

0 commit comments

Comments
 (0)