GitLabgitlab-org

cli

main
ply7 git calls194 ms98 KiB5 cachedprotocol v2
142 lines4.4 KiBRaw
1package proxy
3import (
4 "cmp"
5 "net"
6 "net/http"
7 "net/url"
8 "slices"
9 "strings"
10 "sync/atomic"
12 "gitlab.com/gitlab-org/cli/internal/dbg"
13 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/policy"
16// MavenMirrors holds the mirror URLs Maven sends every repository download
17// through, once they are known, so MavenMatcher can read a download under one
18// exactly instead of trying every prefix a mirror could have added. The zero
19// value knows no mirrors. It is safe for concurrent use.
20type MavenMirrors struct {
21 set atomic.Pointer[mavenMirrorSet]
24type mavenMirrorSet struct {
25 bases []mavenBase // longest path first
26 // externalOnly is set when the catch-all mirror is external:*, which
27 // Maven doesn't apply to repositories on localhost.
28 externalOnly bool
31// mavenBase is a mirror URL reduced to what a request is compared on.
32type mavenBase struct {
33 scheme, host, port, path string
36// Set makes downloads under urls match exactly. externalOnly reports that the
37// catch-all mirror is external:*, so a request to a loopback host may come
38// from a repository Maven didn't mirror and is left to enumeration.
39func (m *MavenMirrors) Set(urls []*url.URL, externalOnly bool) {
40 set := &mavenMirrorSet{externalOnly: externalOnly}
41 for _, u := range urls {
42 if b, ok := newMavenBase(u); ok {
43 set.bases = append(set.bases, b)
44 }
45 }
46 slices.SortStableFunc(set.bases, func(a, b mavenBase) int { return cmp.Compare(len(b.path), len(a.path)) })
47 known := make([]string, len(set.bases))
48 for i, b := range set.bases {
49 known[i] = b.scheme + "://" + net.JoinHostPort(b.host, b.port) + b.path
50 }
51 dbg.Debugf("dependency firewall: matching maven downloads exactly under %s (external only: %t)", strings.Join(known, ", "), externalOnly)
52 m.set.Store(set)
55// newMavenBase normalises u's scheme, host and port, and gives its path a
56// trailing slash so a prefix match ends on a segment boundary.
57func newMavenBase(u *url.URL) (mavenBase, bool) {
58 b := mavenBase{scheme: strings.ToLower(u.Scheme), host: strings.ToLower(u.Hostname()), port: u.Port(), path: u.Path}
59 if b.port == "" {
60 switch b.scheme {
61 case "https":
62 b.port = "443"
63 case "http":
64 b.port = "80"
65 }
66 }
67 if b.host == "" || b.port == "" {
68 return mavenBase{}, false
69 }
70 if !strings.HasSuffix(b.path, "/") {
71 b.path += "/"
72 }
73 return b, true
76// match reads a download under the known mirror URLs. ok is false when m
77// knows no mirrors or req isn't under one, leaving req to enumeration.
78func (m *MavenMirrors) match(req *http.Request, op policy.Operation) (Match, bool) {
79 // Maven mirrors only resolution: an upload goes to a deploy repository,
80 // whose URL can sit under a mirror's.
81 if m == nil || op != policy.Download {
82 return Match{}, false
83 }
84 set := m.set.Load()
85 if set == nil {
86 return Match{}, false
87 }
88 origin, ok := newMavenBase(req.URL)
89 if !ok || set.externalOnly && IsLoopbackHost(origin.host) {
90 return Match{}, false
91 }
92 var under bool
93 var coords []policy.Coordinate
94 for _, b := range set.bases {
95 if b.scheme != origin.scheme || b.host != origin.host || b.port != origin.port {
96 continue
97 }
98 rest, ok := strings.CutPrefix(req.URL.Path, b.path)
99 if !ok {
100 continue
101 }
102 // Maven never sends empty or dot segments, and a server that collapses
103 // them would serve a different coordinate than the path names.
104 if slices.ContainsFunc(strings.Split(strings.TrimSuffix(rest, "/"), "/"), func(s string) bool {
105 return s == "" || s == "." || s == ".."
106 }) {
107 return Match{}, false
108 }
109 under = true
110 name, version, result := mavenLayoutCoordinate(rest)
111 switch result {
112 case mavenUnparseable:
113 return unparseableMatch("maven", op, "unparseable maven artifact coordinate"), true
114 case mavenParsed:
115 c := policy.Coordinate{Ecosystem: "maven", Name: name, Version: version}
116 if !slices.Contains(coords, c) {
117 coords = append(coords, c)
118 }
119 }
120 }
121 if !under {
122 return Match{}, false
123 }
124 if len(coords) == 0 {
125 return Match{}, true
126 }
127 exact := Match{Matched: true, Pass: true, Coordinate: coords[0], Operation: op}
128 if len(coords) > 1 {
129 exact.Alternates = coords[1:]
130 }
131 return exact, true
134// IsLoopbackHost reports whether host is localhost or a loopback address, a
135// superset of the hosts Maven treats as local (localhost and 127.0.0.1).
136func IsLoopbackHost(host string) bool {
137 if strings.EqualFold(host, "localhost") {
138 return true
139 }
140 ip := net.ParseIP(host)
141 return ip != nil && ip.IsLoopback()
Diagnostics7 git calls · 194 ms
CallStatusSizeTime0194 ms
discover200185 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
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 · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=2
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=3
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=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit5 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=5
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=5
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=2
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=26d32585a3a474717e00a318584ccd16918c9d32
Open pack map
fetchtree:02001.0 KiB · hit2 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=1051
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=7a2a90605315ba5b37f63c08726e6b2caa26e48d
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=1051
git.pkt.payload.max=1024
git.sideband.band1.bytes=1024
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1024
git.pack.trailer=7503ef36a75d3595b473035c69e639fd0ce41598
Open pack map
fetch2001.9 KiB183 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=183
git.response.bytes=1946
git.response.cache=MISS
git.response.duration_ms=183
git.fetch.wants=c8986785417743ffc5aa9c057347b3a80ad07e3a
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=1946
git.pkt.payload.max=1919
git.sideband.band1.bytes=1919
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
a5e1fa095aee24ce
Request
c834ea0c…
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