From 0010e501de5cd8b573a3cffd57c955f6c206526e Mon Sep 17 00:00:00 2001 From: Charlie Morrison Date: Sat, 5 Dec 2026 23:49:00 +0200 Subject: [PATCH] Treat Docker Hub's library namespace as implicit in ComparisonForm Docker Hub official images live under an implicit "library" namespace, so pkg:docker/python and pkg:docker/library/python name one image but had different comparison forms. ComparisonForm now drops a "library" namespace for docker purls with no repository_url qualifier. With a repository_url the image is on another registry, where the rule does not apply, so the namespace stays. Fixes #70 Co-authored-by: Claude --- comparison.go | 18 ++++++++++++++---- comparison_test.go | 10 ++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/comparison.go b/comparison.go index 0605de7..81ba9c4 100644 --- a/comparison.go +++ b/comparison.go @@ -8,10 +8,11 @@ import ( ) const ( - purlTypeConda = "conda" - purlTypeNuGet = "nuget" - purlTypeOCI = "oci" - purlTypePyPI = "pypi" + purlTypeConda = "conda" + purlTypeDocker = "docker" + purlTypeNuGet = "nuget" + purlTypeOCI = "oci" + purlTypePyPI = "pypi" ) var pypiSeparators = regexp.MustCompile(`[-_.]+`) @@ -25,6 +26,8 @@ var pypiSeparators = regexp.MustCompile(`[-_.]+`) // - conda: a channel given as namespace or as a "channel/name" name moved // into the channel qualifier // - oci: a namespace folded into the name, so library/alpine is one name +// - docker: the "library" namespace of Docker Hub's official images +// dropped when no repository_url names another registry // - swift: registry identities (a scope without "." or "/") lowercased // // Parse and String are left as they are, since callers derive storage keys @@ -51,6 +54,13 @@ func (p *PURL) ComparisonForm() *PURL { } case purlTypeConda: comparisonConda(&c) + case purlTypeDocker: + // Docker Hub official images have an implicit "library" namespace, so + // library/python and python are one image. The rule is Docker Hub's: + // a repository_url points somewhere else, where it does not hold. + if c.Namespace == "library" && c.Qualifiers.Map()["repository_url"] == "" { + c.Namespace = "" + } case purlTypeOCI: // The spec gives oci no namespace; a path-style repository is one name. if c.Namespace != "" { diff --git a/comparison_test.go b/comparison_test.go index bf75185..6b188a5 100644 --- a/comparison_test.go +++ b/comparison_test.go @@ -42,6 +42,14 @@ func TestComparisonForm(t *testing.T) { {"pkg:oci/library%2Falpine@3.19", "pkg:oci/library%2Falpine@3.19"}, {"pkg:oci/alpine@3.19", "pkg:oci/alpine@3.19"}, + // docker: Docker Hub's implicit library namespace, only without a repository_url. + {"pkg:docker/library/python@3.12", "pkg:docker/python@3.12"}, + {"pkg:docker/python@3.12", "pkg:docker/python@3.12"}, + {"pkg:docker/library/python@sha256%3A0123?arch=amd64", "pkg:docker/python@sha256:0123?arch=amd64"}, + {"pkg:docker/library/python@3.12?repository_url=ghcr.io", "pkg:docker/library/python@3.12?repository_url=ghcr.io"}, + {"pkg:docker/bitnami/python@3.12", "pkg:docker/bitnami/python@3.12"}, + {"pkg:docker/library/library/python@3.12", "pkg:docker/library/library/python@3.12"}, + // swift: registry identities are case-insensitive, source coordinates are not. {"pkg:swift/Apple/Swift-NIO@2.0.0", "pkg:swift/apple/swift-nio@2.0.0"}, {"pkg:swift/github.com/Apple/Swift-NIO@2.0.0", "pkg:swift/github.com/Apple/Swift-NIO@2.0.0"}, @@ -71,6 +79,7 @@ func TestComparisonFormEquivalentSpellings(t *testing.T) { {"pkg:composer/symfony/console@v6.1.7", "pkg:composer/symfony/console@6.1.7"}, {"pkg:conda/conda-forge/numpy@1.26.0", "pkg:conda/numpy@1.26.0?channel=conda-forge", "pkg:conda/conda-forge%2Fnumpy@1.26.0"}, {"pkg:oci/library/alpine@3.19", "pkg:oci/library%2Falpine@3.19"}, + {"pkg:docker/python@3.12", "pkg:docker/library/python@3.12"}, {"pkg:swift/Apple/Swift-NIO@2.0.0", "pkg:swift/apple/swift-nio@2.0.0"}, } for _, group := range groups { @@ -99,6 +108,7 @@ func TestComparisonFormLeavesParseAlone(t *testing.T) { "pkg:composer/symfony/console@v6.1.7": "pkg:composer/symfony/console@v6.1.7", "pkg:conda/conda-forge/numpy@1.26.0": "pkg:conda/conda-forge/numpy@1.26.0", "pkg:oci/library/alpine@3.19": "pkg:oci/library/alpine@3.19", + "pkg:docker/library/python@3.12": "pkg:docker/library/python@3.12", "pkg:swift/Apple/Swift-NIO@2.0.0": "pkg:swift/Apple/Swift-NIO@2.0.0", } for input, want := range inputs {