GitLabgitlab-org
cli
main
1package api
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "io"
10 "mime"
11 "net/http"
12 "net/http/httputil"
13 "net/url"
14 "slices"
16 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
17)
19var sensitiveHeaders = []string{
20 gitlab.AccessTokenHeaderName,
21 gitlab.JobTokenHeaderName,
22 "Authorization",
23 "Proxy-Authorization",
24 "Cf-Access-Client-Secret",
25}
27type debugTransport struct {
28 rt http.RoundTripper
29 w io.Writer
30}
32func (d *debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
33 reqDump, err := dumpRequestRedacted(req)
34 if err != nil {
35 return nil, err
36 }
37 fmt.Fprintf(d.w, "REQUEST:\n%s\n\n", reqDump)
39 // Do request
40 resp, err := d.rt.RoundTrip(req)
41 if err != nil {
42 return nil, err
43 }
45 respDump, err := dumpResponseRedacted(resp)
46 if err != nil {
47 return nil, err
48 }
49 fmt.Fprintf(d.w, "RESPONSE:\n%s\n\n", respDump)
51 return resp, nil
52}
54// sensitiveBodyFields are the fields that carry a credential, in a JSON body
55// at any depth, a form-encoded body or a query string. In responses: the
56// token returned by an agent session token mint, `glab artifact-registry
57// get-token`, `glab cluster agent get-token`, or `glab token create`; the
58// tokens of an OAuth token response and the device code of an OAuth device
59// authorization; the runner registration token in the project and group
60// responses a maintainer gets; and the trigger tokens in a project's trigger
61// list or a GraphQL response. In requests: the pipeline trigger token that
62// `glab ci run-trig` sends, and the private_token and job_token parameters
63// GitLab accepts in place of a token header.
64var sensitiveBodyFields = []string{"token", "access_token", "refresh_token", "id_token", "runners_token", "device_code", "private_token", "job_token"}
66// sensitiveFormFields add the OAuth fields that glab's own login and token
67// refresh send to /oauth/token as a form. They are not matched in JSON, where
68// a field named code usually holds code.
69var sensitiveFormFields = slices.Concat(sensitiveBodyFields, []string{"code", "code_verifier", "client_secret"})
71// dumpRequestRedacted dumps req with sensitive headers, query parameters and
72// body fields redacted, and leaves req for the server unchanged. A dump that
73// fails is noted in the output, so it never stops the request.
74func dumpRequestRedacted(req *http.Request) ([]byte, error) {
75 r1, r2, err := drainBody(req.Body)
76 if err != nil {
77 return nil, err
78 }
79 req.Body = r2
81 cloned := req.Clone(context.Background())
82 cloned.Body = r1
83 for _, h := range sensitiveHeaders {
84 if cloned.Header.Get(h) != "" {
85 cloned.Header.Set(h, "[REDACTED]")
86 }
87 }
88 cloned.URL.RawQuery = string(redactFormFields([]byte(cloned.URL.RawQuery)))
90 var body []byte
91 if r1 != http.NoBody {
92 if body, err = io.ReadAll(r1); err != nil {
93 return nil, err
94 }
95 cloned.Body = io.NopCloser(bytes.NewReader(body))
96 }
97 dump, err := dumpRedacted(httputil.DumpRequestOut, cloned, body, redactBody(cloned.Header, body))
98 if err != nil {
99 dump = fmt.Appendf(nil, "(request not dumped: %v)\n", err)
100 }
101 return dump, nil
102}
104// dumpResponseRedacted dumps resp with sensitive body fields redacted, and
105// leaves resp's body for the caller unchanged.
106func dumpResponseRedacted(resp *http.Response) ([]byte, error) {
107 if resp.Body == nil || resp.Body == http.NoBody {
108 return httputil.DumpResponse(resp, true)
109 }
110 body, err := io.ReadAll(resp.Body)
111 err = errors.Join(err, resp.Body.Close())
112 if err != nil {
113 return nil, err
114 }
115 resp.Body = io.NopCloser(bytes.NewReader(body))
116 return dumpRedacted(httputil.DumpResponse, resp, body, redactBody(resp.Header, body))
117}
119// dumpRedacted dumps msg exactly as httputil does when redacting left its
120// body unchanged. Otherwise it dumps msg's headers as sent, so Content-Length
121// stays the wire value, followed by the redacted body.
122func dumpRedacted[T any](dump func(T, bool) ([]byte, error), msg T, body, redacted []byte) ([]byte, error) {
123 if bytes.Equal(body, redacted) {
124 return dump(msg, true)
125 }
126 head, err := dump(msg, false)
127 if err != nil {
128 return nil, err
129 }
130 return append(head, redacted...), nil
131}
133// redactBody redacts the sensitive fields of a form-encoded body, or of a JSON
134// body at any depth. Any other body is returned unchanged.
135func redactBody(header http.Header, body []byte) []byte {
136 if mediaType, _, _ := mime.ParseMediaType(header.Get("Content-Type")); mediaType == "application/x-www-form-urlencoded" {
137 return redactFormFields(body)
138 }
139 return redactBodyFields(body)
140}
142// redactBodyFields replaces the values of sensitive fields at any depth of a
143// JSON body, in place, so every other byte is dumped as sent. An object under
144// a sensitive field is searched rather than replaced, and null is kept. A body
145// that is not JSON, and anything after the part that parses, is kept.
146func redactBodyFields(body []byte) []byte {
147 dec := json.NewDecoder(bytes.NewReader(body))
148 dec.UseNumber() // so a number such as 1e400 does not stop the walk
149 type container struct {
150 delim json.Delim
151 // sensitive is set on an array that is the value of a sensitive field,
152 // so every value in it is replaced.
153 sensitive bool
154 }
155 var stack []container
156 var out []byte
157 var done int64
158 key, wantKey := "", false
159 for {
160 start := dec.InputOffset()
161 tok, err := dec.Token()
162 if err != nil {
163 break
164 }
165 inObject := len(stack) > 0 && stack[len(stack)-1].delim == '{'
166 sensitive := len(stack) > 0 && (inObject && slices.Contains(sensitiveBodyFields, key) || !inObject && stack[len(stack)-1].sensitive)
167 switch tok := tok.(type) {
168 case json.Delim:
169 if tok == '{' || tok == '[' {
170 stack = append(stack, container{tok, sensitive})
171 } else {
172 stack = stack[:len(stack)-1]
173 }
174 wantKey = len(stack) > 0 && stack[len(stack)-1].delim == '{'
175 continue
176 case string:
177 if wantKey {
178 key, wantKey = tok, false
179 continue
180 }
181 case nil:
182 sensitive = false
183 }
184 wantKey = inObject
185 if sensitive {
186 // Only whitespace and the ':' or ',' before it precede the value.
187 end := dec.InputOffset()
188 start = end - int64(len(bytes.TrimLeft(body[start:end], " \t\r\n:,")))
189 out = append(append(out, body[done:start]...), `"[REDACTED]"`...)
190 done = end
191 }
192 }
193 if out == nil {
194 return body
195 }
196 return append(out, body[done:]...)
197}
199// redactFormFields replaces the sensitive values of a form-encoded body or
200// query string in place, so the dump keeps the field order and encoding that
201// was sent.
202func redactFormFields(body []byte) []byte {
203 pairs := bytes.Split(body, []byte("&"))
204 redacted := false
205 for i, pair := range pairs {
206 key, _, _ := bytes.Cut(pair, []byte("="))
207 name, err := url.QueryUnescape(string(key))
208 if err != nil {
209 name = string(key)
210 }
211 if slices.Contains(sensitiveFormFields, name) {
212 pairs[i] = []byte(string(key) + "=[REDACTED]")
213 redacted = true
214 }
215 }
216 if !redacted {
217 return body
218 }
219 return bytes.Join(pairs, []byte("&"))
220}
222// The code below is copied from net/http/httputil/dump.go
223// Copyright 2009 The Go Authors. All rights reserved.
225// drainBody reads all of b to memory and then returns two equivalent
226// ReadClosers yielding the same bytes.
227//
228// It returns an error if the initial slurp of all bytes fails. It does not attempt
229// to make the returned ReadClosers have identical error-matching behavior.
230func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) { //nolint:nonamedreturns
231 if b == nil || b == http.NoBody {
232 // No copying needed. Preserve the magic sentinel meaning of NoBody.
233 return http.NoBody, http.NoBody, nil
234 }
235 var buf bytes.Buffer
236 if _, err = buf.ReadFrom(b); err != nil {
237 return nil, b, err
238 }
239 if err := b.Close(); err != nil {
240 return nil, b, err
241 }
242 return io.NopCloser(&buf), io.NopCloser(bytes.NewReader(buf.Bytes())), nil
243}
Diagnostics6 git calls · 229 ms
CallStatusSizeTime0229 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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 KiB10 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=UPDATING git.response.duration_ms=10 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
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 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=4273 git.response.cache=HIT git.response.duration_ms=2 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:0 · deepen 1020032 KiB · hit5 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=32926 git.response.cache=HIT git.response.duration_ms=5 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
fetchtree:02001.1 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=1165 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=71c8407e0ff0e0af10f6a041ae330f754b68e7eb 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=1165 git.pkt.payload.max=1138 git.sideband.band1.bytes=1138 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1138 git.pack.trailer=61886367c69619d8db2b2cf2d7d24dca2016141cOpen pack map
fetch2003.0 KiB212 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=212 git.response.bytes=3036 git.response.cache=MISS git.response.duration_ms=212 git.fetch.wants=15ef8297ef3988ad6a4dbac92fcd90ae61352fc4 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=3036 git.pkt.payload.max=3009 git.sideband.band1.bytes=3009 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
- a5e187469e4f5e37
- Request
- d33a5fa7…
- 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