Skip to content

Commit 089f4dc

Browse files
committed
Update purl-spec submodule and fix failing tests
Stop stripping trailing slashes and default registry from bazel repository_url qualifiers. Add otp and vscode-extension type validation: otp prohibits namespace, vscode-extension requires it.
1 parent b2ad76f commit 089f4dc

3 files changed

Lines changed: 59 additions & 65 deletions

File tree

packageurl.go

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -111,41 +111,47 @@ var (
111111
TypeSWID = "swid"
112112
// TypeSwift is pkg:swift purl
113113
TypeSwift = "swift"
114+
// TypeOTP is a pkg:otp purl.
115+
TypeOTP = "otp"
116+
// TypeVSCodeExtension is a pkg:vscode-extension purl.
117+
TypeVSCodeExtension = "vscode-extension"
114118

115119
// KnownTypes is a map of types that are officially supported by the spec.
116120
// See https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#known-purl-types
117121
KnownTypes = map[string]struct{}{
118-
TypeAlpm: {},
119-
TypeApk: {},
120-
TypeBitbucket: {},
121-
TypeBitnami: {},
122-
TypeCargo: {},
123-
TypeCocoapods: {},
124-
TypeComposer: {},
125-
TypeConan: {},
126-
TypeConda: {},
127-
TypeCpan: {},
128-
TypeCran: {},
129-
TypeDebian: {},
130-
TypeDocker: {},
131-
TypeGem: {},
132-
TypeGeneric: {},
133-
TypeGithub: {},
134-
TypeGolang: {},
135-
TypeHackage: {},
136-
TypeHex: {},
137-
TypeHuggingface: {},
138-
TypeMaven: {},
139-
TypeMLFlow: {},
140-
TypeNPM: {},
141-
TypeNuget: {},
142-
TypeOCI: {},
143-
TypePub: {},
144-
TypePyPi: {},
145-
TypeQpkg: {},
146-
TypeRPM: {},
147-
TypeSWID: {},
148-
TypeSwift: {},
122+
TypeAlpm: {},
123+
TypeApk: {},
124+
TypeBitbucket: {},
125+
TypeBitnami: {},
126+
TypeCargo: {},
127+
TypeCocoapods: {},
128+
TypeComposer: {},
129+
TypeConan: {},
130+
TypeConda: {},
131+
TypeCpan: {},
132+
TypeCran: {},
133+
TypeDebian: {},
134+
TypeDocker: {},
135+
TypeGem: {},
136+
TypeGeneric: {},
137+
TypeGithub: {},
138+
TypeGolang: {},
139+
TypeHackage: {},
140+
TypeHex: {},
141+
TypeHuggingface: {},
142+
TypeMaven: {},
143+
TypeMLFlow: {},
144+
TypeNPM: {},
145+
TypeNuget: {},
146+
TypeOCI: {},
147+
TypePub: {},
148+
TypePyPi: {},
149+
TypeQpkg: {},
150+
TypeRPM: {},
151+
TypeSWID: {},
152+
TypeSwift: {},
153+
TypeOTP: {},
154+
TypeVSCodeExtension: {},
149155
}
150156

151157
TypeApache = "apache"
@@ -773,36 +779,10 @@ func typeAdjustVersion(purlType, version string) string {
773779
}
774780

775781
// Make any purl type-specific adjustments to qualifiers.
776-
func typeAdjustQualifiers(purlType string, qualifiers Qualifiers) Qualifiers {
777-
switch purlType {
778-
case "bazel":
779-
return adjustBazelQualifiers(qualifiers)
780-
}
782+
func typeAdjustQualifiers(_ string, qualifiers Qualifiers) Qualifiers {
781783
return qualifiers
782784
}
783785

784-
// adjustBazelQualifiers normalizes bazel qualifiers:
785-
// - Removes default repository_url (https://bcr.bazel.build)
786-
// - Strips trailing slashes from repository_url
787-
func adjustBazelQualifiers(qualifiers Qualifiers) Qualifiers {
788-
const defaultRegistry = "https://bcr.bazel.build"
789-
result := make(Qualifiers, 0, len(qualifiers))
790-
for _, q := range qualifiers {
791-
if q.Key == "repository_url" {
792-
// Strip trailing slash
793-
val := strings.TrimSuffix(q.Value, "/")
794-
// Skip if it's the default registry
795-
if val == defaultRegistry {
796-
continue
797-
}
798-
result = append(result, Qualifier{Key: q.Key, Value: val})
799-
} else {
800-
result = append(result, q)
801-
}
802-
}
803-
return result
804-
}
805-
806786
// https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#mlflow
807787
func adjustMlflowName(name string, qualifiers map[string]string) string {
808788
if repo, ok := qualifiers["repository_url"]; ok {
@@ -917,6 +897,14 @@ func validCustomRules(p PackageURL) error {
917897
if p.Version == "" {
918898
return errors.New("version is required")
919899
}
900+
case TypeOTP:
901+
if p.Namespace != "" {
902+
return errors.New("namespace is not allowed for otp purls")
903+
}
904+
case TypeVSCodeExtension:
905+
if p.Namespace == "" {
906+
return errors.New("namespace is required for vscode-extension purls")
907+
}
920908
}
921909
return nil
922910
}

packageurl_bench_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66

77
// Sample purls of varying complexity
88
var (
9-
simplePurl = "pkg:npm/lodash@4.17.21"
10-
namespacePurl = "pkg:maven/org.apache.commons/commons-lang3@3.12.0"
11-
qualifiersPurl = "pkg:npm/%40angular/core@16.0.0?repository_url=https://registry.npmjs.org"
12-
complexPurl = "pkg:rpm/fedora/curl@7.50.3-1.fc25?arch=i386&distro=fedora-25&repository_url=http://example.com"
13-
subpathPurl = "pkg:github/package-url/purl-spec@244fd47e07d1004f0aed9c#src/main/java"
14-
fullPurl = "pkg:deb/debian/dpkg@1.19.0.4?arch=amd64&distro=stretch&repository_url=http://deb.debian.org#subpath/to/file"
9+
simplePurl = "pkg:npm/lodash@4.17.21"
10+
namespacePurl = "pkg:maven/org.apache.commons/commons-lang3@3.12.0"
11+
qualifiersPurl = "pkg:npm/%40angular/core@16.0.0?repository_url=https://registry.npmjs.org"
12+
complexPurl = "pkg:rpm/fedora/curl@7.50.3-1.fc25?arch=i386&distro=fedora-25&repository_url=http://example.com"
13+
subpathPurl = "pkg:github/package-url/purl-spec@244fd47e07d1004f0aed9c#src/main/java"
14+
fullPurl = "pkg:deb/debian/dpkg@1.19.0.4?arch=amd64&distro=stretch&repository_url=http://deb.debian.org#subpath/to/file"
1515
)
1616

1717
// Pre-parsed PackageURL structs for ToString benchmarks
@@ -86,6 +86,12 @@ func BenchmarkFromString_Complex(b *testing.B) {
8686
}
8787
}
8888

89+
func BenchmarkFromString_Subpath(b *testing.B) {
90+
for i := 0; i < b.N; i++ {
91+
_, _ = FromString(subpathPurl)
92+
}
93+
}
94+
8995
func BenchmarkFromString_Full(b *testing.B) {
9096
for i := 0; i < b.N; i++ {
9197
_, _ = FromString(fullPurl)

testdata/purl-spec

Submodule purl-spec updated 52 files

0 commit comments

Comments
 (0)