Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OCI repository URL resolution to prevent duplicate name fragments #40

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions pkg/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func generateImagePurlVariants(registryString, imageName, digestString, tag, os,
// Purl with full qualifiers
qMap := map[string]string{}
if registryString != "" {
qMap["repository_url"] = strings.TrimSuffix(registryString, "/")
qMap["repository_url"] = registryString + imageName
}

purls = append(purls,
Expand Down Expand Up @@ -233,10 +233,8 @@ func PurlToReferenceString(purlString string, fopts ...RefConverterOptions) (str
qualifiers := p.Qualifiers.Map()

refString := p.Name
if _, ok := qualifiers["repository_url"]; ok {
refString = fmt.Sprintf(
"%s/%s", strings.TrimSuffix(qualifiers["repository_url"], "/"), p.Name,
)
if v, ok := qualifiers["repository_url"]; ok {
refString = v
} else if opts.DefaultRepository != "" {
refString = fmt.Sprintf(
"%s/%s", strings.TrimSuffix(opts.DefaultRepository, "/"), p.Name,
Expand Down
28 changes: 14 additions & 14 deletions pkg/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ func TestGenerateReferenceIdentifiers(t *testing.T) {
expected: IdentifiersBundle{
Identifiers: map[vex.IdentifierType][]string{
vex.PURL: {
"pkg:oci/alpine@sha256%3Aeece025e432126ce23f223450a0326fbebde39cdf496a85d8c016293fc851978?repository_url=index.docker.io%2Flibrary",
"pkg:oci/alpine@sha256%3Aeece025e432126ce23f223450a0326fbebde39cdf496a85d8c016293fc851978?arch=amd64&os=linux&repository_url=index.docker.io%2Flibrary",
"pkg:oci/alpine@sha256%3A48d9183eb12a05c99bcc0bf44a003607b8e941e1d4f41f9ad12bdcc4b5672f86?repository_url=index.docker.io%2Flibrary",
"pkg:oci/alpine@sha256%3A48d9183eb12a05c99bcc0bf44a003607b8e941e1d4f41f9ad12bdcc4b5672f86?arch=amd64&os=linux&repository_url=index.docker.io%2Flibrary",
"pkg:oci/alpine@sha256%3Aeece025e432126ce23f223450a0326fbebde39cdf496a85d8c016293fc851978?repository_url=index.docker.io%2Flibrary%2Falpine",
"pkg:oci/alpine@sha256%3Aeece025e432126ce23f223450a0326fbebde39cdf496a85d8c016293fc851978?arch=amd64&os=linux&repository_url=index.docker.io%2Flibrary%2Falpine",
"pkg:oci/alpine@sha256%3A48d9183eb12a05c99bcc0bf44a003607b8e941e1d4f41f9ad12bdcc4b5672f86?repository_url=index.docker.io%2Flibrary%2Falpine",
"pkg:oci/alpine@sha256%3A48d9183eb12a05c99bcc0bf44a003607b8e941e1d4f41f9ad12bdcc4b5672f86?arch=amd64&os=linux&repository_url=index.docker.io%2Flibrary%2Falpine",
},
},
Hashes: map[vex.Algorithm][]vex.Hash{
Expand All @@ -45,8 +45,8 @@ func TestGenerateReferenceIdentifiers(t *testing.T) {
expected: IdentifiersBundle{
Identifiers: map[vex.IdentifierType][]string{
vex.PURL: {
"pkg:oci/curl@sha256%3A3b987bc327e8aa8e7db26822e0552d927d25392ccb4d3b9d30b5390b485520d8?repository_url=cgr.dev%2Fchainguard",
"pkg:oci/curl@sha256%3A3b987bc327e8aa8e7db26822e0552d927d25392ccb4d3b9d30b5390b485520d8?arch=amd64&os=linux&repository_url=cgr.dev%2Fchainguard",
"pkg:oci/curl@sha256%3A3b987bc327e8aa8e7db26822e0552d927d25392ccb4d3b9d30b5390b485520d8?repository_url=cgr.dev%2Fchainguard%2Fcurl",
"pkg:oci/curl@sha256%3A3b987bc327e8aa8e7db26822e0552d927d25392ccb4d3b9d30b5390b485520d8?arch=amd64&os=linux&repository_url=cgr.dev%2Fchainguard%2Fcurl",
},
},
Hashes: map[vex.Algorithm][]vex.Hash{
Expand All @@ -63,10 +63,10 @@ func TestGenerateReferenceIdentifiers(t *testing.T) {
expected: IdentifiersBundle{
Identifiers: map[vex.IdentifierType][]string{
vex.PURL: {
"pkg:oci/scanner-test@sha256%3Aa0acdc09bf31ac45076a5850d0564ee144a9c8483f89eb869dc6ddd6218629c9?repository_url=localhost%3A5000",
"pkg:oci/scanner-test@sha256%3Aa0acdc09bf31ac45076a5850d0564ee144a9c8483f89eb869dc6ddd6218629c9?arch=amd64&os=linux&repository_url=localhost%3A5000&tag=unfixed-vulnerabilities-wolfi",
"pkg:oci/scanner-test@sha256%3A86e3ee69ede5f56514972d16fcfcfafbb54a65d2349c6f8419670bf7993d8583?repository_url=localhost%3A5000",
"pkg:oci/scanner-test@sha256%3A86e3ee69ede5f56514972d16fcfcfafbb54a65d2349c6f8419670bf7993d8583?arch=amd64&os=linux&repository_url=localhost%3A5000&tag=unfixed-vulnerabilities-wolfi",
"pkg:oci/scanner-test@sha256%3Aa0acdc09bf31ac45076a5850d0564ee144a9c8483f89eb869dc6ddd6218629c9?repository_url=localhost%3A5000%2Fscanner-test",
"pkg:oci/scanner-test@sha256%3Aa0acdc09bf31ac45076a5850d0564ee144a9c8483f89eb869dc6ddd6218629c9?arch=amd64&os=linux&repository_url=localhost%3A5000%2Fscanner-test&tag=unfixed-vulnerabilities-wolfi",
"pkg:oci/scanner-test@sha256%3A86e3ee69ede5f56514972d16fcfcfafbb54a65d2349c6f8419670bf7993d8583?repository_url=localhost%3A5000%2Fscanner-test",
"pkg:oci/scanner-test@sha256%3A86e3ee69ede5f56514972d16fcfcfafbb54a65d2349c6f8419670bf7993d8583?arch=amd64&os=linux&repository_url=localhost%3A5000%2Fscanner-test&tag=unfixed-vulnerabilities-wolfi",
},
},
Hashes: map[vex.Algorithm][]vex.Hash{
Expand All @@ -89,8 +89,8 @@ func TestGenerateReferenceIdentifiers(t *testing.T) {
expected: IdentifiersBundle{
Identifiers: map[vex.IdentifierType][]string{
vex.PURL: {
"pkg:oci/alpine@sha256%3Affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff?repository_url=index.docker.io%2Flibrary",
"pkg:oci/alpine@sha256%3Affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff?arch=amd64&os=linux&repository_url=index.docker.io%2Flibrary",
"pkg:oci/alpine@sha256%3Affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff?repository_url=index.docker.io%2Flibrary%2Falpine",
"pkg:oci/alpine@sha256%3Affffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff?arch=amd64&os=linux&repository_url=index.docker.io%2Flibrary%2Falpine",
},
},
Hashes: map[vex.Algorithm][]vex.Hash{
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestPurlToReferenceString(t *testing.T) {
},
{
"normal-with-repo",
"pkg:oci/curl@sha256%3A47fed8868b46b060efb8699dc40e981a0c785650223e03602d8c4493fc75b68c?repository_url=cgr.dev/chainguard/",
"pkg:oci/curl@sha256%3A47fed8868b46b060efb8699dc40e981a0c785650223e03602d8c4493fc75b68c?repository_url=cgr.dev/chainguard/curl",
"cgr.dev/chainguard/curl@sha256:47fed8868b46b060efb8699dc40e981a0c785650223e03602d8c4493fc75b68c",
[]RefConverterOptions{},
false,
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestPurlToReferenceString(t *testing.T) {
},
{
"repo-override",
"pkg:oci/pause:latest?repository_url=k8s.gcr.io/",
"pkg:oci/pause:latest?repository_url=k8s.gcr.io/pause",
"registry.k8s.io/release/pause:latest",
[]RefConverterOptions{
WithOverrideRepository("registry.k8s.io/release/"),
Expand Down