forked from package-url/packageurl-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackageurl.go
393 lines (342 loc) · 10.9 KB
/
packageurl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
Copyright (c) the purl authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// Package packageurl implements the package-url spec
package packageurl
import (
"errors"
"fmt"
"net/url"
"path"
"regexp"
"strings"
)
var (
// QualifierKeyPattern describes a valid qualifier key:
//
// - The key must be composed only of ASCII letters and numbers, '.',
// '-' and '_' (period, dash and underscore).
// - A key cannot start with a number.
QualifierKeyPattern = regexp.MustCompile(`^[A-Za-z\.\-_][0-9A-Za-z\.\-_]*$`)
)
// These are the known purl types as defined in the spec. Some of these require
// special treatment during parsing.
// https://github.com/package-url/purl-spec#known-purl-types
var (
// TypeAlpm is a pkg:alpm purl.
TypeAlpm = "alpm"
// TypeApk is a pkg:apk purl.
TypeApk = "apk"
// TypeBitbucket is a pkg:bitbucket purl.
TypeBitbucket = "bitbucket"
// TypeCocoapods is a pkg:cocoapods purl.
TypeCocoapods = "cocoapods"
// TypeCargo is a pkg:cargo purl.
TypeCargo = "cargo"
// TypeComposer is a pkg:composer purl.
TypeComposer = "composer"
// TypeConan is a pkg:conan purl.
TypeConan = "conan"
// TypeConda is a pkg:conda purl.
TypeConda = "conda"
// TypeCran is a pkg:cran purl.
TypeCran = "cran"
// TypeDebian is a pkg:deb purl.
TypeDebian = "deb"
// TypeDocker is a pkg:docker purl.
TypeDocker = "docker"
// TypeGem is a pkg:gem purl.
TypeGem = "gem"
// TypeGeneric is a pkg:generic purl.
TypeGeneric = "generic"
// TypeGithub is a pkg:github purl.
TypeGithub = "github"
// TypeGolang is a pkg:golang purl.
TypeGolang = "golang"
// TypeHackage is a pkg:hackage purl.
TypeHackage = "hackage"
// TypeHex is a pkg:hex purl.
TypeHex = "hex"
// TypeMaven is a pkg:maven purl.
TypeMaven = "maven"
// TypeNPM is a pkg:npm purl.
TypeNPM = "npm"
// TypeNuget is a pkg:nuget purl.
TypeNuget = "nuget"
// TypeQPKG is a pkg:qpkg purl.
TypeQpkg = "qpkg"
// TypeOCI is a pkg:oci purl
TypeOCI = "oci"
// TypePyPi is a pkg:pypi purl.
TypePyPi = "pypi"
// TypeRPM is a pkg:rpm purl.
TypeRPM = "rpm"
// TypeSWID is pkg:swid purl
TypeSWID = "swid"
// TypeSwift is pkg:swift purl
TypeSwift = "swift"
// TypeHuggingface is pkg:huggingface purl.
TypeHuggingface = "huggingface"
// TypeMLflow is pkg:mlflow purl.
TypeMLFlow = "mlflow"
// TypeJulia is a pkg:julia purl
TypeJulia = "julia"
)
type Qualifiers = url.Values
// PackageURL is the struct representation of the parts that make a package url
type PackageURL struct {
Type string
Namespace string
Name string
Version string
Qualifiers Qualifiers
Subpath string
}
// NewPackageURL creates a new PackageURL struct instance based on input
func NewPackageURL(purlType, namespace, name, version string,
qualifiers Qualifiers, subpath string) *PackageURL {
return &PackageURL{
Type: purlType,
Namespace: namespace,
Name: name,
Version: version,
Qualifiers: qualifiers,
Subpath: subpath,
}
}
// ToString returns the human-readable instance of the PackageURL structure.
// This is the literal purl as defined by the spec.
func (p *PackageURL) ToString() string {
u := &url.URL{
Scheme: "pkg",
RawQuery: p.Qualifiers.Encode(),
Fragment: p.Subpath,
}
paths := []string{p.Type}
// we need to escape each segment by itself, so that we don't escape "/" in the namespace.
for _, segment := range strings.Split(p.Namespace, "/") {
if segment == "" {
continue
}
paths = append(paths, escape(segment))
}
nameWithVersion := escape(p.Name)
if p.Version != "" {
nameWithVersion += "@" + escape(p.Version)
}
paths = append(paths, nameWithVersion)
u.Opaque = strings.Join(paths, "/")
return u.String()
}
func (p PackageURL) String() string {
return p.ToString()
}
// FromString parses a valid package url string into a PackageURL structure
func FromString(purl string) (PackageURL, error) {
u, err := url.Parse(purl)
if err != nil {
return PackageURL{}, fmt.Errorf("failed to parse as URL: %w", err)
}
if u.Scheme != "pkg" {
return PackageURL{}, fmt.Errorf("purl scheme is not \"pkg\": %q", u.Scheme)
}
p := u.Opaque
// if a purl starts with pkg:/ or even pkg://, we need to fall back to host + path.
if p == "" {
p = strings.TrimPrefix(path.Join(u.Host, u.Path), "/")
}
typ, p, ok := strings.Cut(p, "/")
if !ok {
return PackageURL{}, fmt.Errorf("purl is missing type or name")
}
typ = strings.ToLower(typ)
qualifiers, err := getQualifiers(u.RawQuery)
if err != nil {
return PackageURL{}, fmt.Errorf("invalid qualifiers: %w", err)
}
namespace, name, version, err := separateNamespaceNameVersion(p)
if err != nil {
return PackageURL{}, err
}
pURL := PackageURL{
Qualifiers: qualifiers,
Type: typ,
Namespace: typeAdjustNamespace(typ, namespace),
Name: typeAdjustName(typ, name, qualifiers),
Version: typeAdjustVersion(typ, version),
Subpath: strings.Trim(u.Fragment, "/"),
}
return pURL, validCustomRules(pURL)
}
func getQualifiers(rawQuery string) (url.Values, error) {
qualifiers, err := url.ParseQuery(rawQuery)
if err != nil {
return nil, fmt.Errorf("could not parse qualifiers: %w", err)
}
for k := range qualifiers {
if !validQualifierKey(k) {
return nil, fmt.Errorf("invalid qualifier key: %q", k)
}
v := qualifiers.Get(k)
// only the first character needs to be lowercased. Note that pURL is alwyas UTF8, so we
// don't need to care about unicode here.
normalisedValue := strings.ToLower(v[:1]) + v[1:]
if normalisedKey := strings.ToLower(k); normalisedKey != k {
qualifiers.Del(k)
qualifiers.Set(normalisedKey, normalisedValue)
} else if normalisedValue != v {
qualifiers.Set(k, normalisedValue)
}
}
return qualifiers, nil
}
// escape the given string in a purl-compatible way.
func escape(s string) string {
// for compatibility with other implementations and the purl-spec, we want to escape all
// characters, which is what "QueryEscape" does. The issue with QueryEscape is that it encodes
// " " (space) as "+", which is valid in a query, but invalid in a path (see
// https://stackoverflow.com/questions/2678551/when-should-space-be-encoded-to-plus-or-20) for
// context).
// To work around that, we replace the "+" signs with the path-compatible "%20".
return strings.ReplaceAll(url.QueryEscape(s), "+", "%20")
}
func separateNamespaceNameVersion(path string) (ns, name, version string, err error) {
name = path
if namespaceSep := strings.LastIndex(name, "/"); namespaceSep != -1 {
ns, name = name[:namespaceSep], name[namespaceSep+1:]
ns, err = url.PathUnescape(ns)
if err != nil {
return "", "", "", fmt.Errorf("error unescaping namespace: %w", err)
}
}
if versionSep := strings.LastIndex(name, "@"); versionSep != -1 {
name, version = name[:versionSep], name[versionSep+1:]
version, err = url.PathUnescape(version)
if err != nil {
return "", "", "", fmt.Errorf("error unescaping version: %w", err)
}
}
name, err = url.PathUnescape(name)
if err != nil {
return "", "", "", fmt.Errorf("error unescaping name: %w", err)
}
if name == "" {
return "", "", "", fmt.Errorf("purl is missing name")
}
return ns, name, version, nil
}
// Make any purl type-specific adjustments to the parsed namespace.
// See https://github.com/package-url/purl-spec#known-purl-types
func typeAdjustNamespace(purlType, ns string) string {
switch purlType {
case TypeAlpm,
TypeApk,
TypeBitbucket,
TypeComposer,
TypeDebian,
TypeGithub,
TypeGolang,
TypeNPM,
TypeRPM,
TypeQpkg:
return strings.ToLower(ns)
}
return ns
}
// Make any purl type-specific adjustments to the parsed name.
// See https://github.com/package-url/purl-spec#known-purl-types
func typeAdjustName(purlType, name string, qualifiers Qualifiers) string {
switch purlType {
case TypeAlpm,
TypeApk,
TypeBitbucket,
TypeComposer,
TypeDebian,
TypeGithub,
TypeGolang,
TypeNPM:
return strings.ToLower(name)
case TypePyPi:
return strings.ToLower(strings.ReplaceAll(name, "_", "-"))
case TypeMLFlow:
return adjustMlflowName(name, qualifiers)
}
return name
}
// Make any purl type-specific adjustments to the parsed version.
// See https://github.com/package-url/purl-spec#known-purl-types
func typeAdjustVersion(purlType, version string) string {
switch purlType {
case TypeHuggingface:
return strings.ToLower(version)
}
return version
}
// https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#mlflow
func adjustMlflowName(name string, qualifiers Qualifiers) string {
switch v := qualifiers.Get("repository_url"); {
case v == "":
// No repository qualifier given, keep as-is
return name
case strings.Contains(v, "azureml"):
// Azure ML is case-sensitive and must be kept as-is
return name
case strings.Contains(v, "databricks"):
// Databricks is case-insensitive and must be lowercased
return strings.ToLower(name)
default:
// Unknown repository type, keep as-is
return name
}
}
// validQualifierKey validates a qualifierKey against our QualifierKeyPattern.
func validQualifierKey(key string) bool {
return QualifierKeyPattern.MatchString(key)
}
// validCustomRules evaluates additional rules for each package url type, as specified in the package-url specification.
// On success, it returns nil. On failure, a descriptive error will be returned.
func validCustomRules(p PackageURL) error {
q := p.Qualifiers
switch p.Type {
case TypeConan:
switch channelSet, nsSet := q.Has("channel"), p.Namespace != ""; {
case nsSet && channelSet:
if q.Get("channel") == "" {
return errors.New("the qualifier channel must be not empty if namespace is present")
}
case nsSet && !channelSet:
return errors.New("channel qualifier does not exist")
case !nsSet && channelSet:
if q.Get("channel") != "" {
return errors.New("namespace is required if channel is non empty")
}
}
case TypeSwift:
if p.Namespace == "" {
return errors.New("namespace is required")
}
if p.Version == "" {
return errors.New("version is required")
}
case TypeCran:
if p.Version == "" {
return errors.New("version is required")
}
}
return nil
}