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

fix: improve oval v2 parsing to prevent unnecessary exceptions #410

Merged
merged 1 commit into from
Nov 29, 2023
Merged
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
17 changes: 10 additions & 7 deletions src/vunnel/utils/oval_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ def _parse_criteria(xml_element: ET.Element, oval_ns: str, config: OVALParserCon
if not criteria_element:
return results

if criteria_element.attrib["operator"].lower() == "or":
operator = criteria_element.attrib.get("operator")

if operator and operator.lower() == "or":
# indicates multiple groups of impacted artifacts, parse each group and gather results
for child in criteria_element:
results.extend(VulnerabilityParser._parse_group(child, config))
Expand Down Expand Up @@ -222,15 +224,16 @@ def _parse_sub_group(crit_element: ET.Element, config: OVALParserConfig, regex:
test_ids = []
crit_tag = OVALElementParser._find_with_regex(crit_element.tag, config.tag_regex) # noqa: SLF001

if crit_tag == "criterion":
if crit_tag == "criterion" and "comment" in crit_element.attrib:
regex_match = re.search(regex, crit_element.attrib["comment"])
if regex_match and crit_element.attrib["test_ref"]:
if regex_match and "test_ref" in crit_element.attrib:
test_ids.append(crit_element.attrib["test_ref"])
elif crit_tag == "criteria":
for criterion in crit_element:
regex_match = re.search(regex, criterion.attrib["comment"])
if regex_match and criterion.attrib["test_ref"]:
test_ids.append(criterion.attrib["test_ref"])
if "comment" in criterion.attrib:
regex_match = re.search(regex, criterion.attrib["comment"])
if regex_match and criterion.attrib.get("test_ref"):
test_ids.append(criterion.attrib["test_ref"])

return test_ids

Expand Down Expand Up @@ -336,7 +339,7 @@ def parse(xml_element: ET.Element, config: OVALParserConfig) -> Version | None:
identity = xml_element.attrib["id"]
for child in xml_element:
child_tag = OVALElementParser._find_with_regex(child.tag, config.tag_regex) # noqa: SLF001
if child_tag in ["version", "evr"]:
if child_tag in ["version", "evr"] and "operation" in child.attrib:
op = child.attrib["operation"]
value = child.text
break
Expand Down
Loading