GitLabgitlab-org
cli
main
1package pm
3import (
4 "bytes"
5 "crypto/x509"
6 "errors"
7 "fmt"
8 "io/fs"
9 "maps"
10 "os"
11 "path/filepath"
12 "strings"
13 "unicode"
15 "software.sslmate.com/src/go-pkcs12"
17 "gitlab.com/gitlab-org/cli/internal/dbg"
18)
20const (
21 trustStoreProperty = "javax.net.ssl.trustStore"
22 trustStorePasswordProperty = "javax.net.ssl.trustStorePassword"
23 trustStoreTypeProperty = "javax.net.ssl.trustStoreType"
24 jdkCACertsPassword = "changeit"
25)
27// jksMagic and jceksMagic open the proprietary keystore formats go-pkcs12
28// cannot read. The JVM sniffs the format rather than trusting
29// trustStoreType, so the store is checked the same way.
30var (
31 jksMagic = []byte{0xfe, 0xed, 0xfe, 0xed}
32 jceksMagic = []byte{0xce, 0xce, 0xce, 0xce}
33)
35// jvmTrustAnchors returns the certificates in the truststore a JVM would
36// trust when the -D options in sources set its truststore properties over
37// defaults. Later sources override earlier ones, as later options do on the
38// java command line. Relative paths resolve against dir, the JVM's working
39// directory. Like JSSE, it reads the JDK default under JAVA_HOME when no
40// trustStore is set, and cacerts when the named store is not a readable
41// regular file. It returns an error for a named store it reads but cannot
42// decode. A JDK default it cannot decode is logged only under GLAB_DEBUG,
43// because JDK 17 and earlier ship it as JKS.
44func jvmTrustAnchors(env []string, dir string, defaults map[string]string, sources ...string) ([]*x509.Certificate, error) {
45 props := make(map[string]string, len(defaults)+3)
46 maps.Copy(props, defaults)
47 for _, src := range sources {
48 for _, arg := range splitJVMArgs(src) {
49 rest, ok := strings.CutPrefix(arg, "-D")
50 if !ok {
51 continue
52 }
53 name, value, _ := strings.Cut(rest, "=")
54 switch name {
55 case trustStoreProperty, trustStorePasswordProperty, trustStoreTypeProperty:
56 props[name] = value
57 }
58 }
59 }
61 path, password, storeType := props[trustStoreProperty], props[trustStorePasswordProperty], props[trustStoreTypeProperty]
62 if t := strings.ToLower(storeType); strings.HasPrefix(t, "windows-") || strings.HasPrefix(t, "keychainstore") {
63 // The Windows and macOS stores back the system roots that registry
64 // connections already start from.
65 dbg.Debugf("dependency firewall: JVM truststore type %s is the system store, which registry connections already trust", storeType)
66 return nil, nil
67 }
69 javaHome := lookupEnv(env, "JAVA_HOME")
70 _, named := props[trustStoreProperty]
71 switch {
72 case path == "NONE":
73 return nil, nil
74 case !named:
75 path = jdkDefaultTrustStore(javaHome)
76 default:
77 if !filepath.IsAbs(path) {
78 path = filepath.Join(dir, path)
79 }
80 raw, err := readRegularFile(path)
81 if err != nil {
82 // The path stays out of this line: a misparsed option can carry
83 // the next one, such as the password.
84 dbg.Debugf("dependency firewall: the JVM trustStore option names no readable regular file (%v), so reading cacerts as the JVM does", err)
85 path = jdkCACertsPath(javaHome)
86 break
87 }
88 certs, err := decodeJVMTrustStore(raw, storeType, password)
89 if err != nil {
90 return nil, fmt.Errorf("registry connections do not trust JVM truststore %q: %w", path, err)
91 }
92 return certs, nil
93 }
94 if path == "" {
95 return nil, nil
96 }
98 if _, set := props[trustStorePasswordProperty]; !set {
99 password = jdkCACertsPassword
100 }
101 raw, err := os.ReadFile(path)
102 var certs []*x509.Certificate
103 if err == nil {
104 certs, err = decodeJVMTrustStore(raw, storeType, password)
105 }
106 if err != nil {
107 dbg.Debugf("dependency firewall: skipping JVM truststore %q: %v", path, err)
108 return nil, nil
109 }
110 return certs, nil
111}
113// readRegularFile reads path if it is a readable regular file, which JSSE
114// requires of a truststore; reading a FIFO would block. Its errors leave the
115// path out, so they are safe to log for a misparsed option.
116func readRegularFile(path string) ([]byte, error) {
117 info, err := os.Stat(path)
118 if err == nil && !info.Mode().IsRegular() {
119 return nil, errors.New("not a regular file")
120 }
121 var raw []byte
122 if err == nil {
123 raw, err = os.ReadFile(path)
124 }
125 var pathErr *fs.PathError
126 if errors.As(err, &pathErr) {
127 return nil, pathErr.Err
128 }
129 return raw, err
130}
132// jvmTrustStoreOverride reports a truststore option in one of laterVars, which
133// reach the JVM after optsVar and so replace the truststore glab gives the
134// child there; tool then distrusts the proxy.
135func jvmTrustStoreOverride(env []string, tool, optsVar string, laterVars ...string) error {
136 for _, v := range laterVars {
137 for _, arg := range splitJVMArgs(lookupEnv(env, v)) {
138 rest, ok := strings.CutPrefix(arg, "-D")
139 if !ok {
140 continue
141 }
142 if name, value, _ := strings.Cut(rest, "="); replacesTrustStore(name, value) {
143 return fmt.Errorf("-D%s in %s replaces glab's truststore, so %s cannot trust the dependency firewall proxy; set it in %s instead", name, v, tool, optsVar)
144 }
145 }
146 }
147 return nil
148}
150// replacesTrustStore reports whether -Dname=value replaces the truststore glab
151// gives the child when it takes effect after glab's options. A PKCS12 or JKS
152// trustStoreType is harmless: under the default keystore.type.compat both
153// load glab's PKCS#12.
154func replacesTrustStore(name, value string) bool {
155 switch name {
156 case trustStoreProperty, trustStorePasswordProperty:
157 return true
158 case trustStoreTypeProperty:
159 return !strings.EqualFold(value, "pkcs12") && !strings.EqualFold(value, "jks")
160 }
161 return false
162}
164// jdkDefaultTrustStore returns the store JSSE loads when no trustStore is
165// set: jssecacerts when present, otherwise cacerts.
166func jdkDefaultTrustStore(javaHome string) string {
167 if javaHome == "" {
168 return ""
169 }
170 if jsse := filepath.Join(javaHome, "lib", "security", "jssecacerts"); isFile(jsse) {
171 return jsse
172 }
173 return jdkCACertsPath(javaHome)
174}
176func jdkCACertsPath(javaHome string) string {
177 if javaHome == "" {
178 return ""
179 }
180 return filepath.Join(javaHome, "lib", "security", "cacerts")
181}
183func decodeJVMTrustStore(raw []byte, storeType, password string) ([]*x509.Certificate, error) {
184 switch strings.ToLower(storeType) {
185 case "", "pkcs12", "jks", "jceks":
186 default:
187 return nil, fmt.Errorf("type %s is not supported", storeType)
188 }
189 if bytes.HasPrefix(raw, jksMagic) || bytes.HasPrefix(raw, jceksMagic) {
190 return nil, errors.New("JKS and JCEKS keystores are not supported, convert it to PKCS#12 with keytool -importkeystore")
191 }
192 certs, err := pkcs12.DecodeTrustStore(raw, password)
193 if err == nil {
194 return certs, nil
195 }
196 if password != "" {
197 // The JDK's own cacerts (JDK 18 and later) has no MAC, which
198 // go-pkcs12 accepts only with an empty password; the JVM ignores the
199 // password for such a store.
200 if certs, retryErr := pkcs12.DecodeTrustStore(raw, ""); retryErr == nil {
201 return certs, nil
202 }
203 }
204 switch {
205 case !errors.Is(err, pkcs12.ErrIncorrectPassword):
206 return nil, fmt.Errorf("%w; glab reads PKCS#12 truststores that hold only trusted certificates", err)
207 case password == "":
208 // Without a password the JVM skips the MAC check; go-pkcs12 cannot.
209 return nil, fmt.Errorf("%w; set -D%s so glab can check the store", err, trustStorePasswordProperty)
210 }
211 return nil, err
212}
214// splitJVMArgs splits s into arguments one line at a time, on whitespace
215// outside single or double quotes, and drops the quotes. Backslashes stay
216// literal so Windows paths survive. A line with an unbalanced quote is split
217// on whitespace with its quotes kept, so that quote cannot run on into the
218// next option or line. Quotes follow the JVM, Gradle and Maven 4; Maven 3's
219// Unix launcher passes them to java literally.
220func splitJVMArgs(s string) []string {
221 var args []string
222 for line := range strings.Lines(s) {
223 lineArgs, balanced := splitQuotedArgs(line)
224 if !balanced {
225 lineArgs = strings.Fields(line)
226 }
227 args = append(args, lineArgs...)
228 }
229 return args
230}
232func splitQuotedArgs(line string) ([]string, bool) {
233 var (
234 args []string
235 cur strings.Builder
236 inArg bool
237 quote rune
238 )
239 for _, r := range line {
240 switch {
241 case quote != 0:
242 if r == quote {
243 quote = 0
244 } else {
245 cur.WriteRune(r)
246 }
247 case r == '"' || r == '\'':
248 quote, inArg = r, true
249 case unicode.IsSpace(r):
250 if inArg {
251 args = append(args, cur.String())
252 cur.Reset()
253 inArg = false
254 }
255 default:
256 cur.WriteRune(r)
257 inArg = true
258 }
259 }
260 if inArg {
261 args = append(args, cur.String())
262 }
263 return args, quote == 0
264}
266// mavenBaseDir returns the directory mvn reads .mvn/jvm.config and
267// .mvn/maven.config from. Like Maven 3's launcher (Maven 4 ignores it), it
268// uses MAVEN_BASEDIR when set, else the nearest .mvn directory from the
269// -f/--file argument's directory or from dir.
270func mavenBaseDir(env []string, dir string, args []string) string {
271 if base := lookupEnv(env, "MAVEN_BASEDIR"); base != "" {
272 return base
273 }
274 start := dir
275 for i, arg := range args {
276 if (arg != "-f" && arg != "--file") || i+1 == len(args) {
277 continue
278 }
279 start = args[i+1]
280 if !filepath.IsAbs(start) {
281 start = filepath.Join(dir, start)
282 }
283 if info, err := os.Stat(start); err == nil && !info.IsDir() {
284 start = filepath.Dir(start)
285 }
286 break
287 }
288 return mavenProjectBaseDir(start)
289}
291// mavenJVMConfig returns the contents of the .mvn/jvm.config that the mvn
292// launcher adds to MAVEN_OPTS, or "" when there is none. Like Maven 4's
293// launcher (Maven 3 substitutes nothing), it strips comments before it
294// substitutes MAVEN_PROJECTBASEDIR, so a '#' in the project path is not read
295// as a comment.
296func mavenJVMConfig(env []string, dir string, args []string) string {
297 base := mavenBaseDir(env, dir, args)
299 raw, ok, err := readConfigFile(filepath.Join(base, ".mvn", "jvm.config"))
300 if err != nil {
301 dbg.Debugf("dependency firewall: %v; not reading .mvn/jvm.config for a JVM truststore", err)
302 }
303 if !ok {
304 return ""
305 }
306 return strings.NewReplacer("${MAVEN_PROJECTBASEDIR}", base, "$MAVEN_PROJECTBASEDIR", base).Replace(stripJVMConfigComments(string(raw)))
307}
309// mavenArgProperties returns the -D properties mvn's own arguments set:
310// .mvn/maven.config, then MAVEN_ARGS, then the command line, the later
311// winning as it does for mvn. MAVEN_OPTS is left out, for the caller to read
312// among the JVM options.
313func mavenArgProperties(env []string, dir string, args []string) map[string]string {
314 cli := mavenConfigArgs(mavenBaseDir(env, dir, args))
315 cli = append(cli, strings.Fields(lookupEnv(env, "MAVEN_ARGS"))...)
316 return mavenProperties(nil, append(cli, args...))
317}
319// argTrustStoreOverride reports, with format and the option's name, a
320// truststore option in props, the system properties a tool's own arguments
321// set. Maven 3 and gradle apply those over the JVM options, and so over the
322// truststore glab gives them there.
323func argTrustStoreOverride(props map[string]string, format string) error {
324 for _, name := range []string{trustStoreProperty, trustStorePasswordProperty, trustStoreTypeProperty} {
325 if value, ok := props[name]; ok && replacesTrustStore(name, value) {
326 return fmt.Errorf(format, name)
327 }
328 }
329 return nil
330}
332// stripJVMConfigComments cuts each jvm.config line at a '#' that starts a
333// word. Maven 4 cuts at any '#'; Maven 3 strips nothing, and a word starting
334// with '#' makes it fail (java takes the word as its main class), so this is
335// what both agree on. A '#' inside a word is kept, as Maven 3 keeps it.
336func stripJVMConfigComments(s string) string {
337 var b strings.Builder
338 for line := range strings.Lines(strings.NewReplacer("\r\n", "\n", "\r", "\n").Replace(s)) {
339 prev := ' '
340 for i, r := range line {
341 if r == '#' && unicode.IsSpace(prev) {
342 line = line[:i] + "\n"
343 break
344 }
345 prev = r
346 }
347 b.WriteString(line)
348 }
349 return b.String()
350}
Diagnostics7 git calls · 225 ms
CallStatusSizeTime0225 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch 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 · hit2 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=32926 git.response.cache=HIT git.response.duration_ms=2 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=2 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=1 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 · hit1 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=216 git.response.bytes=1327 git.response.cache=HIT git.response.duration_ms=1 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.1 KiB216 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=4165 git.response.cache=MISS git.response.duration_ms=216 git.fetch.wants=864e28327d1b3a7587fa1bbcb02c684aff6ff933 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=4165 git.pkt.payload.max=4138 git.sideband.band1.bytes=4138 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
- a5e1bb9da8146c75
- Request
- ff1bff29…
- 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