Skip to content

Commit 977378c

Browse files
fmoessbauerUrist-McGit
authored andcommitted
fix(download): correctly reconstruct package from PURL
We currently use a simple regex based parser to reconstruct the name, version and architecture of a package from its PURL. However, this does not unescape the data correctly. To fix this, we just switch to the PURL parser (we are anyways already using in the project). Fixes: 4a32c6b ("feat: add download command to retrieve packages ...") Signed-off-by: Felix Moessbauer <[email protected]>
1 parent d30c8b8 commit 977378c

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/debsbom/download/download.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
from functools import reduce
88
import hashlib
99
import json
10-
import re
1110
import sys
1211
from typing import Generator, Tuple, Type
1312
from pathlib import Path
1413
from urllib.request import urlretrieve
14+
from packageurl import PackageURL
15+
1516
from ..dpkg import package
1617
from ..snapshot import client as sdlclient
1718

@@ -74,9 +75,6 @@ def insert(
7475

7576

7677
class PackageResolver:
77-
def __init__(self):
78-
self.purl_regex = re.compile(r"pkg:deb\/debian\/(.*)@(.*)[?]arch=(.*)$")
79-
8078
@abstractmethod
8179
def debian_pkgs(self) -> Generator:
8280
"""
@@ -91,14 +89,22 @@ def binaries(self) -> Generator[package.BinaryPackage, None, None]:
9189
return filter(lambda p: isinstance(p, package.BinaryPackage), self.debian_pkgs())
9290

9391
def package_from_purl(self, purl: str) -> Tuple[str, str, str]:
94-
parts = self.purl_regex.fullmatch(purl)
95-
if not parts:
92+
purl = PackageURL.from_string(purl)
93+
if not purl.type == "deb":
9694
raise RuntimeError("Not a debian purl", purl)
97-
if parts[3] == "source":
98-
return package.SourcePackage(parts[1], parts[2])
95+
if purl.qualifiers.get("arch") == "source":
96+
return package.SourcePackage(purl.name, purl.version)
9997
else:
10098
return package.BinaryPackage(
101-
parts[1], None, None, parts[3], None, parts[2], None, None, None
99+
name=purl.name,
100+
section=None,
101+
maintainer=None,
102+
architecture=purl.qualifiers.get("arch"),
103+
source=None,
104+
version=purl.version,
105+
depends=None,
106+
description=None,
107+
homepage=None,
102108
)
103109

104110
@staticmethod

0 commit comments

Comments
 (0)