Skip to content

Commit bcccb03

Browse files
authored
Merge pull request #2117 from aboutcode-org/2116-fix-advv2-serialization
Include PackageCommitPatch and Patch in AdvisoryV2 serialization
2 parents 0318583 + ab2ff03 commit bcccb03

File tree

4 files changed

+79
-10
lines changed

4 files changed

+79
-10
lines changed

docs/source/contributing.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ overlooked. We value any suggestions to improve
7373

7474
.. tip::
7575
Our documentation is treated like code. Make sure to check our
76-
`writing guidelines <https://scancode-toolkit.readthedocs.io/en/stable/contribute/contrib_doc.html>`_
76+
`writing guidelines <https://scancode-toolkit.readthedocs.io/en/stable/getting-started/contribute/contributing-docs.html>`_
7777
to help guide new users.
7878

7979
Other Ways
@@ -87,7 +87,7 @@ questions, and interact with us and other community members on
8787
Helpful Resources
8888
-----------------
8989

90-
- Review our `comprehensive guide <https://scancode-toolkit.readthedocs.io/en/stable/contribute/index.html>`_
90+
- Review our `comprehensive guide <https://scancode-toolkit.readthedocs.io/en/stable/getting-started/contribute/index.html>`_
9191
for more details on how to add quality contributions to our codebase and documentation
9292
- Check this free resource on `How to contribute to an open source project on github <https://egghead.io/lessons/javascript-identifying-how-to-contribute-to-an-open-source-project-on-github>`_
9393
- Follow `this wiki page <https://aboutcode.readthedocs.io/en/latest/contributing/writing_good_commit_messages.html>`_

vulnerabilities/importer.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,24 +512,30 @@ def from_dict(cls, affected_pkg: dict):
512512
fixed_version_range = None
513513
affected_range = affected_pkg["affected_version_range"]
514514
fixed_range = affected_pkg["fixed_version_range"]
515-
introduced_by_commit_patches = (
516-
affected_pkg.get("introduced_by_package_commit_patches") or []
517-
)
518-
fixed_by_commit_patches = affected_pkg.get("fixed_by_package_commit_patches") or []
515+
introduced_by_commit_patches = affected_pkg.get("introduced_by_commit_patches") or []
516+
fixed_by_commit_patches = affected_pkg.get("fixed_by_commit_patches") or []
519517

520518
try:
521-
affected_version_range = VersionRange.from_string(affected_range)
522-
fixed_version_range = VersionRange.from_string(fixed_range)
519+
affected_version_range = (
520+
VersionRange.from_string(affected_range) if affected_range else None
521+
)
522+
fixed_version_range = VersionRange.from_string(fixed_range) if fixed_range else None
523523
except:
524524
tb = traceback.format_exc()
525525
logger.error(
526526
f"Cannot create AffectedPackage with invalid or unknown range: {affected_pkg!r} with error: {tb!r}"
527527
)
528528
return
529529

530-
if not fixed_version_range and not affected_version_range:
530+
if (
531+
not fixed_version_range
532+
and not affected_version_range
533+
and not introduced_by_commit_patches
534+
and not fixed_by_commit_patches
535+
):
531536
logger.error(
532-
f"Cannot create AffectedPackage without fixed or affected range: {affected_pkg!r}"
537+
f"Cannot create an AffectedPackage for: {affected_pkg!r}, at least one of the following must be provided: "
538+
"a fixed version range, an affected version range, introduced commit patches, or fixed commit patches"
533539
)
534540
return
535541

vulnerabilities/models.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,6 +2796,19 @@ class Meta:
27962796
)
27972797
]
27982798

2799+
def to_dict(self):
2800+
return {
2801+
"patch_url": self.patch_url,
2802+
"patch_text": self.patch_text,
2803+
"patch_checksum": self.patch_checksum,
2804+
}
2805+
2806+
def to_patch_data(self):
2807+
"""Return `PatchData` from the Patch."""
2808+
from vulnerabilities.importer import PatchData
2809+
2810+
return PatchData.from_dict(self.to_dict())
2811+
27992812

28002813
class PackageCommitPatch(models.Model):
28012814
"""
@@ -2823,6 +2836,14 @@ def save(self, *args, **kwargs):
28232836
class Meta:
28242837
unique_together = ["commit_hash", "vcs_url"]
28252838

2839+
def to_dict(self):
2840+
return {
2841+
"vcs_url": self.vcs_url,
2842+
"commit_hash": self.commit_hash,
2843+
"patch_text": self.patch_text,
2844+
"patch_checksum": self.patch_checksum,
2845+
}
2846+
28262847

28272848
class AdvisoryV2QuerySet(BaseQuerySet):
28282849
def latest_for_avid(self, avid: str):
@@ -3016,6 +3037,7 @@ def to_advisory_data(self) -> "AdvisoryData":
30163037
impacted.to_affected_package_data() for impacted in self.impacted_packages.all()
30173038
],
30183039
references_v2=[ref.to_reference_v2_data() for ref in self.references.all()],
3040+
patches=[patch.to_patch_data() for patch in self.patches.all()],
30193041
date_published=self.date_published,
30203042
weaknesses=[weak.cwe_id for weak in self.weaknesses.all()],
30213043
severities=[sev.to_vulnerability_severity_data() for sev in self.severities.all()],
@@ -3099,6 +3121,12 @@ def to_dict(self):
30993121
"package": purl_to_dict(self.base_purl),
31003122
"affected_version_range": self.affecting_vers,
31013123
"fixed_version_range": self.fixed_vers,
3124+
"introduced_by_commit_patches": [
3125+
commit.to_dict() for commit in self.introduced_by_package_commit_patches.all()
3126+
],
3127+
"fixed_by_commit_patches": [
3128+
commit.to_dict() for commit in self.fixed_by_package_commit_patches.all()
3129+
],
31023130
}
31033131

31043132
def to_affected_package_data(self):

vulnerabilities/tests/test_models.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
from vulnerabilities import models
2525
from vulnerabilities.importer import AdvisoryData
2626
from vulnerabilities.importer import AffectedPackage
27+
from vulnerabilities.importer import AffectedPackageV2
28+
from vulnerabilities.importer import PackageCommitPatchData
29+
from vulnerabilities.importer import PatchData
2730
from vulnerabilities.importer import Reference
31+
from vulnerabilities.importer import ReferenceV2
2832
from vulnerabilities.models import AdvisorySeverity
2933
from vulnerabilities.models import Alias
3034
from vulnerabilities.models import Package
@@ -735,3 +739,34 @@ def test_constraint_none(self):
735739
scoring_system=CVSSV4,
736740
scoring_elements="CVSS:4.0/AV:P/AC:H/AT:P/PR:H/UI:A/VC:L/VI:L/VA:H/SC:H/SI:L/SA:L/E:A/CR:M/IR:M/AR:M/MAV:A/MAC:L/MAT:P/MPR:L/MVC:L/MVI:L/MVA:L/MSC:H/MSI:H/MSA:H/S:P/AU:Y/R:U/V:C/RE:M/U:Amber",
737741
)
742+
743+
744+
class TestAdvisoryV2Model(DjangoTestCase):
745+
def setUp(self):
746+
self.advisoryv2_data1 = AdvisoryData(
747+
advisory_id="test_adv",
748+
aliases=[],
749+
summary="vulnerability description here",
750+
affected_packages=[
751+
AffectedPackageV2(
752+
package=PackageURL(type="pypi", name="dummy"),
753+
affected_version_range=VersionRange.from_string("vers:pypi/>=1.0.0|<=2.0.0"),
754+
introduced_by_commit_patches=[
755+
PackageCommitPatchData(
756+
vcs_url="http://foo.bar/", commit_hash="c4eab154606e801"
757+
)
758+
],
759+
)
760+
],
761+
references_v2=[ReferenceV2(url="https://example.com/with/more/info/CVE-2020-13371337")],
762+
patches=[PatchData(patch_url="https://foo.bar/", patch_text="test patch")],
763+
url="https://test.com",
764+
)
765+
766+
def test_advisoryv2_to_advisory_data_patch_seralization(self):
767+
from vulnerabilities.pipes.advisory import insert_advisory_v2
768+
769+
insert_advisory_v2(advisory=self.advisoryv2_data1, pipeline_id="test_pipeline")
770+
result = models.AdvisoryV2.objects.first().to_advisory_data()
771+
772+
self.assertEqual(result, self.advisoryv2_data1)

0 commit comments

Comments
 (0)