GitLabgitlab-org
cli
main
1// Package purl parses Package URL (PURL) strings into the subset the
2// Dependency Firewall check command supports: npm, pypi, maven, gem, nuget,
3// cargo, and golang. It wraps github.com/package-url/packageurl-go and
4// normalises each PURL type's name to its registry form.
5package purl
7import (
8 "errors"
9 "fmt"
10 "net/url"
11 "regexp"
12 "slices"
13 "strings"
15 "github.com/package-url/packageurl-go"
16)
18// Package is a parsed PURL restricted to the types this command supports.
19// Name is the registry-form name (npm scope prefix retained, PEP 503
20// canonical for pypi, maven "group:artifact" coordinate). Version may be
21// empty when the PURL does not include one.
22type Package struct {
23 Type string
24 Name string
25 Version string
26}
28// Supported PURL type identifiers. Values match the package-url spec.
29// Declared as var to match the upstream library, which exposes these as
30// mutable package-level variables rather than untyped constants.
31var (
32 TypeNpm = packageurl.TypeNPM
33 TypePypi = packageurl.TypePyPi
34 TypeMaven = packageurl.TypeMaven
35 TypeGem = packageurl.TypeGem
36 TypeNuget = packageurl.TypeNuget
37 TypeCargo = packageurl.TypeCargo
38 TypeGolang = packageurl.TypeGolang
39)
41// supportedTypes lists every PURL type this command handles. It drives the
42// user-facing error message when an unsupported PURL is passed. It is
43// unexported so importers cannot mutate the backing array.
44var supportedTypes = []string{TypeNpm, TypePypi, TypeMaven, TypeGem, TypeNuget, TypeCargo, TypeGolang}
46// Parse converts a PURL string into a supported Package. It returns an error
47// on parse failures, unsupported types, or type-specific validation failures
48// (for example a maven PURL without a namespace).
49func Parse(s string) (Package, error) {
50 if strings.TrimSpace(s) == "" {
51 return Package{}, errors.New("invalid PURL: empty")
52 }
54 u, err := packageurl.FromString(s)
55 if err != nil {
56 return Package{}, fmt.Errorf("invalid PURL %q: %w", s, err)
57 }
58 if u.Type == "" {
59 return Package{}, fmt.Errorf("invalid PURL %q: missing type", s)
60 }
61 if u.Name == "" {
62 return Package{}, fmt.Errorf("invalid PURL %q: missing name", s)
63 }
65 if !slices.Contains(supportedTypes, u.Type) {
66 return Package{}, fmt.Errorf("PURL type %q is not supported by the dependency firewall check (supported: %s)",
67 u.Type, strings.Join(supportedTypes, ", "))
68 }
70 var name string
71 if u.Type == TypeGolang {
72 name, err = golangModulePath(s)
73 } else {
74 name, err = normalizeName(u.Type, u.Namespace, u.Name)
75 }
76 if err != nil {
77 return Package{}, fmt.Errorf("invalid PURL %q: %w", s, err)
78 }
80 return Package{Type: u.Type, Name: name, Version: u.Version}, nil
81}
83// golangModulePath rebuilds the module path from the raw PURL with its case
84// kept. packageurl-go lowercases golang namespaces and names as the PURL spec
85// says, but Go module paths are case-sensitive.
86func golangModulePath(s string) (string, error) {
87 _, rest, _ := strings.Cut(s, ":")
88 _, rest, _ = strings.Cut(strings.TrimLeft(rest, "/"), "/")
89 rest, _, _ = strings.Cut(rest, "#")
90 rest, _, _ = strings.Cut(rest, "?")
91 if at := strings.LastIndex(rest, "@"); at >= 0 {
92 rest = rest[:at]
93 }
94 var segments []string
95 for segment := range strings.SplitSeq(rest, "/") {
96 if segment == "" {
97 continue
98 }
99 decoded, err := url.PathUnescape(segment)
100 if err != nil {
101 return "", err
102 }
103 segments = append(segments, decoded)
104 }
105 return strings.Join(segments, "/"), nil
106}
108// pypiNameNormaliser collapses runs of `[-_.]` to a single `-` per PEP 503.
109var pypiNameNormaliser = regexp.MustCompile(`[-_.]+`)
111func normalizeName(typ, namespace, name string) (string, error) {
112 switch typ {
113 case TypeNpm:
114 if namespace != "" {
115 // packageurl-go strips the leading "@" from an npm scope
116 // namespace on parse; restore it so the registry-form name is
117 // "@scope/name".
118 ns := namespace
119 if !strings.HasPrefix(ns, "@") {
120 ns = "@" + ns
121 }
122 return ns + "/" + name, nil
123 }
124 return name, nil
125 case TypePypi:
126 lower := strings.ToLower(name)
127 return pypiNameNormaliser.ReplaceAllString(lower, "-"), nil
128 case TypeMaven:
129 if namespace == "" {
130 return "", errors.New("maven PURLs must include a namespace (the group ID)")
131 }
132 return namespace + ":" + name, nil
133 case TypeGem:
134 return name, nil
135 default:
136 return name, nil
137 }
138}
Diagnostics7 git calls · 303 ms
CallStatusSizeTime0303 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0200361 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=361 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=506cba622ceda1473ee3cde63969809c144b7247 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=361 git.pkt.payload.max=334 git.sideband.band1.bytes=334 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=334 git.pack.trailer=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:0200136 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=290 git.response.bytes=136 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=40c4d76ad298c21e1f1ffd4e5af51f76985602ef git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=136 git.pkt.payload.max=109 git.sideband.band1.bytes=109 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=109 git.pack.trailer=3110fb4fb704c20acf805fef861fcdc22ee8501eOpen pack map
fetch2001.7 KiB290 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1789 git.response.cache=MISS git.response.duration_ms=290 git.fetch.wants=e948716f18a4baea352587cc592905c2d02294fe git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1789 git.pkt.payload.max=1762 git.sideband.band1.bytes=1762 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e2551bec2e1ae0
- Request
- 1120438c…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format