Skip to content
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

Add migrations to remove corrupted advisories #1086 #1087

Merged
merged 3 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# VulnerableCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/vulnerablecode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#

from django.db import migrations

from vulnerabilities.severity_systems import SCORING_SYSTEMS

class Migration(migrations.Migration):

def remove_advisories(apps, schema_editor):
Advisory = apps.get_model("vulnerabilities", "Advisory")
deletables = []
for advisory in Advisory.objects.iterator(chunk_size=1000):
for ref in advisory.references:
if not ref["url"]:
deletables.append(advisory.pk)
break
for sev in ref["severities"]:
if sev["system"] not in SCORING_SYSTEMS:
deletables.append(advisory.pk)
break
Advisory.objects.filter(pk__in=deletables).delete()

dependencies = [
("vulnerabilities", "0037_advisory_weaknesses_weakness"),
]

operations = [
migrations.RunPython(remove_advisories, reverse_code=migrations.RunPython.noop),
]
35 changes: 35 additions & 0 deletions vulnerabilities/tests/test_data_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,38 @@ def test_merge_rows(self):
]

assert severities == expected


class RemoveCorrupteAdvisories(TestMigrations):
app_name = "vulnerabilities"
migrate_from = "0037_advisory_weaknesses_weakness"
migrate_to = "0038_remove_corrupted_advisories_with_incorrect_refs_and_severity"

def setUpBeforeMigration(self, apps):
# using get_model to avoid circular import
Advisory = apps.get_model("vulnerabilities", "Advisory")

corrupted_advisory = Advisory.objects.create(
aliases=["CVE-2020-1234"],
summary="Corrupted advisory",
references=[
{
"reference_id": "cpe:2.3:a:f5:nginx:1.16.1:*:*:*:*:*:*:*",
"url": "",
"severity": [
{
"scoring_system": "cvssv3_vector",
"value": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
}
],
}
],
date_collected="2020-01-01",
date_published="2020-01-01",
)
corrupted_advisory.save()

def test_removal_of_corrupted_advisory(self):
# using get_model to avoid circular import
Advisory = self.apps.get_model("vulnerabilities", "Advisory")
Advisory.objects.all().count() == 0