Skip to content

Commit 86a507d

Browse files
committed
Add checks in create_purl_map
Signed-off-by: Jono Yang <jyang@nexb.com>
1 parent acf69b5 commit 86a507d

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

src/purl_validator/__init__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,25 @@
3535

3636

3737
def create_purl_map(purls):
38-
# purls must be sorted and converted to bytes before going into the Map
39-
prepared_purls = [(bytes(purl, "utf-8"), 1) for purl in sorted(purls)]
38+
# Ensure elements of `purls` are strings:
39+
purl_strs = []
40+
for purl in purls:
41+
if not isinstance(purl, (PackageURL, str)):
42+
raise ValueError(f"invalid purl in `purls`: {purl}")
43+
if isinstance(purl, PackageURL):
44+
purl_str = purl.to_string()
45+
else:
46+
purl_str = purl
47+
purl_strs.append(purl_str)
48+
49+
# purl strs must be sorted and converted to bytes before going into the Map
50+
prepared_purl_strs = [(bytes(purl_str, "utf-8"), 1) for purl_str in sorted(purl_strs)]
51+
52+
# create map
4053
temp_dir = fileutils.get_temp_dir()
4154
map_loc = Path(temp_dir) / "purls.map"
42-
ducer.Map.build(map_loc, prepared_purls)
55+
ducer.Map.build(map_loc, prepared_purl_strs)
56+
4357
return map_loc
4458

4559

0 commit comments

Comments
 (0)