GitLabgitlab-org

cli

main
ply7 git calls231 ms106 KiB4 cachedprotocol v2
871 lines29 KiBRaw
1package proxy
3import (
4 "bufio"
5 "bytes"
6 "cmp"
7 "context"
8 "crypto/rand"
9 "crypto/rsa"
10 "crypto/tls"
11 "crypto/x509"
12 "crypto/x509/pkix"
13 "errors"
14 "fmt"
15 "io"
16 "log"
17 "math/big"
18 "net"
19 "net/http"
20 "net/http/httputil"
21 "net/url"
22 "os"
23 "runtime/debug"
24 "strconv"
25 "strings"
26 "sync"
27 "sync/atomic"
28 "time"
30 "golang.org/x/net/http/httpproxy"
32 "gitlab.com/gitlab-org/cli/internal/dbg"
33 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/policy"
34 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
35 "gitlab.com/gitlab-org/cli/internal/text"
38type certAuthority struct {
39 cert *x509.Certificate
40 key *rsa.PrivateKey
41 certDER []byte
43 mu sync.Mutex
44 cache map[string]*tls.Certificate
47func newCertAuthority() (*certAuthority, error) {
48 key, err := rsa.GenerateKey(rand.Reader, 2048)
49 if err != nil {
50 return nil, err
51 }
52 serial, err := randomSerial()
53 if err != nil {
54 return nil, err
55 }
56 tmpl := &x509.Certificate{
57 SerialNumber: serial,
58 Subject: pkix.Name{CommonName: "glab Dependency Firewall CA"},
59 NotBefore: time.Now().Add(-time.Hour),
60 NotAfter: time.Now().Add(24 * time.Hour),
61 KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature,
62 BasicConstraintsValid: true,
63 IsCA: true,
64 }
65 der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
66 if err != nil {
67 return nil, err
68 }
69 cert, err := x509.ParseCertificate(der)
70 if err != nil {
71 return nil, err
72 }
73 return &certAuthority{cert: cert, key: key, certDER: der, cache: map[string]*tls.Certificate{}}, nil
76// randomSerial returns a cryptographically random 128-bit certificate serial
77// number, as recommended by RFC 5280 §4.1.2.2.
78func randomSerial() (*big.Int, error) {
79 serialBytes := make([]byte, 16)
80 if _, err := rand.Read(serialBytes); err != nil {
81 return nil, err
82 }
83 return new(big.Int).SetBytes(serialBytes), nil
86func (ca *certAuthority) leafFor(host string) (*tls.Certificate, error) {
87 ca.mu.Lock()
88 defer ca.mu.Unlock()
89 if c, ok := ca.cache[host]; ok {
90 return c, nil
91 }
92 key, err := rsa.GenerateKey(rand.Reader, 2048)
93 if err != nil {
94 return nil, err
95 }
96 serial, err := randomSerial()
97 if err != nil {
98 return nil, err
99 }
100 tmpl := &x509.Certificate{
101 SerialNumber: serial,
102 Subject: pkix.Name{CommonName: host},
103 NotBefore: time.Now().Add(-time.Hour),
104 NotAfter: time.Now().Add(24 * time.Hour),
105 KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
106 ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
107 DNSNames: []string{host},
108 }
109 der, err := x509.CreateCertificate(rand.Reader, tmpl, ca.cert, &key.PublicKey, ca.key)
110 if err != nil {
111 return nil, err
112 }
113 leaf := &tls.Certificate{Certificate: [][]byte{der, ca.certDER}, PrivateKey: key}
114 ca.cache[host] = leaf
115 return leaf, nil
118type Proxy struct {
119 ca *certAuthority
120 listener net.Listener
121 server *http.Server
122 upstream *http.Transport
123 matcher Matcher
124 checker policy.Checker
126 // upstreamProxyCert is applied after every option has run, so it wraps
127 // whichever Proxy func and roots they set.
128 upstreamProxyCert *tls.Certificate
130 projectID string
132 // ctx is the proxy-lifetime context; it bounds policy checks made on
133 // hijacked MITM tunnels, which have no request-scoped context of their
134 // own. cancel tears it down on Stop so in-flight checks are cancelled.
135 ctx context.Context
136 cancel context.CancelFunc
138 // inflight tracks MITM tunnels and plain-HTTP handlers. Shutdown skips
139 // hijacked connections and Close waits for nothing, so Stop drains this
140 // (bounded) before Verdicts(), or a late Block never reaches the CI log.
141 inflight sync.WaitGroup
143 mu sync.Mutex
144 verdicts []verdict.Entry
145 seen map[string]struct{}
147 warnUpstreamFailure policy.Logf
148 blockNotice policy.Logf
149 warnMu sync.Mutex
150 warnedAuthorities map[string]struct{}
152 onVerdict func(verdict.Entry)
155// tunnelDrainTimeout bounds each of Stop's waits after the proxy context is
156// cancelled: for plain-HTTP handlers to return, then for in-flight MITM
157// tunnels to finish. A hung upstream or a client that will not close should
158// not block shutdown forever; the cancelled context already unblocks policy
159// checks, so a well-behaved request finishes well within this window.
160const tunnelDrainTimeout = 5 * time.Second
162// Option configures a Proxy at construction time.
163type Option func(*Proxy)
165// WithUpstreamRootCAs overrides the certificate pool the proxy uses to verify
166// the upstream registry's TLS certificate, and the certificate of an https://
167// outbound proxy in front of it. When unset, the proxy verifies against the
168// system trust store.
169func WithUpstreamRootCAs(pool *x509.CertPool) Option {
170 return func(p *Proxy) {
171 p.upstream.TLSClientConfig = &tls.Config{
172 RootCAs: pool,
173 MinVersion: tls.VersionTLS12,
174 NextProtos: []string{"http/1.1"}, // see New(): tunnel is HTTP/1.1
175 }
176 }
179// WithUpstreamProxy overrides how the proxy picks an outbound proxy for each
180// upstream registry request. When unset, it uses upstreamProxyFromEnvironment.
181func WithUpstreamProxy(proxyFunc func(*http.Request) (*url.URL, error)) Option {
182 return func(p *Proxy) {
183 p.upstream.Proxy = proxyFunc
184 }
187// WithUpstreamProxyFor overrides the outbound proxy for upstream registry
188// requests whose URL has the given scheme, such as a package manager's own
189// proxy setting. Requests with any other scheme keep the proxy picked before.
190func WithUpstreamProxyFor(scheme string, proxyFunc func(*http.Request) (*url.URL, error)) Option {
191 return func(p *Proxy) {
192 next := p.upstream.Proxy
193 p.upstream.Proxy = func(r *http.Request) (*url.URL, error) {
194 if r.URL.Scheme == scheme {
195 return proxyFunc(r)
196 }
197 return next(r)
198 }
199 }
202// upstreamProxyFromEnvironment reads glab's environment like
203// http.ProxyFromEnvironment, but a plain-HTTP request falls back to
204// HTTPS_PROXY, the outbound proxy glab documents, when HTTP_PROXY is unset.
205func upstreamProxyFromEnvironment() func(*http.Request) (*url.URL, error) {
206 // httpproxy.FromEnvironment reads the lowercase names first, unlike
207 // http.ProxyFromEnvironment and the rest of glab.
208 config := &httpproxy.Config{
209 HTTPProxy: cmp.Or(os.Getenv("HTTP_PROXY"), os.Getenv("http_proxy")),
210 HTTPSProxy: cmp.Or(os.Getenv("HTTPS_PROXY"), os.Getenv("https_proxy")),
211 NoProxy: cmp.Or(os.Getenv("NO_PROXY"), os.Getenv("no_proxy")),
212 CGI: os.Getenv("REQUEST_METHOD") != "",
213 }
214 // In a CGI environment httpproxy refuses any HTTP_PROXY value, so keep going direct there.
215 if config.HTTPProxy == "" && !config.CGI {
216 config.HTTPProxy = config.HTTPSProxy
217 }
218 pick := config.ProxyFunc()
219 return func(r *http.Request) (*url.URL, error) { return pick(r.URL) }
222// WithUpstreamFailureWarning sets where the proxy reports a failed upstream
223// registry request. It reports each registry authority at most once per
224// proxy. When unset, failures are only debug-logged.
225func WithUpstreamFailureWarning(warn policy.Logf) Option {
226 return func(p *Proxy) {
227 p.warnUpstreamFailure = warn
228 }
231// WithVerdictHook registers fn to be called once per newly recorded verdict, as
232// it is recorded and before any blocked response is written to the client. The
233// job-level proxy uses it to append each verdict to the CI log synchronously, so
234// a `ci-summary` that runs immediately after a blocked install still sees the
235// block. fn runs on a tunnel goroutine, must be safe for concurrent use, and
236// must not call back into the proxy.
237func WithVerdictHook(fn func(verdict.Entry)) Option {
238 return func(p *Proxy) { p.onVerdict = fn }
241// WithBlockNotice sets where the proxy reports each package it blocks, at the
242// moment it blocks it, so the reason appears next to the package manager's
243// own failure rather than only in the summary after it exits.
244func WithBlockNotice(notice policy.Logf) Option {
245 return func(p *Proxy) {
246 p.blockNotice = notice
247 }
250// WithUpstreamProxyCertificate presents cert on the TLS handshake with an
251// https:// outbound proxy that requires a client certificate. Registries never
252// receive it, and the proxy is still verified against the upstream roots.
253func WithUpstreamProxyCertificate(cert tls.Certificate) Option {
254 return func(p *Proxy) {
255 p.upstreamProxyCert = &cert
256 }
259func New(matcher Matcher, checker policy.Checker, projectID string, opts ...Option) (*Proxy, error) {
260 ca, err := newCertAuthority()
261 if err != nil {
262 return nil, err
263 }
264 p := &Proxy{
265 ca: ca,
266 // Upstream verification uses the system trust store by default; the
267 // package manager verifies the proxy via its configured CA bundle, and
268 // the proxy in turn verifies the real registry.
269 //
270 // Force HTTP/1.1 upstream: the tunnel we serve back to the client is
271 // HTTP/1.1 (bufio.NewReader + http.ReadRequest + resp.Write), so an
272 // HTTP/2 response from the upstream would be re-serialized as HTTP/2
273 // framing over an HTTP/1 tunnel and the client sees
274 // `UnknownProtocol('HTTP/2.0')`.
275 //
276 // Only the package manager's environment points at this proxy, so the
277 // caller's HTTPS_PROXY in glab's own environment cannot loop back here.
278 upstream: &http.Transport{
279 Proxy: upstreamProxyFromEnvironment(),
280 TLSClientConfig: &tls.Config{
281 MinVersion: tls.VersionTLS12,
282 NextProtos: []string{"http/1.1"},
283 },
284 TLSNextProto: map[string]func(string, *tls.Conn) http.RoundTripper{},
285 },
286 matcher: matcher,
287 checker: checker,
288 projectID: projectID,
289 seen: map[string]struct{}{},
290 warnedAuthorities: map[string]struct{}{},
291 }
292 p.ctx, p.cancel = context.WithCancel(context.Background())
293 for _, opt := range opts {
294 opt(p)
295 }
296 if p.upstreamProxyCert != nil {
297 presentCertToProxy(p.upstream, *p.upstreamProxyCert)
298 }
299 return p, nil
302func (p *Proxy) CACertificate() *x509.Certificate { return p.ca.cert }
304func (p *Proxy) Addr() string {
305 if p.listener == nil {
306 return ""
307 }
308 return p.listener.Addr().String()
311func (p *Proxy) Start() error {
312 ln, err := net.Listen("tcp", "127.0.0.1:0")
313 if err != nil {
314 return err
315 }
316 p.listener = ln
317 p.server = &http.Server{
318 Handler: http.HandlerFunc(p.handle),
319 ReadHeaderTimeout: 30 * time.Second,
320 }
321 go func() { _ = p.server.Serve(ln) }()
322 return nil
325func (p *Proxy) Stop() {
326 if p.cancel != nil {
327 p.cancel()
328 }
329 if p.server != nil {
330 // A plain-HTTP handler forwarding an upload stays blocked reading the
331 // client's body even once cancelled, so a stalled client could hold
332 // Shutdown open forever.
333 ctx, cancel := context.WithTimeout(context.Background(), tunnelDrainTimeout)
334 defer cancel()
335 if err := p.server.Shutdown(ctx); err != nil {
336 dbg.Debugf("dependency firewall proxy shutdown: %v", err)
337 _ = p.server.Close()
338 }
339 }
340 // Drain inflight (bounded) before the caller reads Verdicts().
341 done := make(chan struct{})
342 go func() {
343 p.inflight.Wait()
344 close(done)
345 }()
346 select {
347 case <-done:
348 case <-time.After(tunnelDrainTimeout):
349 dbg.Debugf("dependency firewall proxy: timed out draining in-flight requests")
350 }
353func (p *Proxy) Verdicts() []verdict.Entry {
354 p.mu.Lock()
355 defer p.mu.Unlock()
356 out := make([]verdict.Entry, len(p.verdicts))
357 copy(out, p.verdicts)
358 return out
361func (p *Proxy) record(e verdict.Entry) bool {
362 p.mu.Lock()
363 if _, ok := p.seen[e.Key()]; ok {
364 p.mu.Unlock()
365 return false
366 }
367 p.seen[e.Key()] = struct{}{}
368 p.verdicts = append(p.verdicts, e)
369 hook := p.onVerdict
370 p.mu.Unlock()
371 // Called outside the lock: the hook may do I/O — the job-level proxy writes
372 // the CI log here so a verdict is durable before the blocked response reaches
373 // the client — and must not re-enter record while the lock is held.
374 if hook != nil {
375 hook(e)
376 }
377 return true
380func (p *Proxy) recordBlock(e verdict.Entry, checkFailed bool) {
381 // The caching checker already warns about a failed check, and its reason
382 // is the raw API error.
383 if !p.record(e) || checkFailed || p.blockNotice == nil {
384 return
385 }
386 msg := strings.TrimSpace("dependency firewall blocked " + text.SanitizeInline(e.Package) + " " + text.SanitizeInline(e.Version))
387 if reason := strings.TrimSuffix(text.SanitizeInline(e.Reason), "."); reason != "" {
388 msg += ": " + reason
389 }
390 p.warnMu.Lock()
391 defer p.warnMu.Unlock()
392 p.blockNotice("%s. The summary at the end of this command lists every blocked package.\n", msg)
395func (p *Proxy) warnUpstreamFailureOnce(authority string, err error) {
396 if p.warnUpstreamFailure == nil {
397 return
398 }
399 // The sink runs under warnMu so tunnels never write to it concurrently.
400 p.warnMu.Lock()
401 defer p.warnMu.Unlock()
402 if _, ok := p.warnedAuthorities[authority]; ok {
403 return
404 }
405 p.warnedAuthorities[authority] = struct{}{}
406 p.warnUpstreamFailure("warning: dependency firewall request to registry %s failed: %v\n", authority, err)
409// inboundBody records whether reading the package manager's request body
410// failed, which the upstream round trip reports as its own error.
411type inboundBody struct {
412 io.ReadCloser
413 readFailed atomic.Bool
416func (b *inboundBody) Read(buf []byte) (int, error) {
417 n, err := b.ReadCloser.Read(buf)
418 if err != nil && !errors.Is(err, io.EOF) {
419 b.readFailed.Store(true)
420 }
421 return n, err
424func (p *Proxy) handle(w http.ResponseWriter, r *http.Request) {
425 switch {
426 case r.Method == http.MethodConnect:
427 p.handleConnect(w, r)
428 case r.URL.IsAbs() && r.URL.Scheme == "http":
429 p.handlePlain(w, r)
430 default:
431 http.Error(w, "only CONNECT and absolute-form http:// requests are supported", http.StatusMethodNotAllowed)
432 }
435// plainBlockDrainTimeout keeps a stalled blocked upload from holding its connection open.
436const plainBlockDrainTimeout = 2 * time.Second
438var errUnpromptedSwitch = errors.New("registry switched protocols without being asked")
440// handlePlain serves an absolute-form request for an http:// URL, which a
441// client sends to its proxy in place of CONNECT, through the same policy
442// check as a tunnelled request.
443func (p *Proxy) handlePlain(w http.ResponseWriter, r *http.Request) {
444 p.inflight.Add(1)
445 defer p.inflight.Done()
447 // Drop any upgrade offer rather than refuse it: libcurl offers h2c on every
448 // request. With no Upgrade header ReverseProxy answers an upstream 101 with
449 // 502 instead of splicing the client past the policy check.
450 r.Header.Del("Upgrade")
452 // Stop cancels the proxy context; tie this request to it so a slow check
453 // or upstream round trip ends then. A client stalling mid-upload is left
454 // to Stop's bounded Shutdown.
455 ctx, cancel := context.WithCancel(r.Context())
456 defer cancel()
457 defer context.AfterFunc(p.ctx, cancel)() //nolint:contextcheck // Stop's proxy context must also end this request
458 r = r.WithContext(ctx)
460 if ecosystem, reason, _, ok := p.admit(ctx, r); !ok {
461 contentType, body := blockBody(ecosystem, reason)
462 w.Header().Set("Content-Type", contentType)
463 w.Header().Set("Content-Length", strconv.Itoa(len(body)))
464 w.Header().Set(firewallHeader, "blocked")
465 w.Header().Set("Connection", "close")
466 w.WriteHeader(http.StatusForbidden)
467 _, _ = io.WriteString(w, body)
468 // Flush before draining so a client still uploading reads the 403 at once.
469 rc := http.NewResponseController(w)
470 _ = rc.Flush()
471 _ = rc.SetReadDeadline(time.Now().Add(plainBlockDrainTimeout))
472 drainAndClose(r.Body)
473 return
474 }
475 authority := r.URL.Host
476 rp := &httputil.ReverseProxy{
477 Rewrite: func(pr *httputil.ProxyRequest) {
478 pr.Out.Header.Set(firewallHeader, "allowed")
479 },
480 Transport: p.upstream,
481 // Any 101 is unprompted once Upgrade is dropped. Refusing it here makes
482 // ReverseProxy close the upstream connection, which its own 502 leaves open.
483 ModifyResponse: func(res *http.Response) error {
484 if res.StatusCode == http.StatusSwitchingProtocols {
485 return errUnpromptedSwitch
486 }
487 return nil
488 },
489 ErrorLog: log.New(debugLog{}, "", 0),
490 ErrorHandler: func(w http.ResponseWriter, req *http.Request, err error) {
491 dbg.Debugf("dependency firewall proxy: upstream request to %s failed: %v", authority, err)
492 // net/http cancels the request when the client's connection drops,
493 // as Stop does; neither is a registry failure.
494 if req.Context().Err() == nil {
495 p.warnUpstreamFailureOnce(authority, err)
496 }
497 w.WriteHeader(http.StatusBadGateway)
498 },
499 }
500 rp.ServeHTTP(w, r)
503// debugLog sends ReverseProxy's own messages, such as a registry dropping the
504// connection mid-response, to the debug log rather than glab's stderr.
505type debugLog struct{}
507func (debugLog) Write(b []byte) (int, error) {
508 dbg.Debugf("dependency firewall proxy: %s", bytes.TrimSuffix(b, []byte("\n")))
509 return len(b), nil
512func (p *Proxy) handleConnect(w http.ResponseWriter, r *http.Request) {
513 authority := r.Host
514 if r.URL.Host != "" {
515 authority = r.URL.Host
516 }
517 host, _, err := net.SplitHostPort(authority)
518 if err != nil {
519 host = authority
520 }
522 hijacker, ok := w.(http.Hijacker)
523 if !ok {
524 http.Error(w, "hijack unsupported", http.StatusInternalServerError)
525 return
526 }
527 clientConn, _, err := hijacker.Hijack()
528 if err != nil {
529 return
530 }
531 defer clientConn.Close()
533 _, _ = clientConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n"))
535 leaf, err := p.ca.leafFor(host)
536 if err != nil {
537 return
538 }
539 tlsConn := tls.Server(clientConn, &tls.Config{Certificates: []tls.Certificate{*leaf}}) //nolint:gosec // leaf cert dictates negotiated version; min version not required for localhost MITM
540 if err := tlsConn.Handshake(); err != nil {
541 return
542 }
543 defer tlsConn.Close()
545 // A hijacked MITM tunnel outlives the CONNECT request and serves many
546 // requests, so r.Context() (cancelled when handleConnect returns) is the
547 // wrong lifetime. Use the proxy-lifetime context, which Stop cancels.
548 //
549 // Track the tunnel so Stop can drain in-flight ones before the caller
550 // snapshots Verdicts(): http.Server.Shutdown does not wait for hijacked
551 // connections, so without this a policy check completing after the child
552 // exits could record a Block that never reaches the CI log.
553 p.inflight.Add(1)
554 defer p.inflight.Done()
555 p.serveTunnel(p.ctx, tlsConn, authority) //nolint:contextcheck // MITM tunnel is bounded by the proxy lifetime, not the CONNECT request
558func (p *Proxy) serveTunnel(ctx context.Context, conn net.Conn, authority string) {
559 br := bufio.NewReader(conn)
560 for {
561 req, err := http.ReadRequest(br)
562 if err != nil {
563 return
564 }
566 req.URL.Scheme = "https"
567 req.URL.Host = authority
568 req.RequestURI = ""
569 req.Header.Del("Accept-Encoding")
571 if ecosystem, reason, checkFailed, ok := p.admit(ctx, req); !ok {
572 _, _ = io.WriteString(conn, blockResponse(ecosystem, reason, checkFailed))
573 drainAndClose(req.Body)
574 return
575 }
576 req.Header.Set(firewallHeader, "allowed")
577 var inbound *inboundBody
578 if req.Body != nil && req.Body != http.NoBody {
579 inbound = &inboundBody{ReadCloser: req.Body}
580 req.Body = inbound
581 }
583 resp, err := p.upstream.RoundTrip(req)
584 if err != nil {
585 dbg.Debugf("dependency firewall proxy: upstream request to %s failed: %v", authority, err)
586 // A package manager that aborts its own upload is not a registry failure.
587 if inbound == nil || !inbound.readFailed.Load() {
588 p.warnUpstreamFailureOnce(authority, err)
589 }
590 return
591 }
593 if isStreamable(resp) {
594 err := resp.Write(conn)
595 resp.Body.Close()
596 if err != nil {
597 return
598 }
599 continue
600 }
602 body, err := io.ReadAll(resp.Body)
603 resp.Body.Close()
604 if err != nil {
605 return
606 }
607 resp.Body = io.NopCloser(bytes.NewReader(body))
608 resp.ContentLength = int64(len(body))
609 if resp.Uncompressed {
610 resp.Header.Del("Content-Encoding")
611 }
612 resp.Header.Set("Content-Length", strconv.Itoa(len(body)))
613 resp.TransferEncoding = nil
614 if err := resp.Write(conn); err != nil {
615 return
616 }
617 }
620// admit matches req and checks it against policy, recording the verdict. It
621// returns ok=false when req must not reach the upstream, with the ecosystem,
622// the reason to show the package manager, and whether the policy check itself
623// failed.
624func (p *Proxy) admit(ctx context.Context, req *http.Request) (string, string, bool, bool) {
625 // Match before the round trip: an upload carries its identity in the
626 // body, which the matcher reads and restores before RoundTrip.
627 m := p.matcher.Match(req)
629 if m.Matched && !m.Pass {
630 // The matcher recognized an in-scope request but did not clear it
631 // for the policy check — e.g. an over-limit upload body it could
632 // not inspect. Fail closed.
633 p.recordBlock(verdict.Entry{
634 Package: m.Coordinate.Name,
635 Version: m.Coordinate.Version,
636 Verdict: verdict.Blocked,
637 Status: http.StatusForbidden,
638 Reason: m.Reason,
639 }, false)
640 return m.Coordinate.Ecosystem, m.Reason, false, false
641 }
642 if !m.Matched {
643 return "", "", false, true
644 }
645 res, coordinate := p.checkPolicy(ctx, m)
646 if res.Blocked() {
647 p.recordBlock(verdict.Entry{
648 Package: coordinate.Name,
649 Version: coordinate.Version,
650 Verdict: verdict.Blocked,
651 Status: http.StatusForbidden,
652 Reason: res.Reason,
653 }, res.CheckFailed)
654 reason := res.Reason
655 switch {
656 case res.Internal:
657 reason = policy.InternalErrorPhrase(coordinate)
658 case res.CheckFailed:
659 reason = checkFailedReason
660 }
661 return m.Coordinate.Ecosystem, reason, res.CheckFailed, false
662 }
663 if res.Verdict == verdict.Warning {
664 // Status is left unset: a warning allows the request through, but
665 // the upstream round trip has not happened yet, so the real response
666 // status is unknown here. Recording StatusOK would be a guess that
667 // could contradict a later non-200 upstream response.
668 p.record(verdict.Entry{
669 Package: coordinate.Name,
670 Version: coordinate.Version,
671 Verdict: verdict.Warning,
672 Reason: res.Reason,
673 })
674 }
675 return "", "", false, true
678// maxConcurrentChecks bounds how many of one request's candidate coordinates
679// are checked at once, so enumeration doesn't flood the policy endpoint.
680const maxConcurrentChecks = 4
682// checkPolicy checks every candidate coordinate m carries and returns the
683// strongest verdict, with the coordinate it belongs to; a block always wins.
684// All candidates share one overall policy.CheckTimeout deadline so a slow
685// backend bounds total latency instead of compounding per candidate. The
686// primary coordinate goes first, so a block on it or a failing backend costs
687// one call; the alternates then start in order, a few at a time, and the loop
688// starts no more once it sees one blocked or failed. The verdicts are read in
689// candidate order, so the result doesn't depend on which check returns first.
690func (p *Proxy) checkPolicy(ctx context.Context, m Match) (policy.Result, policy.Coordinate) {
691 ctx, cancel := context.WithTimeout(ctx, policy.CheckTimeout)
692 defer cancel()
694 candidates := append([]policy.Coordinate{m.Coordinate}, m.Alternates...)
695 results := make([]policy.Result, len(candidates))
696 errs := make([]error, len(candidates))
697 var decided atomic.Bool
698 check := func(i int) {
699 // An alternate's goroutine is outside net/http's recover, so a panicking
700 // checker would end glab. The value stays out of the block reason.
701 defer func() {
702 if r := recover(); r != nil {
703 dbg.Debugf("dependency firewall policy check panicked for %s: %v\n%s", candidates[i].Key(), r, debug.Stack())
704 errs[i] = policy.ErrCheckPanicked
705 }
706 if errs[i] != nil || results[i].Blocked() {
707 decided.Store(true)
708 }
709 }()
710 results[i], errs[i] = p.checker.Check(ctx, policy.Request{Coordinate: candidates[i], ProjectID: p.projectID, Operation: m.Operation})
711 }
712 check(0)
713 if !decided.Load() {
714 slots := make(chan struct{}, maxConcurrentChecks)
715 var wg sync.WaitGroup
716 for i := 1; i < len(candidates); i++ {
717 slots <- struct{}{}
718 // Candidates start in order, so a decided one is earlier than i
719 // and the read below returns before reaching i.
720 if decided.Load() {
721 break
722 }
723 wg.Go(func() {
724 defer func() { <-slots }()
725 check(i)
726 })
727 }
728 wg.Wait()
729 }
731 strongest := policy.Result{}
732 strongestCoordinate := m.Coordinate
734 for i, coordinate := range candidates {
735 res, err := results[i], errs[i]
736 if errors.Is(err, policy.ErrCheckPanicked) {
737 return policy.InternalErrorResult(m.Coordinate), m.Coordinate
738 }
739 if err != nil {
740 // Fail closed, naming the request's own coordinate rather than the
741 // invented candidate that errored.
742 dbg.Debugf("dependency firewall policy check failed for %s: %v", coordinate.Key(), err)
743 return policy.Result{
744 Verdict: verdict.Blocked,
745 Reason: fmt.Sprintf("policy check failed: %v", err),
746 CheckFailed: true,
747 }, m.Coordinate
748 }
749 if res.Blocked() {
750 // A failed check says nothing about the candidate, so name the request.
751 if res.Internal {
752 return policy.InternalErrorResult(m.Coordinate), m.Coordinate
753 }
754 if res.CheckFailed {
755 return res, m.Coordinate
756 }
757 return res, coordinate
758 }
759 if res.Verdict == verdict.Warning && strongest.Verdict == verdict.Allowed {
760 strongest, strongestCoordinate = res, coordinate
761 }
762 }
763 return strongest, strongestCoordinate
766// blockDrainLimit bounds how much of a blocked upload's request body the proxy
767// reads and discards before closing the connection. On a blocked upload the
768// client may still be streaming its (potentially large) body; a bare Close
769// leaves those bytes unread, so some clients see a connection reset and never
770// read the synthesized 403. Draining a bounded amount lets the client's write
771// drain and its read of the 403 succeed, while the cap keeps a hostile or huge
772// upload from tying up the tunnel goroutine.
773const blockDrainLimit = 1 << 20 // 1 MiB
775// drainAndClose reads and discards up to blockDrainLimit bytes from body, then
776// closes it. body may be nil. It is used after a synthesized 403 so the client
777// reliably reads the block response instead of hitting a connection reset from
778// unread request bytes.
779func drainAndClose(body io.ReadCloser) {
780 if body == nil {
781 return
782 }
783 _, _ = io.Copy(io.Discard, io.LimitReader(body, blockDrainLimit))
784 _ = body.Close()
787// binaryArtifactExts are the download suffixes that identify a package
788// artifact payload across ecosystems. A response for one of these is streamed
789// straight to the client rather than buffered for inspection, regardless of
790// its Content-Type: some registries serve them with a text or otherwise
791// unhelpful type (for example files.pythonhosted.org serves wheels as
792// binary/octet-stream and crates.io serves .crate files as application/gzip).
793//
794// The Maven entries are the binary members of mavenPrimaryExts; ".pom" is
795// omitted because it is small XML metadata, not an artifact payload.
796var binaryArtifactExts = []string{
797 ".whl", ".crate", ".tgz", ".tar.gz", ".tar.bz2", ".zip", ".gem",
798 ".jar", ".war", ".aar", ".nupkg",
801// binaryContentTypes are the Content-Type prefixes that identify a binary
802// artifact payload. These cover the octet-stream and archive types registries
803// return for package downloads, including the application/java-archive that
804// repo1.maven.org returns for .jar/.war. Metadata/index responses
805// (application/json, text/html) are deliberately excluded so they still take
806// the buffer-and-rewrite path that fixes up Content-Length after the
807// transport's automatic decompression.
808var binaryContentTypes = []string{
809 "application/octet-stream",
810 "binary/octet-stream",
811 "application/gzip",
812 "application/x-gzip",
813 "application/zip",
814 "application/java-archive",
817// isStreamable reports whether resp is a binary package payload that can be
818// streamed straight to the client rather than buffered for the
819// Content-Length/Content-Encoding fixup. Buffering large artifacts would stall
820// the tunnel and can trip a client read-timeout on multi-megabyte downloads,
821// so genuine artifacts are streamed; but the streaming path writes the
822// response verbatim, so it is only safe when the response is self-delimiting
823// and needs no rewrite:
824//
825// - Known Content-Length. The proxy strips the client's Accept-Encoding and
826// the transport may transparently decompress the body, leaving
827// ContentLength == -1. Writing an unknown-length body over the keep-alive
828// HTTP/1.1 tunnel gives the client no framing to detect end-of-body, so it
829// stalls until its read timeout. Those responses must take the buffered
830// path, which sets Content-Length.
831// - Not transport-decompressed (resp.Uncompressed == false). When the
832// transport gunzipped the body it also dropped Content-Length and left a
833// now-inaccurate Content-Encoding, both of which only the buffered path
834// repairs.
835//
836// Real artifacts (wheels, crates, tarballs, gems, jars) are served
837// pre-compressed with an accurate Content-Length and are not
838// transport-decompressed, so they stream; metadata/index responses and the
839// PEP 658 ".whl.metadata" sidecar (served without a length and often gzipped)
840// fall through to buffering.
841func isStreamable(resp *http.Response) bool {
842 if resp.StatusCode < 200 || resp.StatusCode >= 300 {
843 return false
844 }
845 if resp.ContentLength < 0 || resp.Uncompressed {
846 return false
847 }
848 if !isBinaryArtifact(resp) {
849 return false
850 }
851 return true
854// isBinaryArtifact reports whether resp looks like a binary package payload,
855// by request path suffix or Content-Type. Metadata/index responses
856// (application/json, text/html) are deliberately excluded.
857func isBinaryArtifact(resp *http.Response) bool {
858 path := resp.Request.URL.Path
859 for _, ext := range binaryArtifactExts {
860 if strings.HasSuffix(path, ext) {
861 return true
862 }
863 }
864 contentType := resp.Header.Get("Content-Type")
865 for _, ct := range binaryContentTypes {
866 if strings.HasPrefix(contentType, ct) {
867 return true
868 }
869 }
870 return false
Diagnostics7 git calls · 231 ms
CallStatusSizeTime0231 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 KiB2 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=UPDATING
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 · 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 · 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=361
git.response.cache=HIT
git.response.duration_ms=3
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
fetch20010.0 KiB220 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=219
git.response.bytes=10218
git.response.cache=MISS
git.response.duration_ms=220
git.fetch.wants=61c3b1cb47b3ba8542888670156c67c7f7d1f0b5
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=10218
git.pkt.payload.max=10191
git.sideband.band1.bytes=10191
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
a5e198c3fce3b86e
Request
682eab5a…
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