-
Notifications
You must be signed in to change notification settings - Fork 1.9k
🐛 fix close_old_findings through service field in several parsers #14640 #14687
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
manuel-sommer
wants to merge
11
commits into
DefectDojo:dev
Choose a base branch
from
manuel-sommer:issue_14640_closeoldfindings
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5a47ed8
:bug: fix close_old_findings field in several parsers #14640
manuel-sommer 10a89e4
update
manuel-sommer 4d518a6
udpate
manuel-sommer 59916fb
update
manuel-sommer ec67f25
update
manuel-sommer efe2b2c
update
manuel-sommer 52a4850
added unittest
manuel-sommer 02ee542
added release notes
manuel-sommer c8b82a5
added db_migration for parsers
manuel-sommer 873bda2
fix
manuel-sommer a4ba402
Merge branch 'dev' into issue_14640_closeoldfindings
manuel-sommer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --- | ||
| title: 'Upgrading to DefectDojo Version 2.58.x' | ||
| toc_hide: true | ||
| weight: -20260504 | ||
| description: 'Breaking change: parsers no longer set Finding.service directly' | ||
| --- | ||
|
|
||
| ## Breaking Change: Parsers No Longer Set `Finding.service` | ||
|
|
||
| Starting with DefectDojo 2.58.x, parsers no longer set the `service` field directly on findings. | ||
|
|
||
| ### Why this is a breaking change | ||
|
|
||
| Whenever parsers set the `service` field on findings, this breaks `close_old_findings` functionality. | ||
|
|
||
| The reason is that import and reimport differ in a way that import uses the `service` field, however reimport does not include a `service` value. The `close_old_findings` feature only closes findings that match the service value provided in the request. As a result, findings with a non-empty parser-populated service value are not closed. | ||
|
|
||
| Also, if the application name changes, findings in the reimport report are no longer matched against existing findings. | ||
|
|
||
| ### Required actions | ||
|
|
||
| - If your integrations relied on parser-populated `service` field, update your workflow to pass service explicitly at import/reimport time when needed. | ||
| - Review automation that depends on `close_old_findings` behavior and verify expected closure scope after upgrading. | ||
|
|
||
| For more information, check the [Release Notes](https://github.com/DefectDojo/django-DefectDojo/releases/tag/2.58.0). |
95 changes: 95 additions & 0 deletions
95
dojo/db_migrations/0264_clear_service_for_affected_parsers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import logging | ||
|
|
||
| from django.db import migrations | ||
| from django.db.models import Q | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| AFFECTED_PARSER_SCAN_TYPES = [ | ||
| "Trivy Scan", | ||
| "Trivy Operator Scan", | ||
| "Hydra Scan", | ||
| "JFrog Xray API Summary Artifact Scan", | ||
| "Orca Security Alerts", | ||
| "OpenReports", | ||
| "StackHawk HawkScan", | ||
| ] | ||
|
|
||
|
|
||
| def clear_service_and_rehash_findings(apps, schema_editor): | ||
| """ | ||
| Clear parser-populated service values for affected parser scan types and | ||
| recompute hash_code. | ||
|
|
||
| This migration only touches findings where: | ||
| - the finding belongs to an affected parser by test_type or scan_type | ||
| - service is set (not NULL and not empty) | ||
| """ | ||
| historical_finding = apps.get_model("dojo", "Finding") | ||
|
|
||
| affected_ids = set() | ||
| for scan_type in AFFECTED_PARSER_SCAN_TYPES: | ||
| findings = ( | ||
| historical_finding.objects | ||
| .filter( | ||
| Q(test__test_type__name=scan_type) | ||
| | Q(test__scan_type=scan_type), | ||
| ) | ||
| .exclude(service__isnull=True) | ||
| .exclude(service="") | ||
| ) | ||
| count = findings.count() | ||
| if count: | ||
| logger.warning( | ||
| "Identified %d findings with parser-populated service for scan type '%s'", | ||
| count, | ||
| scan_type, | ||
| ) | ||
| affected_ids.update(findings.values_list("id", flat=True)) | ||
|
|
||
| if not affected_ids: | ||
| logger.warning("No findings found for parser service cleanup migration") | ||
| return | ||
|
|
||
| # Use live model here to access compute_hash_code() and save() behavior. | ||
| from dojo.models import Finding # noqa: PLC0415 | ||
|
|
||
| migrated = 0 | ||
| for finding in ( | ||
| Finding.objects | ||
| .filter(id__in=affected_ids) | ||
| .select_related("test", "test__test_type") | ||
| .iterator(chunk_size=200) | ||
| ): | ||
| finding.service = None | ||
| finding.hash_code = finding.compute_hash_code() | ||
| finding.save( | ||
| dedupe_option=False, | ||
| rules_option=False, | ||
| product_grading_option=False, | ||
| issue_updater_option=False, | ||
| push_to_jira=False, | ||
| ) | ||
| migrated += 1 | ||
|
|
||
| logger.warning( | ||
| "Parser service cleanup migration updated %d findings (service cleared, hash_code recomputed)", | ||
| migrated, | ||
| ) | ||
|
|
||
|
|
||
| def noop_reverse(apps, schema_editor): | ||
| # Intentionally irreversible: previous parser-populated service values are not recoverable. | ||
| pass | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("dojo", "0263_language_type_unique_language"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython(clear_service_and_rehash_findings, noop_reverse), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If a service was supplied to these test types at import time, does that value override the value set by the parser? If so, could this migration potentially erase service fields that are from the user?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a question beyond my knowledge. Maybe @valentijnscholten has an opinion on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The importers overwrite any fields set by the parsers:
django-DefectDojo/dojo/importers/default_importer.py
Lines 216 to 217 in 9d661d7
django-DefectDojo/dojo/importers/default_reimporter.py
Lines 337 to 338 in 9d661d7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it seems like this migration would erase data intentionally set by users...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a possibility to distinguish between the two ways?