Skip to content
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
69 changes: 33 additions & 36 deletions dojo/tools/jfrogxray/parser.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
import json
from cvss import CVSS3
import re
from dojo.models import Finding


class XrayJSONParser(object):
"""JFrog Xray JSON reports"""
def __init__(self, json_output, test):
# Start with an empty findings
self.items = []
# Exit if file is not provided
if json_output is None:
return

tree = self.parse_json(json_output)

if tree:
self.items = [data for data in self.get_items(tree, test)]
else:
self.items = []

def parse_json(self, json_output):
try:
data = json_output.read()
try:
tree = json.loads(str(data, 'utf-8'))
except:
tree = json.loads(data)
except:
raise Exception("Invalid format")

return tree
tree = json.load(json_output)
self.items = [data for data in self.get_items(tree, test)]

def get_items(self, tree, test):
items = {}
Expand All @@ -46,6 +38,13 @@ def get_items(self, tree, test):
return list(items.values())


def decode_cwe_number(value):
match = re.match(r"CWE-\d+", value, re.IGNORECASE)
if match is None:
return 0
return int(match[0].rsplit('-')[1])


def get_item(vulnerability, test):
# Following the CVSS Scoring per https://nvd.nist.gov/vuln-metrics/cvss
if 'severity' in vulnerability:
Expand All @@ -59,28 +58,25 @@ def get_item(vulnerability, test):

cve = "No CVE on file"
cwe = 0
cvssv3 = None
Comment thread
madchap marked this conversation as resolved.
Resolved
cvss_v3 = "No CVSS v3 score."
cvss_v2 = "No CVSS v2 score."
mitigation = "N/A"
extra_desc = ""
# Some entries have no CVE entries, despite they exist. Example CVE-2017-1000502.
if 'cves' in vulnerability['component_versions']['more_details']:
if 'cve' in vulnerability['component_versions']['more_details']['cves'][0]:
cve = vulnerability['component_versions']['more_details']['cves'][0]['cve']

if 'cwe' in vulnerability['component_versions']['more_details']['cves'][0]:
# take only the first one for now, limitation of DD model.
cwe = vulnerability['component_versions']['more_details']['cves'][0]['cwe'][0].split('-')[1]

# some can be "NVD-CWE-noinfo"
if not type(cwe) is int:
cwe = 0

if 'cvss_v3' in vulnerability['component_versions']['more_details']['cves'][0]:
cvss_v3 = vulnerability['component_versions']['more_details']['cves'][0]['cvss_v3']

if 'cvss_v2' in vulnerability['component_versions']['more_details']['cves'][0]:
cvss_v2 = vulnerability['component_versions']['more_details']['cves'][0]['cvss_v2']
cves = vulnerability['component_versions']['more_details'].get('cves', [])
Comment thread
damiencarol marked this conversation as resolved.
Resolved
if len(cves) > 0:
if 'cve' in cves[0]:
cve = cves[0]['cve']
# take only the first one for now, limitation of DD model.
if len(cves[0].get('cwe', [])) > 0:
cwe = decode_cwe_number(cves[0].get('cwe', [])[0])
if 'cvss_v3' in cves[0]:
cvss_v3 = cves[0]['cvss_v3']
# this dedicated package will clean the vector
cvssv3 = CVSS3.from_rh_vector(cvss_v3).clean_vector(output_prefix=False)
if 'cvss_v2' in cves[0]:
cvss_v2 = cves[0]['cvss_v2']

if 'fixed_versions' in vulnerability['component_versions']:
mitigation = "Versions containing a fix:\n"
Expand Down Expand Up @@ -114,6 +110,7 @@ def get_item(vulnerability, test):
static_finding=True,
dynamic_finding=False,
references=vulnerability.get('component_versions').get('more_details').get('provider'),
impact=severity)
impact=severity,
cvssv3=cvssv3)

return finding
62 changes: 62 additions & 0 deletions dojo/unittests/scans/jfrogxray/many_vulns2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"total_count": 2,
"data": [
{
"id": "",
"severity": "High",
"summary": "pip PyPI (Python Packaging Index) PipXmlrpcTransport._download_http_url() Function Content-Disposition Header Path Traversal Arbitrary File Write Weakness",
"issue_type": "security",
"provider": "JFrog",
"component": "pip",
"source_id": "pypi://pip",
"source_comp_id": "pypi://pip:9.0.1",
"component_versions": {
"more_details": {
"cves": [
{
"cvss_v2": "7.6/AV:N/AC:H/Au:N/C:C/I:C/A:C",
"cvss_v3": "7.5/CVSS:3.0/AV:N/AC:H/PR:N/UI:R/S:U/C:H/I:H/A:H"
}
],
"provider": "JFrog"
}
},
"edited": "2020-11-04T19:26:15Z",
"is_source_root": false
},
{
"id": "",
"severity": "High",
"summary": "A flaw was found in the Linux kernel before 5.9-rc4. Memory corruption can be exploited to gain root privileges from unprivileged processes. The highest threat from this vulnerability is to data confidentiality and integrity.",
"issue_type": "security",
"provider": "JFrog",
"component": "ubuntu:bionic:linux",
"source_id": "deb://ubuntu:bionic:linux",
"source_comp_id": "deb://ubuntu:bionic:linux:4.15.0-88.88",
"component_versions": {
"id": "ubuntu:bionic:linux",
"vulnerable_versions": [
"< 4.15.0-117.118"
],
"fixed_versions": [
"≥ 4.15.0-117.118"
],
"more_details": {
"cves": [
{
"cve": "CVE-2020-14386",
"cwe": [
"CWE-787"
],
"cvss_v2": "7.2/CVSS:2.0/AV:L/AC:L/Au:N/C:C/I:C/A:C",
"cvss_v3": "7.8/CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H"
}
],
"provider": "JFrog"
}
},
"edited": "2020-12-29T10:08:57Z",
"is_source_root": false
}
]
}
31 changes: 30 additions & 1 deletion dojo/unittests/test_jfrogxray_parser.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from django.test import TestCase
from dojo.models import Test
from dojo.tools.jfrogxray.parser import XrayJSONParser
from dojo.tools.jfrogxray.parser import XrayJSONParser, decode_cwe_number


class TestJfrogXrayJSONParser(TestCase):
def test_parse_without_file_has_no_finding(self):
parser = XrayJSONParser(None, Test())
self.assertEqual(0, len(parser.items))

def test_parse_file_with_one_vuln(self):
testfile = open("dojo/unittests/scans/jfrogxray/one_vuln.json")
parser = XrayJSONParser(testfile, Test())
Expand All @@ -12,9 +16,34 @@ def test_parse_file_with_one_vuln(self):
item = parser.items[0]
self.assertEquals('debian:stretch:libx11', item.component_name)
self.assertEquals('2:1.6.4-3', item.component_version)
self.assertEquals('CVE-2018-14600', item.cve)
self.assertEquals(787, item.cwe)

def test_parse_file_with_many_vulns(self):
testfile = open("dojo/unittests/scans/jfrogxray/many_vulns.json")
parser = XrayJSONParser(testfile, Test())
testfile.close()
self.assertEqual(3, len(parser.items))

def test_parse_file_with_many_vulns2(self):
testfile = open("dojo/unittests/scans/jfrogxray/many_vulns2.json")
parser = XrayJSONParser(testfile, Test())
testfile.close()
self.assertEqual(2, len(parser.items))
item = parser.items[0]
self.assertEquals('pip', item.component_name)
self.assertEquals('9.0.1', item.component_version)
item = parser.items[1]
self.assertEquals('ubuntu:bionic:linux', item.component_name)
self.assertEquals('4.15.0-88.88', item.component_version)
self.assertEquals('CVE-2020-14386', item.cve)
self.assertEquals(787, item.cwe)
self.assertEquals("AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H", item.cvssv3)

def test_decode_cwe_number(self):
with self.subTest(val="CWE-1234"):
self.assertEquals(1234, decode_cwe_number("CWE-1234"))
with self.subTest(val=""):
self.assertEquals(0, decode_cwe_number(""))
with self.subTest(val="cwe-1"):
self.assertEquals(1, decode_cwe_number("cwe-1"))
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ vcrpy==4.1.1
vcrpy-unittest==0.1.7
git+https://github.com/radiac/django-tagulous.git@develop#egg=django-tagulous
PyJWT==1.7.1
cvss==2.1