Skip to content

Commit fb0b5d4

Browse files
committed
throw error in parse_purl
1 parent 2dd312d commit fb0b5d4

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

gcp/api/server.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,6 @@ def query_info(query) -> tuple[str, str | None, str | None]:
382382
if query.package.purl:
383383
try:
384384
purl = purl_helpers.parse_purl(query.package.purl) # can raise ValueError
385-
if not purl:
386-
raise ValueError('purl is invalid.')
387385
if query.package.ecosystem or query.package.name:
388386
raise ValueError('purl and name/ecosystem cannot both be specified')
389387
if purl.version and query.version:
@@ -725,15 +723,15 @@ def do_query(query: osv_service_v1_pb2.Query,
725723
version = query.version
726724

727725
# convert purl to package names
728-
purl = purl_helpers.parse_purl(purl_str)
729-
730-
if purl_str and not purl:
731-
context.service_context.abort(
732-
grpc.StatusCode.INVALID_ARGUMENT,
733-
'Invalid PURL.',
734-
)
726+
if purl_str:
727+
try:
728+
purl = purl_helpers.parse_purl(purl_str)
729+
except ValueError:
730+
context.service_context.abort(
731+
grpc.StatusCode.INVALID_ARGUMENT,
732+
'Invalid PURL.',
733+
)
735734

736-
if purl:
737735
if package_name: # Purls already include the package name
738736
context.service_context.abort(
739737
grpc.StatusCode.INVALID_ARGUMENT,

osv/purl_helpers.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,26 @@ def parse_purl(purl_str: str) -> Purl | None:
8383

8484
try:
8585
purl = PackageURL.from_string(purl_str)
86-
except ValueError: # Catch potential parsing errors
87-
return None
86+
except ValueError as e: # Catch potential parsing errors
87+
raise e
88+
89+
package = purl.name
90+
version = purl.version
8891

8992
match purl:
9093
case PackageURL(type='bitnami'):
9194
ecosystem = 'Bitnami'
9295
case PackageURL(type='cargo'):
9396
ecosystem = 'crates.io'
94-
case PackageURL(
95-
type='golang', namespace=namespace, name=name, version=version):
96-
# OSV uses the combined purl.namespace and purl.name for Go package names
97+
case PackageURL(type='golang', namespace=namespace, name=name):
98+
# Go uses the combined purl.namespace and purl.name for Go package names
9799
# Example:
98100
# pkg:golang/github.com/cri-o/cri-o
99101
# -> namespace: github.com/cri-o
100102
# -> name: cri-o
101103
# -> package name in OSV: github.com/cri-o/cri-o
102-
return Purl('Go', namespace + '/' + name, version)
104+
ecosystem = 'Go'
105+
package = namespace + '/' + name
103106
case PackageURL(type='hackage'):
104107
ecosystem = 'Hackage'
105108
case PackageURL(type='hex'):
@@ -144,9 +147,6 @@ def parse_purl(purl_str: str) -> Purl | None:
144147
ecosystem = 'Wolfi'
145148

146149
case _:
147-
return None
148-
149-
package = purl.name
150-
version = purl.version
150+
raise ValueError('Invalid ecosystem.')
151151

152152
return Purl(ecosystem, package, version)

osv/purl_helpers_test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@ def test_parse_purl(self):
139139
('Wolfi', 'test-package', '1.2.3'),
140140
purl_helpers.parse_purl('pkg:apk/wolfi/[email protected]'))
141141

142-
self.assertEqual(None, purl_helpers.parse_purl('pkg:bad/ubuntu/pygments'))
142+
with self.assertRaises(ValueError):
143+
purl_helpers.parse_purl('pkg:bad/ubuntu/pygments')
143144

144-
self.assertEqual(
145-
None, purl_helpers.parse_purl('purl:apk/wolfi/[email protected]'))
145+
with self.assertRaises(ValueError):
146+
purl_helpers.parse_purl('purl:apk/wolfi/[email protected]')
146147

147148
self.assertEqual(
148149
'Alpine',

0 commit comments

Comments
 (0)