Skip to content

Commit

Permalink
Merge pull request #210 from multiflexi/link_deduplication
Browse files Browse the repository at this point in the history
Link deduplication in product
  • Loading branch information
Progress1 authored Mar 7, 2024
2 parents 8fd28c9 + 1ccd64c commit dc08d4a
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/presenters/presenters/base_presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Returns:
_description_
"""

from shared.schema.presenter import PresenterSchema
from managers import log_manager
import json
Expand Down Expand Up @@ -143,31 +144,38 @@ def get_max_tlp(self, reports):
"""
color_values = {"WHITE": 0, "CLEAR": 1, "GREEN": 2, "AMBER": 3, "AMBER+STRICT": 4, "RED": 5}
colors = []

for report in reports:
if report.type == "Vulnerability Report":
if report.type.startswith("Vulnerability Report"):
colors.append(report.attrs.tlp)

max_tlp = max(colors, key=lambda color: color_values.get(color, 0))
if not max_tlp:
if colors:
max_tlp = max(colors, key=lambda color: color_values.get(color, 1))
else:
max_tlp = "CLEAR"
return max_tlp

def add_link_prefix(self, report, letter):
"""Add link prefix to the report description and recommendations.
def link_renumbering(self, text, report_links, product_links):
"""Replace the numbers enclosed in brackets in the given text with the corresponding indices from product_links.
Arguments:
report -- report to add link prefix to
letter -- letter to use as a link prefix
text (str): The text in which the numbers enclosed in brackets will be replaced.
report_links (list): The list of report links.
product_links (list): The list of product links.
Returns:
description, recommendations: report description and recommendations with link prefix added
str: The updated text with the numbers enclosed in brackets replaced by the corresponding indices from product_links.
"""
pattern = r"\[(\d+)\]"
description = re.sub(pattern, lambda match: f"[{letter}{match.group(1)}]", report.attrs.description)
recommendations = re.sub(pattern, lambda match: f"[{letter}{match.group(1)}]", report.attrs.recommendations)

return description, recommendations
# Create a mapping from old indices to new indices
mapping = {old_index + 1: product_links.index(item) + 1 for old_index, item in enumerate(report_links)}

# Use a regular expression to find all instances of numbers enclosed in brackets
def replace_match(match):
old_index = int(match.group(1))
new_index = mapping.get(old_index, old_index) # Use the old index as a fallback
return f"[{new_index}]"

return re.sub(pattern, replace_match, text)

def __init__(self, presenter_input):
"""Initialize the object.
Expand Down Expand Up @@ -195,16 +203,22 @@ def __init__(self, presenter_input):
for report in presenter_input.reports:
self.report_items.append(BasePresenter.ReportItemObject(report, report_types, attribute_map))

letter = "A"
vul_report_count = 0
product_links = []
# If there are multiple vulnerability reports, we need to renumber the links
for report in self.report_items:
if report.type == "Vulnerability Report":
report.attrs.description, report.attrs.recommendations = self.add_link_prefix(report, letter)
report.attrs.link_prefix = letter
letter = chr(ord(letter) + 1)
if report.type.startswith("Vulnerability Report"):
vul_report_count += 1
if report.attrs.links:
for link in report.attrs.links:
if link not in product_links:
product_links.append(link)
report.attrs.description = self.link_renumbering(report.attrs.description, report.attrs.links, product_links)
report.attrs.recommendations = self.link_renumbering(report.attrs.recommendations, report.attrs.links, product_links)
# If there are vulnerability reports, set the max TLP and product links
if vul_report_count > 0:
self.product.max_tlp = self.get_max_tlp(self.report_items)
self.product.links = product_links

def get_info(self):
"""Get info about the presenter.
Expand Down

0 comments on commit dc08d4a

Please sign in to comment.