GitLabgitlab-org
cli
main
1package proxy
3import (
4 "bufio"
5 "bytes"
6 "encoding/json"
7 "errors"
8 "io"
9 "maps"
10 "slices"
11)
13// packument is the part of an npm publish body the matcher checks.
14type packument struct {
15 name string
16 versions []string
17 attachments bool
18}
20var errMalformedPackument = errors.New("malformed npm packument")
22// maxPackumentKey bounds a top-level key; npm's are short field names.
23const maxPackumentKey = 4 << 10
25// scanPackument reads the top-level keys of a publish body from r without
26// holding the body in memory, so a tarball larger than the inspection window
27// does not hide the coordinate. Only name and versions are decoded, each
28// capped at maxValue bytes; every other value, including the base64 tarball
29// in _attachments, is skipped byte by byte. A repeated key replaces the
30// earlier one, as it does for the registry's JSON parser.
31func scanPackument(r io.Reader, maxValue int) (packument, error) {
32 br := bufio.NewReader(r)
33 var p packument
34 if c, err := nextNonSpace(br); err != nil || c != '{' {
35 return p, errMalformedPackument
36 }
37 c, err := nextNonSpace(br)
38 if err != nil {
39 return p, err
40 }
41 if c == '}' {
42 return p, nil
43 }
44 for {
45 if c != '"' {
46 return p, errMalformedPackument
47 }
48 var raw bytes.Buffer
49 raw.WriteByte('"')
50 if err := readStringRest(br, &raw, maxPackumentKey); err != nil {
51 return p, err
52 }
53 var key string
54 if err := json.Unmarshal(raw.Bytes(), &key); err != nil {
55 return p, err
56 }
57 if c, err := nextNonSpace(br); err != nil || c != ':' {
58 return p, errMalformedPackument
59 }
60 if err := p.readValue(br, key, maxValue); err != nil {
61 return p, err
62 }
63 c, err = nextNonSpace(br)
64 if err != nil {
65 return p, err
66 }
67 switch c {
68 case '}':
69 return p, nil
70 case ',':
71 if c, err = nextNonSpace(br); err != nil {
72 return p, err
73 }
74 default:
75 return p, errMalformedPackument
76 }
77 }
78}
80func (p *packument) readValue(br *bufio.Reader, key string, maxValue int) error {
81 switch key {
82 case "name":
83 var raw bytes.Buffer
84 if _, err := walkValue(br, &raw, maxValue); err != nil {
85 return err
86 }
87 return json.Unmarshal(raw.Bytes(), &p.name)
88 case "versions":
89 var raw bytes.Buffer
90 if _, err := walkValue(br, &raw, maxValue); err != nil {
91 return err
92 }
93 var versions map[string]json.RawMessage
94 if err := json.Unmarshal(raw.Bytes(), &versions); err != nil {
95 return err
96 }
97 p.versions = slices.Sorted(maps.Keys(versions))
98 return nil
99 case "_attachments":
100 first, err := walkValue(br, nil, 0)
101 p.attachments = first == '{'
102 return err
103 default:
104 _, err := walkValue(br, nil, 0)
105 return err
106 }
107}
109// walkValue consumes one JSON value from br and returns its first byte. When
110// out is non-nil the value's bytes are copied into it, failing once they
111// exceed limit. It tracks only strings and nesting, which is enough to find
112// where a value ends; captured values are validated by json.Unmarshal.
113func walkValue(br *bufio.Reader, out *bytes.Buffer, limit int) (byte, error) {
114 first, err := nextNonSpace(br)
115 if err != nil {
116 return 0, err
117 }
118 if err := capture(out, limit, first); err != nil {
119 return first, err
120 }
121 switch first {
122 case '"':
123 return first, readStringRest(br, out, limit)
124 case '{', '[':
125 for depth := 1; depth > 0; {
126 c, err := br.ReadByte()
127 if err != nil {
128 return first, err
129 }
130 if err := capture(out, limit, c); err != nil {
131 return first, err
132 }
133 switch c {
134 case '"':
135 if err := readStringRest(br, out, limit); err != nil {
136 return first, err
137 }
138 case '{', '[':
139 depth++
140 case '}', ']':
141 depth--
142 }
143 }
144 return first, nil
145 default:
146 for {
147 c, err := br.ReadByte()
148 if err != nil {
149 return first, err
150 }
151 if c == ',' || c == '}' || c == ']' || isJSONSpace(c) {
152 return first, br.UnreadByte()
153 }
154 if err := capture(out, limit, c); err != nil {
155 return first, err
156 }
157 }
158 }
159}
161// readStringRest consumes a JSON string up to and including its closing quote;
162// the opening quote has already been read.
163func readStringRest(br *bufio.Reader, out *bytes.Buffer, limit int) error {
164 for escaped := false; ; {
165 c, err := br.ReadByte()
166 if err != nil {
167 return err
168 }
169 if err := capture(out, limit, c); err != nil {
170 return err
171 }
172 switch {
173 case escaped:
174 escaped = false
175 case c == '\\':
176 escaped = true
177 case c == '"':
178 return nil
179 }
180 }
181}
183func capture(out *bytes.Buffer, limit int, c byte) error {
184 if out == nil {
185 return nil
186 }
187 if out.Len() >= limit {
188 return errMalformedPackument
189 }
190 return out.WriteByte(c)
191}
193func nextNonSpace(br *bufio.Reader) (byte, error) {
194 for {
195 c, err := br.ReadByte()
196 if err != nil || !isJSONSpace(c) {
197 return c, err
198 }
199 }
200}
202func isJSONSpace(c byte) bool {
203 return c == ' ' || c == '\t' || c == '\n' || c == '\r'
204}
Diagnostics7 git calls · 48 ms
CallStatusSizeTime048 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=9 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
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.0 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=1051 git.response.cache=HIT git.response.duration_ms=3 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=7503ef36a75d3595b473035c69e639fd0ce41598Open pack map
fetch2001.6 KiB · hit29 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=29 git.response.bytes=1607 git.response.cache=HIT git.response.duration_ms=29 git.fetch.wants=365eced98ecbb8f4ac29938737a463f98f9ff836 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=1607 git.pkt.payload.max=1580 git.sideband.band1.bytes=1580 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
- a5e26fff3ef6d806
- Request
- c52080e2…
- 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