GitLabgitlab-org
cli
main
1package policy
3import (
4 "context"
5 "slices"
6 "strings"
8 "gitlab.com/gitlab-org/cli/internal/dbg"
9 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
10)
12// fakeChecker is a placeholder policy source driven by GLAB_DF_FAKE_*
13// environment variables. It is selected when any such variable is set and
14// is used for testing. It never returns an error.
15type fakeChecker struct {
16 block []matcher
17 warn []matcher
18 defaultVerdict verdict.Verdict // verdict.Allowed, verdict.Blocked, or verdict.Warning
19}
21// matcher is one parsed GLAB_DF_FAKE_* list entry: ecosystem + name, with
22// an optional exact version. A zero version matches any version.
23type matcher struct {
24 ecosystem string
25 name string
26 version string // "" = any version
27}
29func (m matcher) matches(c Coordinate) bool {
30 if m.ecosystem != c.Ecosystem || m.name != c.Name {
31 return false
32 }
33 return m.version == "" || m.version == c.Version
34}
36func newFakeChecker(environ []string) Checker {
37 return fakeChecker{
38 block: parseList(lookupEnv(environ, "GLAB_DF_FAKE_BLOCK")),
39 warn: parseList(lookupEnv(environ, "GLAB_DF_FAKE_WARN")),
40 defaultVerdict: parseDefault(lookupEnv(environ, "GLAB_DF_FAKE_DEFAULT")),
41 }
42}
44// lookupEnv returns the value of key in environ ("KEY=value" entries), or ""
45// if absent. It reads only the keys the fake checker needs rather than
46// materializing the whole environment into a map. It scans from the end so a
47// duplicate key resolves to its last occurrence, matching os.Environ and the
48// map-based lookup this replaced (last occurrence wins).
49func lookupEnv(environ []string, key string) string {
50 prefix := key + "="
51 for _, e := range slices.Backward(environ) {
52 if v, ok := strings.CutPrefix(e, prefix); ok {
53 return v
54 }
55 }
56 return ""
57}
59// parseList turns "eco:name@version,eco:name" into matchers. Malformed
60// entries (missing ecosystem or name) are skipped.
61func parseList(raw string) []matcher {
62 var out []matcher
63 for item := range strings.SplitSeq(raw, ",") {
64 item = strings.TrimSpace(item)
65 if item == "" {
66 continue
67 }
68 eco, rest, ok := strings.Cut(item, ":")
69 if !ok || eco == "" {
70 continue
71 }
72 name := rest
73 version := ""
74 // Split on the last "@" so scoped npm names (for example
75 // "@babel/core@7.24.0") keep their leading "@" in the name.
76 if at := strings.LastIndex(rest, "@"); at > 0 {
77 name, version = rest[:at], rest[at+1:]
78 }
79 if name == "" {
80 continue
81 }
82 out = append(out, matcher{ecosystem: eco, name: name, version: version})
83 }
84 return out
85}
87// parseDefault returns the default verdict for GLAB_DF_FAKE_DEFAULT. An
88// empty (unset) value means allow; any other unrecognized value is treated
89// as allow but logged, so a typo (e.g. "blck") is surfaced rather than
90// silently masking a misconfiguration.
91func parseDefault(raw string) verdict.Verdict {
92 switch strings.TrimSpace(strings.ToLower(raw)) {
93 case "", "allow":
94 return verdict.Allowed
95 case "block":
96 return verdict.Blocked
97 case "warn":
98 return verdict.Warning
99 default:
100 dbg.Debugf("policy: unrecognized GLAB_DF_FAKE_DEFAULT value %q, treating as allow", raw)
101 return verdict.Allowed
102 }
103}
105// bestMatch returns the strongest matching entry's verdict. Exact-version
106// matches take precedence over any-version matches; within the same
107// specificity, block is consulted before warn. Returns ("", false) when
108// nothing matches.
109func (f fakeChecker) bestMatch(c Coordinate) (verdict.Verdict, bool) {
110 // Exact version first.
111 if hasExact(f.block, c) {
112 return verdict.Blocked, true
113 }
114 if hasExact(f.warn, c) {
115 return verdict.Warning, true
116 }
117 // Then any-version.
118 if hasAny(f.block, c) {
119 return verdict.Blocked, true
120 }
121 if hasAny(f.warn, c) {
122 return verdict.Warning, true
123 }
124 return verdict.Allowed, false
125}
127func hasExact(ms []matcher, c Coordinate) bool {
128 for _, m := range ms {
129 if m.version != "" && m.matches(c) {
130 return true
131 }
132 }
133 return false
134}
136func hasAny(ms []matcher, c Coordinate) bool {
137 for _, m := range ms {
138 if m.version == "" && m.matches(c) {
139 return true
140 }
141 }
142 return false
143}
145func (f fakeChecker) Check(_ context.Context, r Request) (Result, error) {
146 v, matched := f.bestMatch(r.Coordinate)
147 source := "GLAB_DF_FAKE_BLOCK/WARN"
148 if !matched {
149 v = f.defaultVerdict
150 source = "GLAB_DF_FAKE_DEFAULT"
151 }
152 switch v {
153 case verdict.Blocked:
154 return Result{Verdict: verdict.Blocked, Reason: "blocked by " + source}, nil
155 case verdict.Warning:
156 return Result{Verdict: verdict.Warning, Reason: "warned by " + source}, nil
157 default:
158 return Result{}, nil
159 }
160}
Diagnostics7 git calls · 26 ms
CallStatusSizeTime026 ms
discover200185 B3 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 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
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=2 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 · 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=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
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:0200332 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=17 git.response.bytes=332 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=cb813f0d675e9dca14f167e8c7788d61eb19627f 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=332 git.pkt.payload.max=305 git.sideband.band1.bytes=305 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=305 git.pack.trailer=5e210c1831ecf78390a6fe6e1784efd898336da0Open pack map
fetch2001.8 KiB · hit17 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1869 git.response.cache=HIT git.response.duration_ms=17 git.fetch.wants=307d1a1e06d43d01a91ca097d7be1ae948baabec 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=1869 git.pkt.payload.max=1842 git.sideband.band1.bytes=1842 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
- a5e2bb65ba17cfc3
- Request
- 48975293…
- 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