Skip to content

Commit 897b5c1

Browse files
committed
Stop doubling the namespace in a removed package's purl
update_package_values already prefixes a namespaced package's purl with its namespace, so prefixing it again while collecting removed artifacts produced `com.example/widget@1.0.0com.example/com.example/widget@1.0.0`. The purl reaches the dependency overview comment verbatim, so every removed or replaced row for a namespaced package rendered with an unreadable name. The loop collecting added artifacts calls the same function and never did this.
1 parent 8c369e8 commit 897b5c1

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎socketsecurity/core/__init__.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,8 +2267,6 @@ def get_added_and_removed_packages(
22672267
try:
22682268
pkg = Package.from_diff_artifact(asdict(artifact))
22692269
pkg = Core.update_package_values(pkg)
2270-
if pkg.namespace:
2271-
pkg.purl += f"{pkg.namespace}/{pkg.purl}"
22722270
removed_packages[artifact.id] = pkg
22732271
except KeyError:
22742272
log.error(f"KeyError: Could not create package from removed artifact {artifact.id}")

‎tests/core/test_diff_generation.py‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44

55
import pytest
6-
from socketdev.fullscans import DiffArtifact
6+
from socketdev.fullscans import DiffArtifact, StreamDiffResponse
77

88
from socketsecurity.core import Core
99
from socketsecurity.core.classes import Package
@@ -312,3 +312,48 @@ def print_added_and_removed(added, removed):
312312
# pkg1_purl = next(p for p in diff.new_packages if p.id == "pkg1")
313313
# assert hasattr(pkg1_purl, "capabilities")
314314
# assert set(pkg1_purl.capabilities) == {"File System Access", "Network Access"}
315+
316+
317+
def _namespaced_diff_response(namespace: str = "com.example"):
318+
"""One namespaced artifact, delivered as both an addition and a removal."""
319+
raw = json.loads(
320+
(Path(__file__).parent.parent / "data/fullscans/diff/stream_diff.json").read_text()
321+
)
322+
template = raw["data"]["artifacts"]["added"][0]
323+
artifacts = {bucket: [] for bucket in ("added", "removed", "unchanged", "replaced", "updated")}
324+
for bucket in ("added", "removed"):
325+
artifacts[bucket].append(
326+
dict(
327+
template,
328+
diffType=bucket,
329+
head=None,
330+
base=None,
331+
id=f"namespaced-{bucket}",
332+
namespace=namespace,
333+
name="widget",
334+
version="1.0.0",
335+
type="maven",
336+
)
337+
)
338+
return StreamDiffResponse.from_dict({
339+
"success": raw["success"],
340+
"status": raw["status"],
341+
"data": {**raw["data"], "artifacts": artifacts},
342+
})
343+
344+
345+
def test_removed_package_purl_matches_the_added_form(core):
346+
"""A namespace belongs in the purl once, whichever bucket the artifact arrives in.
347+
348+
The purl reaches the dependency overview comment verbatim, so a second copy of
349+
the namespace renders as an unreadable package name on every removed or
350+
replaced row.
351+
"""
352+
core.sdk.fullscans.stream_diff.side_effect = None
353+
core.sdk.fullscans.stream_diff.return_value = _namespaced_diff_response()
354+
core.sdk.diffscans.create_from_ids.side_effect = Exception("diff-scans unavailable")
355+
356+
added, removed, _ = core.get_added_and_removed_packages("head", "new")
357+
358+
assert added["namespaced-added"].purl == "com.example/widget@1.0.0"
359+
assert removed["namespaced-removed"].purl == added["namespaced-added"].purl

0 commit comments

Comments
 (0)