Skip to content

Commit 780892a

Browse files
feat: improved sbom filename extension handling codeql fix
1 parent e84e40f commit 780892a

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

cve_bin_tool/sbom_manager/sbom_detection.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import json
55
from typing import Optional
6+
from urllib.parse import urlparse
67

78
import defusedxml.ElementTree as ET
89

@@ -51,15 +52,22 @@ def sbom_detection(file_path: str) -> Optional[str]:
5152
try:
5253
tree = ET.parse(file_path)
5354
root = tree.getroot()
54-
namespace = (
55+
namespace_uri = (
5556
root.tag.split("}", 1)[0].strip("{") if "}" in root.tag else ""
5657
)
5758

5859
# Check CycloneDX namespace
59-
if "cyclonedx.org" in namespace and validate_cyclonedx(file_path):
60+
parsed_uri = urlparse(namespace_uri)
61+
domain = parsed_uri.netloc.lower()
62+
if (
63+
domain == "cyclonedx.org" or domain.endswith(".cyclonedx.org")
64+
) and validate_cyclonedx(file_path):
6065
return "cyclonedx"
6166
# Check SWID by root tag and namespace
62-
elif root.tag.endswith("SoftwareIdentity") and "iso/19770" in namespace:
67+
elif (
68+
root.tag.endswith("SoftwareIdentity")
69+
and "iso/19770" in namespace_uri
70+
):
6371
return "swid"
6472
except ET.ParseError as e:
6573
LOGGER.debug(f"XML parsing error for {file_path}: {str(e)}")
@@ -109,15 +117,24 @@ def detect_sbom_type_from_content(file_path: str) -> Optional[str]:
109117
try:
110118
tree = ET.parse(file_path)
111119
root = tree.getroot()
112-
namespace = (
120+
namespace_uri = (
113121
root.tag.split("}", 1)[0].strip("{") if "}" in root.tag else ""
114122
)
115123

116-
if "cyclonedx.org" in namespace:
124+
# Check CycloneDX namespace
125+
parsed_cyclonedx_uri = urlparse(namespace_uri)
126+
cyclonedx_domain = parsed_cyclonedx_uri.netloc.lower()
127+
if cyclonedx_domain == "cyclonedx.org" or cyclonedx_domain.endswith(
128+
".cyclonedx.org"
129+
):
117130
return "cyclonedx"
118-
elif "iso/19770" in namespace:
131+
# Check SWID namespace
132+
elif "iso/19770" in namespace_uri:
119133
return "swid"
120-
elif "spdx.org" in namespace:
134+
# Check SPDX namespace
135+
parsed_spdx_uri = urlparse(namespace_uri)
136+
spdx_domain = parsed_spdx_uri.netloc.lower()
137+
if spdx_domain == "spdx.org" or spdx_domain.endswith(".spdx.org"):
121138
return "spdx"
122139
except ET.ParseError:
123140
pass

0 commit comments

Comments
 (0)