Skip to content

Commit 32a2db3

Browse files
authored
fix: encode PURL qualifiers as a normalized string (#466)
This PR sets `encode=True` to encode qualifiers of a PURL string as a normalized string while converting it to a dictionary and storing it to the SQLite database because SQLite doesn't support dict type. It also adds exception handling for deserializing a PURL string while initializing a Component instance. Signed-off-by: behnazh-w <[email protected]>
1 parent 83569ee commit 32a2db3

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/macaron/database/table_definitions.py

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

2525
from macaron.database.database_manager import ORMBase
2626
from macaron.database.rfc3339_datetime import RFC3339DateTime
27-
from macaron.errors import CUEExpectationError, CUERuntimeError
27+
from macaron.errors import CUEExpectationError, CUERuntimeError, InvalidPURLError
2828
from macaron.slsa_analyzer.provenance.expectations.cue import cue_validator
2929
from macaron.slsa_analyzer.provenance.expectations.expectation import Expectation
3030
from macaron.slsa_analyzer.slsa_req import ReqName
@@ -172,9 +172,21 @@ def __init__(self, purl: str, analysis: Analysis, repository: "Repository | None
172172
The corresponding analysis.
173173
repository: Repository | None
174174
The corresponding repository.
175+
176+
Raises
177+
------
178+
InvalidPURLError
179+
If the PURL provided from the user is invalid.
175180
"""
176-
purl_parts = PackageURL.from_string(purl)
177-
purl_kwargs = purl_parts.to_dict()
181+
try:
182+
purl_parts = PackageURL.from_string(purl)
183+
except ValueError as error:
184+
raise InvalidPURLError(f"The package url {purl} is not valid.") from error
185+
186+
# We set ``encode=True`` to encode qualifiers as a normalized string because SQLite doesn't support ``dict`` type.
187+
# TODO: Explore the ``dbm`` or ``shelve`` packages to support dict type, which are part of the Python standard library.
188+
purl_kwargs = purl_parts.to_dict(encode=True)
189+
178190
super().__init__(purl=purl, analysis=analysis, repository=repository, **purl_kwargs)
179191

180192
@property

0 commit comments

Comments
 (0)