Skip to content

Commit 6b1b5e5

Browse files
cvss4: remove no longer needed custom parsing (#13037)
* cvss4: remove no longer needed custom parsing * cvss4: remove no longer needed custom parsing
1 parent 9adaaa3 commit 6b1b5e5

File tree

3 files changed

+7
-45
lines changed

3 files changed

+7
-45
lines changed

docs/content/en/open_source/contributing/how-to-write-a-parser.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ Example of use:
197197
from cvss import CVSS2, CVSS3, CVSS4
198198

199199
# TEMPORARY: Use Defect Dojo implementation of `parse_cvss_from_text` white waiting for https://github.com/RedHatProductSecurity/cvss/pull/75 to be released
200-
vectors = dojo.utils.parse_cvss_from_text("CVSS:3.0/S:C/C:H/I:H/A:N/AV:P/AC:H/PR:H/UI:R/E:H/RL:O/RC:R/CR:H/IR:X/AR:X/MAC:H/MPR:X/MUI:X/MC:L/MA:X")
200+
vectors = cvss.parser.parse_cvss_from_text("CVSS:3.0/S:C/C:H/I:H/A:N/AV:P/AC:H/PR:H/UI:R/E:H/RL:O/RC:R/CR:H/IR:X/AR:X/MAC:H/MPR:X/MUI:X/MC:L/MA:X")
201201
if len(vectors) > 0 and type(vectors[0]) is CVSS3:
202202
print(vectors[0].severities()) # this is the 3 severities
203203

dojo/utils.py

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515

1616
import bleach
1717
import crum
18+
import cvss
1819
import hyperlink
1920
import vobject
2021
from asteval import Interpreter
2122
from auditlog.models import LogEntry
2223
from cryptography.hazmat.backends import default_backend
2324
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
24-
from cvss import CVSS2, CVSS3, CVSS4, CVSSError
25+
from cvss import CVSS2, CVSS3, CVSS4
2526
from dateutil.parser import parse
2627
from dateutil.relativedelta import MO, SU, relativedelta
2728
from django.conf import settings
@@ -2660,49 +2661,11 @@ def generate_file_response_from_file_path(
26602661
return response
26612662

26622663

2663-
# TEMPORARY: Local implementation until the upstream PR is merged & released: https://github.com/RedHatProductSecurity/cvss/pull/75
2664-
def parse_cvss_from_text(text):
2665-
"""
2666-
Parses CVSS2, CVSS3, and CVSS4 vectors from arbitrary text and returns a list of CVSS objects.
2667-
2668-
Parses text for substrings that look similar to CVSS vector
2669-
and feeds these matches to CVSS constructor.
2670-
2671-
Args:
2672-
text (str): arbitrary text
2673-
2674-
Returns:
2675-
A list of CVSS objects.
2676-
2677-
"""
2678-
# Looks for substrings that resemble CVSS2, CVSS3, or CVSS4 vectors.
2679-
# CVSS3 and CVSS4 vectors start with a 'CVSS:x.x/' prefix and are matched by the optional non-capturing group.
2680-
# CVSS2 vectors do not include a prefix and are matched by raw vector pattern only.
2681-
# Minimum total match length is 26 characters to reduce false positives.
2682-
matches = re.compile(r"(?:CVSS:[3-4]\.\d/)?[A-Za-z:/]{26,}").findall(text)
2683-
2684-
cvsss = set()
2685-
for match in matches:
2686-
try:
2687-
if match.startswith("CVSS:4."):
2688-
cvss = CVSS4(match)
2689-
elif match.startswith("CVSS:3."):
2690-
cvss = CVSS3(match)
2691-
else:
2692-
cvss = CVSS2(match)
2693-
2694-
cvsss.add(cvss)
2695-
except (CVSSError, KeyError):
2696-
pass
2697-
2698-
return list(cvsss)
2699-
2700-
27012664
def parse_cvss_data(cvss_vector_string: str) -> dict:
27022665
if not cvss_vector_string:
27032666
return {}
27042667

2705-
vectors = parse_cvss_from_text(cvss_vector_string)
2668+
vectors = cvss.parser.parse_cvss_from_text(cvss_vector_string)
27062669
if len(vectors) > 0:
27072670
vector = vectors[0]
27082671
# For CVSS2, environmental score is at index 2

dojo/validators.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
from collections.abc import Callable
44

5+
import cvss
56
from cvss import CVSS2, CVSS3, CVSS4
67
from django.core.exceptions import ValidationError
78
from django.core.validators import FileExtensionValidator
@@ -49,8 +50,7 @@ def clean_tags(value: str | list[str], exception_class: Callable = ValidationErr
4950

5051
def cvss3_validator(value: str | list[str], exception_class: Callable = ValidationError) -> None:
5152
logger.debug("cvss3_validator called with value: %s", value)
52-
from dojo.utils import parse_cvss_from_text
53-
cvss_vectors = parse_cvss_from_text(value)
53+
cvss_vectors = cvss.parser.parse_cvss_from_text(value)
5454
if len(cvss_vectors) > 0:
5555
vector_obj = cvss_vectors[0]
5656

@@ -76,8 +76,7 @@ def cvss3_validator(value: str | list[str], exception_class: Callable = Validati
7676

7777
def cvss4_validator(value: str | list[str], exception_class: Callable = ValidationError) -> None:
7878
logger.debug("cvss4_validator called with value: %s", value)
79-
from dojo.utils import parse_cvss_from_text
80-
cvss_vectors = parse_cvss_from_text(value)
79+
cvss_vectors = cvss.parser.parse_cvss_from_text(value)
8180
if len(cvss_vectors) > 0:
8281
vector_obj = cvss_vectors[0]
8382

0 commit comments

Comments
 (0)