GitLabgitlab-org
cli
main
1package pm
3import (
4 "errors"
5 "fmt"
6 "net/http"
7 "net/url"
8 "slices"
9 "strings"
11 "golang.org/x/net/http/httpproxy"
13 "gitlab.com/gitlab-org/cli/internal/dbg"
14 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/proxy"
15)
17// upstreamProxy is the outbound proxy a package manager would have used on its
18// own. Run points the manager's proxy settings at the inspection proxy, so the
19// inspection proxy uses this one for its own connection to the registry.
20type upstreamProxy struct {
21 // Source names the setting the proxy came from, for diagnostics.
22 Source string
23 // URL is the proxy as the manager reads it. A value without a scheme is
24 // taken as http://, as the managers do. Empty means the manager connects
25 // directly, without falling back to HTTPS_PROXY or HTTP_PROXY.
26 URL string
27 // NoProxy lists the hosts the manager reaches directly, in NO_PROXY syntax.
28 NoProxy string
29 // NonProxyHosts lists more hosts this proxy skips, as JVM-style patterns
30 // in which "*" matches any run of characters, ignoring case.
31 NonProxyHosts []string
32 // Fallbacks are tried in order for a host this proxy skips, as Maven does
33 // with its other proxies. A host that every proxy skips is reached
34 // directly.
35 Fallbacks []upstreamProxy
36}
38// upstreamProxyResolver is implemented by managers that read a proxy setting of
39// their own ahead of HTTPS_PROXY and HTTP_PROXY, from their environment
40// variables or their user and global config files. For any other manager, the
41// inspection proxy uses glab's HTTPS_PROXY, HTTP_PROXY and NO_PROXY.
42type upstreamProxyResolver interface {
43 // resolveUpstreamProxy reports the proxy the manager would use for HTTPS
44 // requests when run in dir with environ and args, or false when it has
45 // none beyond HTTPS_PROXY. It fails when a config file it reads is
46 // unreadable or malformed.
47 resolveUpstreamProxy(environ []string, dir string, args []string) (upstreamProxy, bool, error)
48 // proxyVariables lists the manager's own proxy variables that its
49 // resolvers read, for help text.
50 proxyVariables() []string
51 // proxyConfigFiles lists the user and global config files its resolvers
52 // read, for help text.
53 proxyConfigFiles() []string
54}
56// plainHTTPProxyResolver is implemented by managers that pick a different
57// proxy for plain-HTTP registries than for HTTPS ones. Other resolvers' HTTPS
58// proxy carries plain-HTTP requests too, as npm, pnpm, pip and cargo do.
59type plainHTTPProxyResolver interface {
60 // resolvePlainHTTPProxy reports the proxy the manager would use for
61 // plain-HTTP requests when run in dir with environ, or false when it has
62 // none beyond HTTP_PROXY.
63 resolvePlainHTTPProxy(environ []string, dir string) (upstreamProxy, bool, error)
64}
66// OwnProxyVariables lists the proxy variables of manager's own that glab reads
67// ahead of HTTPS_PROXY and HTTP_PROXY, or nil when it reads none.
68func OwnProxyVariables(manager PackageManager) []string {
69 if r, ok := manager.(upstreamProxyResolver); ok {
70 return r.proxyVariables()
71 }
72 return nil
73}
75// OwnProxyConfigFiles lists the user and global config files of manager's own
76// that glab reads proxy settings from, or nil when it reads none. glab skips
77// any of them that is inside the project being installed.
78func OwnProxyConfigFiles(manager PackageManager) []string {
79 if r, ok := manager.(upstreamProxyResolver); ok {
80 return r.proxyConfigFiles()
81 }
82 return nil
83}
85// OwnProxyHelpNote returns what the help for manager's proxy settings must
86// add, such as settings that only some of its versions use, or "".
87func OwnProxyHelpNote(manager PackageManager) string {
88 if n, ok := manager.(interface{ proxyHelpNote() string }); ok {
89 return n.proxyHelpNote()
90 }
91 return ""
92}
94// PlainHTTPUsesHTTPSProxy reports whether manager sends plain-HTTP requests
95// through HTTPS_PROXY ahead of HTTP_PROXY, as npm and pnpm do.
96func PlainHTTPUsesHTTPSProxy(manager PackageManager) bool {
97 _, ok := manager.(npmManager)
98 return ok
99}
101// upstreamProxyOptions returns the proxy options that send the inspection
102// proxy's registry traffic through the outbound proxy the manager would have
103// used for each scheme, read from environ before Run overrides it.
104func upstreamProxyOptions(manager PackageManager, environ []string, dir string, args []string) ([]proxy.Option, error) {
105 resolver, ok := manager.(upstreamProxyResolver)
106 if !ok {
107 return nil, nil
108 }
109 httpsProxy, httpsOK, err := resolver.resolveUpstreamProxy(environ, dir, args)
110 if err != nil {
111 return nil, fmt.Errorf("failed to read the %s proxy settings: %w", manager.Name(), err)
112 }
113 // npm and pnpm send plain HTTP through the same proxy, even a bare
114 // HTTPS_PROXY ahead of HTTP_PROXY (verified against npm 10.9, pnpm 10.12).
115 plainProxy, plainOK := httpsProxy, httpsOK
116 if r, ok := manager.(plainHTTPProxyResolver); ok {
117 if plainProxy, plainOK, err = r.resolvePlainHTTPProxy(environ, dir); err != nil {
118 return nil, fmt.Errorf("failed to read the %s proxy settings: %w", manager.Name(), err)
119 }
120 }
121 var opts []proxy.Option
122 for _, s := range []struct {
123 scheme string
124 upstream upstreamProxy
125 ok bool
126 }{{"https", httpsProxy, httpsOK}, {"http", plainProxy, plainOK}} {
127 if !s.ok {
128 continue
129 }
130 proxyFunc, err := s.upstream.proxyFunc()
131 if err != nil {
132 return nil, fmt.Errorf("invalid outbound proxy in %s: %w", s.upstream.Source, err)
133 }
134 if s.upstream.URL == "" {
135 dbg.Debugf("dependency firewall: reaching %s registries directly, as %s disables the proxy", s.scheme, s.upstream.Source)
136 } else {
137 dbg.Debugf("dependency firewall: reaching %s registries through the outbound proxy from %s", s.scheme, s.upstream.Source)
138 }
139 opts = append(opts, proxy.WithUpstreamProxyFor(s.scheme, proxyFunc))
140 }
141 return opts, nil
142}
144// proxyFunc returns a proxy selector for the inspection proxy's upstream
145// transport. It validates every URL up front so a typo fails the run with a
146// clear message, instead of every registry request failing later.
147func (u upstreamProxy) proxyFunc() (func(*http.Request) (*url.URL, error), error) {
148 if u.URL == "" {
149 return func(*http.Request) (*url.URL, error) { return nil, nil }, nil
150 }
151 if err := validateProxyURL(u.URL); err != nil {
152 return nil, err
153 }
154 // upstreamProxyOptions names u.Source, so only a fallback names its own.
155 for _, f := range u.Fallbacks {
156 if err := validateProxyURL(f.URL); err != nil {
157 return nil, fmt.Errorf("invalid outbound proxy in %s: %w", f.Source, err)
158 }
159 }
160 candidates := append([]upstreamProxy{u}, u.Fallbacks...)
161 picks := make([]func(*url.URL) (*url.URL, error), 0, len(candidates))
162 for _, c := range candidates {
163 pick := (&httpproxy.Config{HTTPProxy: c.URL, HTTPSProxy: c.URL, NoProxy: c.NoProxy}).ProxyFunc()
164 nonProxyHosts := c.NonProxyHosts
165 picks = append(picks, func(target *url.URL) (*url.URL, error) {
166 if matchesHostPattern(nonProxyHosts, target.Hostname()) {
167 return nil, nil
168 }
169 return pick(target)
170 })
171 }
172 return func(r *http.Request) (*url.URL, error) {
173 for _, pick := range picks {
174 if p, err := pick(r.URL); p != nil || err != nil {
175 return p, err
176 }
177 }
178 return nil, nil
179 }, nil
180}
182// matchesHostPattern reports whether host matches one of patterns, where "*"
183// matches any run of characters and case is ignored.
184func matchesHostPattern(patterns []string, host string) bool {
185 host = strings.ToLower(host)
186 for _, p := range patterns {
187 parts := strings.Split(strings.ToLower(p), "*")
188 if len(parts) == 1 {
189 if parts[0] == host {
190 return true
191 }
192 continue
193 }
194 rest, ok := strings.CutPrefix(host, parts[0])
195 if !ok {
196 continue
197 }
198 for _, mid := range parts[1 : len(parts)-1] {
199 i := strings.Index(rest, mid)
200 if i < 0 {
201 ok = false
202 break
203 }
204 rest = rest[i+len(mid):]
205 }
206 if ok && strings.HasSuffix(rest, parts[len(parts)-1]) {
207 return true
208 }
209 }
210 return false
211}
213// validateProxyURL checks raw as httpproxy reads it, taking a value without a
214// scheme as http://. Errors never include raw, because a proxy URL can carry
215// credentials.
216func validateProxyURL(raw string) error {
217 if !strings.Contains(raw, "://") {
218 raw = "http://" + raw
219 }
220 u, err := url.Parse(raw)
221 if err != nil || u.Hostname() == "" {
222 return errors.New("not a valid proxy URL")
223 }
224 switch u.Scheme {
225 case "http", "https", "socks5", "socks5h":
226 return nil
227 default:
228 return errors.New("unsupported proxy scheme; use http, https, socks5 or socks5h")
229 }
230}
232// firstSetEnv returns the name and value of the first key in keys with a
233// non-empty value in environ, and false when none is set. Package managers
234// treat an empty value as unset. With fold, keys match case-insensitively, as
235// npm's npm_config_* variables do.
236func firstSetEnv(environ []string, fold bool, keys ...string) (string, string, bool) {
237 for _, want := range keys {
238 for _, e := range slices.Backward(environ) {
239 k, v, found := strings.Cut(e, "=")
240 if !found || v == "" {
241 continue
242 }
243 if k == want || (fold && strings.EqualFold(k, want)) {
244 return k, v, true
245 }
246 }
247 }
248 return "", "", false
249}
251// joinNoProxy joins the non-empty NO_PROXY-style lists in lists.
252func joinNoProxy(lists ...string) string {
253 var parts []string
254 for _, l := range lists {
255 if l != "" {
256 parts = append(parts, l)
257 }
258 }
259 return strings.Join(parts, ",")
260}
262// envNoProxy returns NO_PROXY, or no_proxy when NO_PROXY is unset, matching
263// http.ProxyFromEnvironment.
264func envNoProxy(environ []string) string {
265 _, v, _ := firstSetEnv(environ, false, "NO_PROXY", "no_proxy")
266 return v
267}
269func (npmManager) proxyVariables() []string {
270 return []string{"npm_config_https_proxy", "npm_config_proxy"}
271}
273func (m npmManager) proxyConfigFiles() []string {
274 if m.binary != "npm" {
275 return nil
276 }
277 return []string{"~/.npmrc", "$PREFIX/etc/npmrc"}
278}
280// resolveUpstreamProxy for npm and pnpm. For HTTPS requests both use
281// https-proxy, then proxy, then HTTPS_PROXY, and skip the proxy for a host in
282// either noproxy or NO_PROXY (verified against npm 10.9 and pnpm 10.12). npm
283// also reads these settings from its user and global npmrc files. pnpm's
284// config files are not read yet, so for pnpm only npm_config_* variables are.
285func (m npmManager) resolveUpstreamProxy(environ []string, dir string, _ []string) (upstreamProxy, bool, error) {
286 config := configLayers{npmEnvLayer(environ)}
287 if m.binary == "npm" {
288 var err error
289 if config, err = readNpmConfig(environ, dir); err != nil {
290 return upstreamProxy{}, false, err
291 }
292 }
293 upstream, ok := npmUpstreamProxy(config, environ)
294 return upstream, ok, nil
295}
297func (yarnManager) proxyVariables() []string {
298 return []string{"YARN_HTTPS_PROXY", "YARN_PROXY", "npm_config_https_proxy", "npm_config_proxy"}
299}
301func (yarnManager) proxyConfigFiles() []string {
302 return []string{"~/.yarnrc", "~/.npmrc", "$PREFIX/etc/yarnrc", "$PREFIX/etc/npmrc", "~/.yarnrc.yml"}
303}
305func (yarnManager) proxyHelpNote() string {
306 return "For Yarn 2 and later, glab reads only YARN_HTTPS_PROXY and ~/.yarnrc.yml."
307}
309// resolveUpstreamProxy for Yarn. Both lines read YARN_HTTPS_PROXY in any case.
310// Berry then reads httpsProxy in ~/.yarnrc.yml. Classic reads https-proxy,
311// then proxy, from YARN_HTTPS_PROXY, YARN_PROXY and its user and global
312// .yarnrc files, then from npm_config_* variables and the user and global
313// .npmrc files. Neither applies NO_PROXY to these settings (verified against
314// Yarn 4.9 and 1.22).
315func (yarnManager) resolveUpstreamProxy(environ []string, dir string, _ []string) (upstreamProxy, bool, error) {
316 berry, err := yarnIsBerry(dir)
317 if err != nil {
318 return upstreamProxy{}, false, err
319 }
320 if berry {
321 return berryUpstreamProxy(environ, newConfigFiles(dir))
322 }
323 return yarnClassicUpstreamProxy(environ, newConfigFiles(dir))
324}
326// resolvePlainHTTPProxy for Yarn. For plain-HTTP requests Classic uses proxy,
327// then HTTP_PROXY (verified against Yarn 1.22). Berry sends plain-HTTP
328// requests through a CONNECT tunnel, so they take the HTTPS path.
329func (yarnManager) resolvePlainHTTPProxy(environ []string, dir string) (upstreamProxy, bool, error) {
330 berry, err := yarnIsBerry(dir)
331 if berry || err != nil {
332 return upstreamProxy{}, false, err
333 }
334 return yarnClassicPlainHTTPProxy(environ, newConfigFiles(dir))
335}
337func (pyPIManager) proxyVariables() []string { return []string{"PIP_PROXY"} }
339func (pyPIManager) proxyConfigFiles() []string {
340 return []string{"pip.conf", "$PIP_CONFIG_FILE"}
341}
343func (m pyPIManager) proxyHelpNote() string {
344 if m.binary != "pip" {
345 return ""
346 }
347 return "glab reads pip's --proxy option ahead of all of these."
348}
350// resolveUpstreamProxy for pip and pipenv, as pip 26.0 reads its settings:
351// pip's --proxy option, then PIP_PROXY, then a proxy in its config files. Any
352// of these makes pip ignore HTTPS_PROXY and NO_PROXY, except that an empty
353// --proxy clears the other two and leaves pip on HTTPS_PROXY. pip 26.3 reads
354// an empty --proxy as a direct connection instead; glab keeps HTTPS_PROXY.
355func (m pyPIManager) resolveUpstreamProxy(environ []string, dir string, args []string) (upstreamProxy, bool, error) {
356 command := ""
357 if m.binary == "pip" {
358 if value, _, ok := pipProxyArg(args); ok {
359 if value == "" {
360 dbg.Debugf("dependency firewall: pip's --proxy is empty, so glab keeps its default outbound proxy")
361 return upstreamProxy{}, false, nil
362 }
363 return upstreamProxy{Source: "--proxy", URL: value}, true, nil
364 }
365 command = pipCommand(args)
366 }
367 return pipUpstreamProxy(environ, dir, m.binary, command, m.binary == "pip" && pipIsolated(args))
368}
370func (cargoManager) proxyVariables() []string { return []string{"CARGO_HTTP_PROXY"} }
372func (cargoManager) proxyConfigFiles() []string {
373 return []string{"$CARGO_HOME/config.toml", "~/.gitconfig"}
374}
376// resolveUpstreamProxy for cargo. CARGO_HTTP_PROXY outranks HTTPS_PROXY, and
377// libcurl still skips it for a host in NO_PROXY or no_proxy (verified against
378// cargo 1.99).
379func (cargoManager) resolveUpstreamProxy(environ []string, dir string, _ []string) (upstreamProxy, bool, error) {
380 return cargoUpstreamProxy(environ, dir)
381}
383func (*mavenManager) proxyVariables() []string { return nil }
385func (*mavenManager) proxyConfigFiles() []string {
386 return []string{"~/.m2/settings.xml", "${maven.home}/conf/settings.xml"}
387}
389func (*mavenManager) proxyHelpNote() string {
390 return "glab doesn't support proxy passwords encrypted with settings-security.xml."
391}
393// resolveUpstreamProxy for Maven: the active <proxy> in the user and global
394// settings files.
395func (*mavenManager) resolveUpstreamProxy(environ []string, dir string, args []string) (upstreamProxy, bool, error) {
396 return mavenUpstreamProxy(environ, dir, args)
397}
399func (*gradleManager) proxyVariables() []string { return nil }
401func (*gradleManager) proxyConfigFiles() []string {
402 return []string{"$GRADLE_USER_HOME/gradle.properties", "the Gradle installation's gradle.properties"}
403}
405func (*gradleManager) proxyHelpNote() string {
406 return "glab also reads them from -D options on the gradle command line."
407}
409// resolveUpstreamProxy for Gradle: the https.proxyHost system properties from
410// the command line and the user and installation gradle.properties.
411func (*gradleManager) resolveUpstreamProxy(environ []string, dir string, args []string) (upstreamProxy, bool, error) {
412 return gradleUpstreamProxy(environ, dir, args)
413}
Diagnostics7 git calls · 275 ms
CallStatusSizeTime0275 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 · 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=2 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=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=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=4 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=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=26d32585a3a474717e00a318584ccd16918c9d32Open pack map
fetchtree:02001.3 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=1327 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=2758711b27b3e124482ce287b5df86092bb20d62 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=1327 git.pkt.payload.max=1300 git.sideband.band1.bytes=1300 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1300 git.pack.trailer=c83fc6004a9a1360845513c0fb16272b5c14a3e4Open pack map
fetch2004.5 KiB263 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=263 git.response.bytes=4592 git.response.cache=MISS git.response.duration_ms=263 git.fetch.wants=9b03249238fc412dffe3ef29310230f8a5104ebf 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=4592 git.pkt.payload.max=4565 git.sideband.band1.bytes=4565 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
- a5e271f35e04d806
- Request
- 6644c056…
- 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