Skip to content

Commit d500fde

Browse files
fix: Sanitize ORT package IDs to handle colons in versions (#2005)
Signed-off-by: Fabian Noll <Fabian.Noll@googlemail.com>
1 parent 5eb41fa commit d500fde

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

scanpipe/pipes/ort.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,28 @@ def get_ort_project_type(project):
130130
return "docker"
131131

132132

133+
def sanitize_id_part(value):
134+
"""
135+
Sanitize an identifier part by replacing colons with underscores.
136+
ORT uses colons as separators in the identifier string representation.
137+
"""
138+
if value:
139+
return value.replace(":", "_")
140+
return value
141+
142+
133143
def to_ort_package_list_yml(project):
134144
"""Convert a project object into a YAML string in the ORT package list format."""
135145
project_type = get_ort_project_type(project)
136146

137147
dependencies = []
138148
for package in project.discoveredpackages.all():
149+
type_ = sanitize_id_part(project_type or package.type)
150+
name = sanitize_id_part(package.name)
151+
version = sanitize_id_part(package.version)
152+
139153
dependency = Dependency(
140-
id=f"{project_type or package.type}::{package.name}:{package.version}",
154+
id=f"{type_}::{name}:{version}",
141155
purl=package.purl,
142156
sourceArtifact=SourceArtifact(url=package.download_url),
143157
declaredLicenses=[package.get_declared_license_expression_spdx()],

scanpipe/tests/pipes/test_ort.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,21 @@ def test_scanpipe_ort_pipes_to_ort_package_list_yml(self):
7272
],
7373
}
7474
self.assertEqual(expected, package_list)
75+
76+
def test_scanpipe_ort_pipes_to_ort_package_list_yml_sanitization(self):
77+
project = make_project(name="Analysis")
78+
package_data = {
79+
"name": "passwd",
80+
"type": "deb",
81+
"version": "1:4.13+dfsg1-4ubuntu3.2",
82+
"purl": "pkg:deb/ubuntu/passwd@1:4.13%2Bdfsg1-4ubuntu3.2?arch=amd64",
83+
}
84+
pipes.update_or_create_package(project, package_data)
85+
86+
package_list_yml = ort.to_ort_package_list_yml(project)
87+
package_list = saneyaml.load(package_list_yml)
88+
dependency_id = package_list["dependencies"][0]["id"]
89+
90+
# The colon in the version should be sanitized
91+
self.assertNotIn("1:4.13", dependency_id)
92+
self.assertEqual("deb::passwd:1_4.13+dfsg1-4ubuntu3.2", dependency_id)

0 commit comments

Comments
 (0)