|
3 | 3 |
|
4 | 4 | import json
|
5 | 5 | from typing import Optional
|
| 6 | +from urllib.parse import urlparse |
6 | 7 |
|
7 | 8 | import defusedxml.ElementTree as ET
|
8 | 9 |
|
@@ -51,15 +52,22 @@ def sbom_detection(file_path: str) -> Optional[str]:
|
51 | 52 | try:
|
52 | 53 | tree = ET.parse(file_path)
|
53 | 54 | root = tree.getroot()
|
54 |
| - namespace = ( |
| 55 | + namespace_uri = ( |
55 | 56 | root.tag.split("}", 1)[0].strip("{") if "}" in root.tag else ""
|
56 | 57 | )
|
57 | 58 |
|
58 | 59 | # 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): |
60 | 65 | return "cyclonedx"
|
61 | 66 | # 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 | + ): |
63 | 71 | return "swid"
|
64 | 72 | except ET.ParseError as e:
|
65 | 73 | 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]:
|
109 | 117 | try:
|
110 | 118 | tree = ET.parse(file_path)
|
111 | 119 | root = tree.getroot()
|
112 |
| - namespace = ( |
| 120 | + namespace_uri = ( |
113 | 121 | root.tag.split("}", 1)[0].strip("{") if "}" in root.tag else ""
|
114 | 122 | )
|
115 | 123 |
|
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 | + ): |
117 | 130 | return "cyclonedx"
|
118 |
| - elif "iso/19770" in namespace: |
| 131 | + # Check SWID namespace |
| 132 | + elif "iso/19770" in namespace_uri: |
119 | 133 | 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"): |
121 | 138 | return "spdx"
|
122 | 139 | except ET.ParseError:
|
123 | 140 | pass
|
|
0 commit comments