GitLabgitlab-org

cli

main
ply7 git calls49 ms97 KiB5 cachedprotocol v2
133 lines4.5 KiBRaw
1//go:build !integration
3package policy
5import (
6 "testing"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
11 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/verdict"
14func fakeFrom(env map[string]string) Checker {
15 var environ []string
16 for k, v := range env {
17 environ = append(environ, k+"="+v)
18 }
19 return newFakeChecker(environ)
22func req(eco, name, version string) Request {
23 return Request{Coordinate: Coordinate{Ecosystem: eco, Name: name, Version: version}}
26func TestFakeDefaultAllow(t *testing.T) {
27 c := fakeFrom(map[string]string{"GLAB_DF_FAKE_DEFAULT": "allow"})
28 r, err := c.Check(t.Context(), req("npm", "left-pad", "1.3.0"))
29 require.NoError(t, err)
30 assert.Equal(t, verdict.Verdict(""), r.Verdict)
33func TestFakeDefaultEmptyIsAllow(t *testing.T) {
34 c := fakeFrom(map[string]string{"GLAB_DF_FAKE_WARN": ""})
35 r, err := c.Check(t.Context(), req("npm", "x", "1.0.0"))
36 require.NoError(t, err)
37 assert.Equal(t, verdict.Verdict(""), r.Verdict)
40func TestFakeDefaultUnrecognizedIsAllow(t *testing.T) {
41 c := fakeFrom(map[string]string{"GLAB_DF_FAKE_DEFAULT": "blck"})
42 r, err := c.Check(t.Context(), req("npm", "x", "1.0.0"))
43 require.NoError(t, err)
44 assert.Equal(t, verdict.Verdict(""), r.Verdict)
47func TestFakeBlockExactVersion(t *testing.T) {
48 c := fakeFrom(map[string]string{"GLAB_DF_FAKE_BLOCK": "npm:left-pad@1.3.0"})
49 r, _ := c.Check(t.Context(), req("npm", "left-pad", "1.3.0"))
50 assert.Equal(t, verdict.Blocked, r.Verdict)
52 r2, _ := c.Check(t.Context(), req("npm", "left-pad", "2.0.0"))
53 assert.Equal(t, verdict.Verdict(""), r2.Verdict)
56func TestFakeBlockScopedNPMExactVersion(t *testing.T) {
57 c := fakeFrom(map[string]string{"GLAB_DF_FAKE_BLOCK": "npm:@babel/core@7.24.0"})
58 r, _ := c.Check(t.Context(), req("npm", "@babel/core", "7.24.0"))
59 assert.Equal(t, verdict.Blocked, r.Verdict)
61 r2, _ := c.Check(t.Context(), req("npm", "@babel/core", "8.0.0"))
62 assert.Equal(t, verdict.Verdict(""), r2.Verdict)
65func TestFakeBlockScopedNPMAnyVersion(t *testing.T) {
66 c := fakeFrom(map[string]string{"GLAB_DF_FAKE_BLOCK": "npm:@babel/core"})
67 r, _ := c.Check(t.Context(), req("npm", "@babel/core", "7.24.0"))
68 assert.Equal(t, verdict.Blocked, r.Verdict)
71func TestFakeBlockAnyVersion(t *testing.T) {
72 c := fakeFrom(map[string]string{"GLAB_DF_FAKE_BLOCK": "pypi:requests"})
73 r, _ := c.Check(t.Context(), req("pypi", "requests", "2.31.0"))
74 assert.Equal(t, verdict.Blocked, r.Verdict)
77func TestFakeWarn(t *testing.T) {
78 c := fakeFrom(map[string]string{"GLAB_DF_FAKE_WARN": "gem:rails@7.1.3"})
79 r, _ := c.Check(t.Context(), req("gem", "rails", "7.1.3"))
80 assert.Equal(t, verdict.Warning, r.Verdict)
83func TestFakeExactBeatsWildcard(t *testing.T) {
84 c := fakeFrom(map[string]string{
85 "GLAB_DF_FAKE_WARN": "npm:left-pad",
86 "GLAB_DF_FAKE_BLOCK": "npm:left-pad@1.3.0",
87 })
88 r, _ := c.Check(t.Context(), req("npm", "left-pad", "1.3.0"))
89 assert.Equal(t, verdict.Blocked, r.Verdict)
92func TestFakeBlockBeforeWarnSameSpecificity(t *testing.T) {
93 c := fakeFrom(map[string]string{
94 "GLAB_DF_FAKE_BLOCK": "npm:pkg",
95 "GLAB_DF_FAKE_WARN": "npm:pkg",
96 })
97 r, _ := c.Check(t.Context(), req("npm", "pkg", "1.0.0"))
98 assert.Equal(t, verdict.Blocked, r.Verdict)
101func TestFakeDefaultBlock(t *testing.T) {
102 c := fakeFrom(map[string]string{"GLAB_DF_FAKE_DEFAULT": "block"})
103 r, _ := c.Check(t.Context(), req("npm", "anything", "1.0.0"))
104 assert.Equal(t, verdict.Blocked, r.Verdict)
107func TestFakeDefaultWarn(t *testing.T) {
108 c := fakeFrom(map[string]string{"GLAB_DF_FAKE_DEFAULT": "warn"})
109 r, _ := c.Check(t.Context(), req("npm", "anything", "1.0.0"))
110 assert.Equal(t, verdict.Warning, r.Verdict)
113func TestFakeParseListSkipsMalformedEntries(t *testing.T) {
114 c := fakeFrom(map[string]string{"GLAB_DF_FAKE_BLOCK": "npm:, ,:x,pypi:requests"})
115 blocked, _ := c.Check(t.Context(), req("pypi", "requests", "2.31.0"))
116 assert.Equal(t, verdict.Blocked, blocked.Verdict)
118 allowed, _ := c.Check(t.Context(), req("npm", "x", "1.0.0"))
119 assert.Equal(t, verdict.Verdict(""), allowed.Verdict)
122// TestFakeDuplicateEnvKeyUsesLastOccurrence pins the POSIX last-occurrence-wins
123// semantics for duplicate keys, matching os.Environ and the map-based lookup
124// that lookupEnv replaced. A raw environ slice is passed directly since a map
125// cannot represent duplicate keys.
126func TestFakeDuplicateEnvKeyUsesLastOccurrence(t *testing.T) {
127 c := newFakeChecker([]string{
128 "GLAB_DF_FAKE_DEFAULT=allow",
129 "GLAB_DF_FAKE_DEFAULT=block",
130 })
131 r, _ := c.Check(t.Context(), req("npm", "anything", "1.0.0"))
132 assert.Equal(t, verdict.Blocked, r.Verdict, "the last duplicate GLAB_DF_FAKE_DEFAULT must win")
Diagnostics7 git calls · 49 ms
CallStatusSizeTime049 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=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 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 · hit3 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=3
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=26d32585a3a474717e00a318584ccd16918c9d32
Open 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=40
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=5e210c1831ecf78390a6fe6e1784efd898336da0
Open pack map
fetch2001.1 KiB · hit40 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1128
git.response.cache=HIT
git.response.duration_ms=40
git.fetch.wants=2ef411d37a364b791772fd234051572fd7209f56
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=1128
git.pkt.payload.max=1101
git.sideband.band1.bytes=1101
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
a5e2bfc9384789da
Request
c848ba2f…
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