GitLabgitlab-org
cli
main
1//go:build !integration
3package proxy
5import (
6 "bufio"
7 "io"
8 "net/http"
9 "strings"
10 "testing"
11 "unicode/utf8"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
16 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/policy"
17)
19func TestBlockResponseStatusLineCarriesReason(t *testing.T) {
20 t.Parallel()
21 resp := parseBlock(t, blockResponse("maven", "Package 'g/a' violates 'Deny log4j' policy", false))
22 defer resp.Body.Close()
23 assert.Equal(t, http.StatusForbidden, resp.StatusCode)
24 assert.Equal(t, "403 Blocked by GitLab Dependency Firewall: Package 'g/a' violates 'Deny log4j' policy", resp.Status)
25}
27func TestBlockResponseStatusLineWithoutReason(t *testing.T) {
28 t.Parallel()
29 resp := parseBlock(t, blockResponse("maven", "", false))
30 defer resp.Body.Close()
31 assert.Equal(t, "403 Blocked by GitLab Dependency Firewall", resp.Status)
32}
34func TestBlockResponseStatusLineCannotInjectHeaders(t *testing.T) {
35 t.Parallel()
36 resp := parseBlock(t, blockResponse("maven", "nope\r\nX-Injected: yes\r\n\r\nbody", false))
37 defer resp.Body.Close()
38 assert.Equal(t, http.StatusForbidden, resp.StatusCode)
39 assert.Empty(t, resp.Header.Get("X-Injected"))
40 assert.Equal(t, "403 Blocked by GitLab Dependency Firewall: nope X-Injected: yes body", resp.Status)
41}
43func TestBlockResponseStatusLineHidesFailedCheckError(t *testing.T) {
44 t.Parallel()
45 resp := parseBlock(t, blockResponse("maven", "policy check failed: GET https://gitlab.example/api/v4/projects/9/x: 500 {host=10.0.0.5}", true))
46 defer resp.Body.Close()
47 assert.Equal(t, http.StatusForbidden, resp.StatusCode)
48 assert.Equal(t, "403 Dependency Firewall could not evaluate this package", resp.Status)
49}
51func TestBlockResponseStatusLineIsCapped(t *testing.T) {
52 t.Parallel()
53 long := strings.Repeat("é", 500)
54 resp := parseBlock(t, blockResponse("gem", long, false))
55 defer resp.Body.Close()
56 assert.LessOrEqual(t, len(resp.Status), len("403 ")+512)
57 assert.True(t, utf8.ValidString(resp.Status), "the cap must not split a rune")
58 body, err := io.ReadAll(resp.Body)
59 require.NoError(t, err)
60 assert.Equal(t, long, string(body), "the body keeps the full reason")
61}
63func parseBlock(t *testing.T, raw string) *http.Response {
64 t.Helper()
65 resp, err := http.ReadResponse(bufio.NewReader(strings.NewReader(raw)), nil)
66 require.NoError(t, err)
67 return resp
68}
70func TestBlockResponseNPM(t *testing.T) {
71 t.Parallel()
72 raw := blockResponse("npm", "blocked by policy", false)
73 resp := parseBlock(t, raw)
74 defer resp.Body.Close()
75 assert.Equal(t, http.StatusForbidden, resp.StatusCode)
76 assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
77 assert.Equal(t, "blocked", resp.Header.Get("X-Gitlab-Dependency-Firewall"))
78 body, err := io.ReadAll(resp.Body)
79 require.NoError(t, err)
80 assert.Contains(t, string(body), "blocked by policy")
81}
83func TestBlockResponsePyPIIsHTML(t *testing.T) {
84 t.Parallel()
85 raw := blockResponse("pypi", "nope", false)
86 resp := parseBlock(t, raw)
87 defer resp.Body.Close()
88 assert.Equal(t, "text/html", resp.Header.Get("Content-Type"))
89 assert.Equal(t, http.StatusForbidden, resp.StatusCode)
90}
92func TestBlockResponseGemIsPlainText(t *testing.T) {
93 t.Parallel()
94 raw := blockResponse("gem", "nope", false)
95 resp := parseBlock(t, raw)
96 defer resp.Body.Close()
97 assert.Equal(t, "text/plain", resp.Header.Get("Content-Type"))
98}
100func TestBlockResponseMavenIsHTML(t *testing.T) {
101 t.Parallel()
102 raw := blockResponse("maven", "nope", false)
103 resp := parseBlock(t, raw)
104 defer resp.Body.Close()
105 assert.Equal(t, "text/html", resp.Header.Get("Content-Type"))
106}
108func TestBlockResponseNuGetIsJSON(t *testing.T) {
109 t.Parallel()
110 raw := blockResponse("nuget", "blocked by policy", false)
111 resp := parseBlock(t, raw)
112 defer resp.Body.Close()
113 assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
114 body, err := io.ReadAll(resp.Body)
115 require.NoError(t, err)
116 assert.Contains(t, string(body), "blocked by policy")
117}
119func TestInternalErrorStatusLineCannotInjectHeaders(t *testing.T) {
120 t.Parallel()
121 phrase := policy.InternalErrorPhrase(policy.Coordinate{Name: "org.evil\r\nX-Injected: yes\r\n\r\nbody:a", Version: "1"})
122 resp := parseBlock(t, blockResponse("maven", phrase, true))
123 defer resp.Body.Close()
124 assert.Equal(t, http.StatusForbidden, resp.StatusCode)
125 assert.Empty(t, resp.Header.Get("X-Injected"))
127 long := parseBlock(t, blockResponse("maven", policy.InternalErrorPhrase(policy.Coordinate{Name: strings.Repeat("a", 2000), Version: "1"}), true))
128 defer long.Body.Close()
129 assert.LessOrEqual(t, len(long.Status), len("403 ")+maxBlockReasonPhrase)
130}
Diagnostics7 git calls · 219 ms
CallStatusSizeTime0219 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 KiB · hit3 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=HIT git.response.duration_ms=3 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 · 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=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: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
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.1 KiB208 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=208 git.response.bytes=1165 git.response.cache=MISS git.response.duration_ms=208 git.fetch.wants=df2a2f097ee87845d5336a46ec105a32c7333217 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
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
- a5e233d38d5ba594
- Request
- a222fd58…
- 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