You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@magpie.apache.org by po...@apache.org on 2026/09/18 03:20:48 UTC

(magpie) branch main updated: feat(cve-json): compute a purl for Helm charts (#1266)

This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/magpie.git


The following commit(s) were added to refs/heads/main by this push:
     new 6e1b3269 feat(cve-json): compute a purl for Helm charts (#1266)
6e1b3269 is described below

commit 6e1b3269f091a18a9281d7a778f4de3095c7e5be
Author: Jarek Potiuk <ja...@potiuk.com>
AuthorDate: Fri Sep 18 05:20:42 2026 +0200

    feat(cve-json): compute a purl for Helm charts (#1266)
    
    A Helm chart had no purl. The skill documented it as the example of a product
    with no package host — `purl_type = "none"` — which since #1247 means an
    advisory that carries no package identifier at all.
    
    Adds `purl_type = "helm"`, emitting
    
        pkg:helm/<chart>?repository_url=<base URL>
    
    and the qualifier support that needed: `compute_purl` previously only ever
    built `pkg:<type>/<name>`.
    
    THE REPOSITORY IS REQUIRED, AND THE GENERATOR FAILS CLOSED WITHOUT IT
    
    A chart is identified by where it is published, not by its name — two projects
    may both ship a chart called `superset`. So the base URL comes from
    `default_collection_url` (or the package's override) and no purl is emitted
    without one. The error now names that as a fix and tells the reader to ask
    whoever publishes the chart, because the value is frequently recorded nowhere:
    charts are routinely served from a different host than the project site, so it
    cannot be inferred from the homepage. `https://…` serving index.yaml and
    `oci://…` for an OCI artifact are both accepted verbatim; a trailing slash is
    dropped so one repository cannot produce two identifiers.
    
    WHY `helm`, WHICH THE SPEC DOES NOT REGISTER
    
    The purl specification defines 43 types and none of them is Helm. The two
    spec-correct encodings are `pkg:oci/` for a chart in an OCI registry and
    `pkg:generic/` for a classic repository — which give the *same chart two
    identities* depending on how it happens to be published, defeating the purpose
    of an identifier. Scanners that would match this advisory to a deployed chart
    already emit `pkg:helm/`. A strict validator may object to the type; being
    matched by no scanner is the worse failure. The reasoning is recorded at the
    constant and in the skill, so this is a decision rather than an oversight.
    
    Existing types are untouched: the qualifier is emitted only for types that
    cannot be identified by name alone, and a repository passed to any other type
    is ignored rather than changing its identifier.
    
    Generated-by: Claude Opus 5
---
 .../cve-tool-vulnogram/generate-cve-json/SKILL.md  | 35 +++++++++++++-
 .../src/generate_cve_json/cve_json.py              | 54 +++++++++++++++++++---
 .../tests/test_generate_cve_json.py                | 43 +++++++++++++++++
 3 files changed, 124 insertions(+), 8 deletions(-)

diff --git a/tools/cve-tool-vulnogram/generate-cve-json/SKILL.md b/tools/cve-tool-vulnogram/generate-cve-json/SKILL.md
index d8da3d96..f2b1e052 100644
--- a/tools/cve-tool-vulnogram/generate-cve-json/SKILL.md
+++ b/tools/cve-tool-vulnogram/generate-cve-json/SKILL.md
@@ -110,6 +110,37 @@ diff the two to see what the tool has added / what you changed by hand.
     record only through the purl it is used to derive (see
     `product.purl_type` below) — except for a product that opts out of
     purls, the one case a record still carries a `collectionURL`.
+  - **Helm charts — `purl_type = "helm"`, and you must know where the
+    chart is published.** A chart is identified by its repository, not by
+    its name: two projects may both ship a chart called `superset`. So the
+    generator emits
+
+    ```text
+    pkg:helm/<chart>?repository_url=<base URL>
+    ```
+
+    taking the base URL from `product.default_collection_url` (or the
+    package's `collection_url` override), and **refuses to emit a purl at
+    all without one** rather than producing a chart identifier that matches
+    somebody else's chart.
+
+    **If the base URL is not recorded anywhere, ask whoever publishes the
+    chart.** It is whatever a user would put in `helm repo add` — the
+    `https://…` site serving `index.yaml`, or the `oci://…` registry path
+    for a chart published as an OCI artifact. Either is accepted verbatim;
+    a trailing slash is dropped so the same repository does not produce two
+    identifiers. Do not infer it from the project's homepage: charts are
+    routinely served from a different host than the project site.
+
+    Note that `helm` is **not** one of the types the purl specification
+    registers. It is used because the scanners that would match this
+    advisory to a deployed chart emit it, and because the two spec-correct
+    encodings — `pkg:oci/` for an OCI registry and `pkg:generic/` for a
+    classic repository — would give the same chart two different identities
+    depending on how it happened to be published. A strict validator may
+    object to the type; being invisible to every scanner is the worse
+    outcome.
+
   - `product.purl_type` *(config only)* — the Package URL type
     for the project's packages (`pypi`, `npm`, `cargo`, …). Every
     `affected[]` entry carries a `packageURL` (`pkg:<type>/<packageName>`).
@@ -134,8 +165,8 @@ diff the two to see what the tool has added / what you changed by hand.
     valid-looking identifier pointing at the wrong ecosystem.
 
     Set `purl_type = "none"` to state deliberately that a product has no
-    package host — a Helm chart served from the project's own site, for
-    instance. That is an explicit decision rather than an omission, which
+    package host — a source-only release published to `dist.apache.org`
+    and nowhere else, for instance. That is an explicit decision rather than an omission, which
     is the distinction the previous optional behaviour lost. Those
     entries — and only those — carry `collectionURL` / `packageName`
     instead, because there is no purl for the CVE tool to derive them
diff --git a/tools/cve-tool-vulnogram/generate-cve-json/src/generate_cve_json/cve_json.py b/tools/cve-tool-vulnogram/generate-cve-json/src/generate_cve_json/cve_json.py
index 70b29a73..75b510d7 100644
--- a/tools/cve-tool-vulnogram/generate-cve-json/src/generate_cve_json/cve_json.py
+++ b/tools/cve-tool-vulnogram/generate-cve-json/src/generate_cve_json/cve_json.py
@@ -1161,7 +1161,14 @@ def build_affected(
         resolved_type = resolve_purl_type(entry_purl_type, entry_collection_url)
         purl: str | None = None
         if resolved_type != PURL_TYPE_NONE:
-            purl = compute_purl(resolved_type, entry_package, namespace=purl_namespace or "")
+            purl = compute_purl(
+                resolved_type,
+                entry_package,
+                namespace=purl_namespace or "",
+                # For a Helm chart the collection URL *is* the chart
+                # repository, which is the only thing that identifies it.
+                repository_url=entry_collection_url or "",
+            )
             if not purl:
                 raise ValueError(
                     f"Could not build a Package URL (purl) for {entry_package!r}. "
@@ -1174,6 +1181,11 @@ def build_affected(
                     f'or `[packages.overrides."{entry_package}"].purl_type`;\n'
                     f"  - set `product.purl_namespace` when the type requires one "
                     f"(maven groupId, composer vendor, go module prefix);\n"
+                    f"  - set `product.default_collection_url` (or the package's "
+                    f"`collection_url` override) when the type requires a "
+                    f"repository. A Helm chart needs the base URL it is published "
+                    f"to — ask whoever publishes the chart if it is not recorded "
+                    f"anywhere; the chart name alone does not identify it;\n"
                     f'  - set `product.purl_type = "{PURL_TYPE_NONE}"` to state '
                     f"deliberately that this product has no package host."
                 )
@@ -1457,6 +1469,19 @@ _PURL_NAMESPACE_PROHIBITED = frozenset({"pypi", "cargo", "gem", "nuget"})
 # artifactId, and Go's module/package split cannot be derived from a name
 # without consulting the module proxy, so neither is ever guessed here.
 _PURL_NAMESPACE_REQUIRED = frozenset({"maven", "golang", "composer"})
+# Types whose *repository* must be stated as a `repository_url` qualifier,
+# because the name alone does not identify the package.
+#
+# `helm` is deliberately not one of the 43 types the purl spec registers.
+# There is no registered type for a Helm chart, and the two spec-correct
+# encodings — `pkg:oci/` for a chart in an OCI registry, `pkg:generic/` for a
+# classic index.yaml repository — give the *same chart two identities*
+# depending on how it happens to be published, which is the opposite of what
+# an identifier is for. Scanners (Syft, Trivy) already emit `pkg:helm/`, so
+# this is the form with a chance of matching a deployed chart to our advisory.
+# A strict validator may reject the type; being unmatched by every scanner is
+# the worse failure.
+_PURL_REPOSITORY_REQUIRED = frozenset({"helm"})
 # Types the spec says lowercase both segments.
 _PURL_LOWERCASE_BOTH = frozenset({"golang", "composer"})
 
@@ -1523,7 +1548,12 @@ def resolve_purl_type(configured: str, collection_url: str) -> str:
     return _PURL_TYPE_BY_COLLECTION_HOST.get(host, "")
 
 
-def compute_purl(purl_type: str, package_name: str, namespace: str = "") -> str | None:
+def compute_purl(
+    purl_type: str,
+    package_name: str,
+    namespace: str = "",
+    repository_url: str = "",
+) -> str | None:
     """Return the Package URL (purl) for a package, or ``None``.
 
     Builds the ``affected[].packageURL`` value the CVE Record Format
@@ -1538,10 +1568,16 @@ def compute_purl(purl_type: str, package_name: str, namespace: str = "") -> str
     namespace. An npm scope carried in the name itself always wins over
     it.
 
+    ``repository_url`` is emitted as a qualifier for the types in
+    :data:`_PURL_REPOSITORY_REQUIRED`, which cannot be identified by name
+    alone. A Helm chart called ``superset`` means nothing without the
+    repository it is published to, so the qualifier is required rather than
+    decorative and its absence yields no purl.
+
     Returns ``None`` when no type is configured, when the name is empty,
-    or when the type needs a namespace and none is available — no purl
-    beats a confidently wrong identifier pointing at someone else's
-    package.
+    when the type needs a namespace and none is available, or when the type
+    needs a repository and none is available — no purl beats a confidently
+    wrong identifier pointing at someone else's package.
 
     Not to be confused with :func:`compute_package_url`, which returns a
     human-readable *project page* URL for the attachment table.
@@ -1575,9 +1611,15 @@ def compute_purl(purl_type: str, package_name: str, namespace: str = "") -> str
         return None
     if not name:
         return None
+    if normalised_type in _PURL_REPOSITORY_REQUIRED and not repository_url:
+        return None
     segments = _encode_purl_segments(resolved_namespace)
     segments.append(urllib.parse.quote(name, safe=""))
-    return f"pkg:{normalised_type}/" + "/".join(segments)
+    built = f"pkg:{normalised_type}/" + "/".join(segments)
+    if normalised_type in _PURL_REPOSITORY_REQUIRED:
+        # The spec orders qualifiers alphabetically; there is only one here.
+        built += "?repository_url=" + urllib.parse.quote(repository_url.rstrip("/"), safe=":/")
+    return built
 
 
 def split_purl(purl: str) -> tuple[str, str] | None:
diff --git a/tools/cve-tool-vulnogram/generate-cve-json/tests/test_generate_cve_json.py b/tools/cve-tool-vulnogram/generate-cve-json/tests/test_generate_cve_json.py
index 42802a32..3554ea20 100644
--- a/tools/cve-tool-vulnogram/generate-cve-json/tests/test_generate_cve_json.py
+++ b/tools/cve-tool-vulnogram/generate-cve-json/tests/test_generate_cve_json.py
@@ -1709,6 +1709,49 @@ class TestComputePurl:
     def test_pypi_rejects_a_slash_because_the_type_has_no_namespace(self):
         assert compute_purl("pypi", "example/thing") is None
 
+    # --- Helm charts ---------------------------------------------------
+    #
+    # `helm` is not one of the 43 types the purl spec registers. It is used
+    # anyway because the scanners that would match a CVE to a deployed chart
+    # emit it, and because the two spec-correct encodings (`pkg:oci/` and
+    # `pkg:generic/`) give the same chart two identities depending on how it
+    # is published. See the note on `_PURL_REPOSITORY_REQUIRED`.
+
+    def test_a_helm_chart_carries_its_repository_as_a_qualifier(self):
+        assert (
+            compute_purl("helm", "superset", repository_url="https://apache.github.io/superset")
+            == "pkg:helm/superset?repository_url=https://apache.github.io/superset"
+        )
+
+    def test_a_helm_chart_without_a_repository_yields_no_purl(self):
+        """The chart name alone identifies nothing — two projects may ship
+        a chart called `superset`. Failing closed is the whole point."""
+        assert compute_purl("helm", "superset") is None
+
+    def test_a_helm_repository_may_be_an_oci_registry(self):
+        assert (
+            compute_purl("helm", "superset", repository_url="oci://ghcr.io/apache")
+            == "pkg:helm/superset?repository_url=oci://ghcr.io/apache"
+        )
+
+    def test_a_trailing_slash_on_the_repository_is_dropped(self):
+        """`https://host/charts` and `https://host/charts/` are the same
+        repository; emitting both would defeat purl matching."""
+        assert compute_purl(
+            "helm", "superset", repository_url="https://apache.github.io/superset/"
+        ) == compute_purl("helm", "superset", repository_url="https://apache.github.io/superset")
+
+    def test_a_repository_is_ignored_by_types_that_do_not_need_one(self):
+        """Only the types that cannot be identified by name alone carry the
+        qualifier; adding it elsewhere would change existing identifiers."""
+        assert (
+            compute_purl("pypi", "apache-example", repository_url="https://pypi.org/simple")
+            == "pkg:pypi/apache-example"
+        )
+
+    def test_split_purl_reads_a_helm_chart_back_without_its_qualifier(self):
+        assert split_purl("pkg:helm/superset?repository_url=https://x/y") == ("helm", "superset")
+
     def test_an_unverified_type_keeps_the_name_as_given(self):
         # Only pypi and npm have normalisation rules taken from the purl
         # spec; guessing one for another type corrupts the identifier.