GitLabgitlab-org

cli

main
ply7 git calls30 ms98 KiB6 cachedprotocol v2
173 lines6.7 KiBRaw
1//go:build !integration
3package proxy
5import (
6 "net/http"
7 "net/http/httptest"
8 "testing"
10 "github.com/stretchr/testify/assert"
12 "gitlab.com/gitlab-org/cli/internal/dependencyfirewall/policy"
15// mvn builds a request against Maven Central by default; pass host to
16// simulate a mirror instead.
17func mvn(method, path string, host ...string) *http.Request {
18 h := "https://repo.maven.apache.org"
19 if len(host) > 0 {
20 h = host[0]
21 }
22 return httptest.NewRequest(method, h+path, http.NoBody)
25func TestMavenMatchJarDownload(t *testing.T) {
26 t.Parallel()
27 m := MavenMatcher{}.Match(mvn(http.MethodGet,
28 "/maven2/org/slf4j/slf4j-api/2.0.13/slf4j-api-2.0.13.jar"))
29 assert.True(t, m.Matched)
30 assert.Equal(t, policy.Coordinate{Ecosystem: "maven", Name: "org.slf4j:slf4j-api", Version: "2.0.13"}, m.Coordinate)
31 assert.Equal(t, policy.Download, m.Operation)
34func TestMavenMatchPomDownload(t *testing.T) {
35 t.Parallel()
36 m := MavenMatcher{}.Match(mvn(http.MethodGet,
37 "/maven2/org/slf4j/slf4j-api/2.0.13/slf4j-api-2.0.13.pom"))
38 assert.True(t, m.Matched)
39 assert.Equal(t, "org.slf4j:slf4j-api", m.Coordinate.Name)
42func TestMavenSidecarChecksumIsNoMatch(t *testing.T) {
43 t.Parallel()
44 m := MavenMatcher{}.Match(mvn(http.MethodGet,
45 "/maven2/org/slf4j/slf4j-api/2.0.13/slf4j-api-2.0.13.jar.sha1"))
46 assert.False(t, m.Matched)
49func TestMavenMetadataIsNoMatch(t *testing.T) {
50 t.Parallel()
51 m := MavenMatcher{}.Match(mvn(http.MethodGet,
52 "/maven2/org/slf4j/slf4j-api/maven-metadata.xml"))
53 assert.False(t, m.Matched)
56func TestMavenUploadPut(t *testing.T) {
57 t.Parallel()
58 m := MavenMatcher{}.Match(mvn(http.MethodPut,
59 "/maven2/org/slf4j/slf4j-api/2.0.13/slf4j-api-2.0.13.jar"))
60 assert.True(t, m.Matched)
61 assert.Equal(t, policy.Upload, m.Operation)
62 assert.Equal(t, "org.slf4j:slf4j-api", m.Coordinate.Name)
65func TestMavenSnapshotLiteralDownload(t *testing.T) {
66 t.Parallel()
67 m := MavenMatcher{}.Match(mvn(http.MethodGet,
68 "/maven2/org/slf4j/slf4j-api/1.0-SNAPSHOT/slf4j-api-1.0-SNAPSHOT.jar"))
69 assert.True(t, m.Matched)
70 assert.Equal(t, policy.Coordinate{Ecosystem: "maven", Name: "org.slf4j:slf4j-api", Version: "1.0-SNAPSHOT"}, m.Coordinate)
73func TestMavenSnapshotTimestampedDownloadIsMatched(t *testing.T) {
74 t.Parallel()
75 // A deployed snapshot artifact carries a timestamp instead of the literal
76 // "SNAPSHOT"; it must still be matched so the download is policy-checked
77 // rather than bypassing the firewall.
78 m := MavenMatcher{}.Match(mvn(http.MethodGet,
79 "/maven2/org/slf4j/slf4j-api/1.0-SNAPSHOT/slf4j-api-1.0-20240101.123456-1.jar"))
80 assert.True(t, m.Matched)
81 assert.Equal(t, policy.Coordinate{Ecosystem: "maven", Name: "org.slf4j:slf4j-api", Version: "1.0-SNAPSHOT"}, m.Coordinate)
84func TestMavenSnapshotTimestampedSidecarIsNoMatch(t *testing.T) {
85 t.Parallel()
86 m := MavenMatcher{}.Match(mvn(http.MethodGet,
87 "/maven2/org/slf4j/slf4j-api/1.0-SNAPSHOT/slf4j-api-1.0-20240101.123456-1.jar.sha1"))
88 assert.False(t, m.Matched)
91func TestMavenDoesNotDoubleDecodePath(t *testing.T) {
92 t.Parallel()
93 // req.URL.Path is already decoded once by Go's parser. The matcher must
94 // evaluate that once-decoded path (what the upstream server receives) and
95 // not decode it a second time. Simulate a parser output where a segment
96 // still contains a literal "%2e": upstream sees "...slf4j-api-2.0.13%2ejar"
97 // (an unrecognized extension, not a .jar), so the matcher must not match.
98 // A second PathUnescape would turn "%2ejar" into ".jar" and wrongly match.
99 req := mvn(http.MethodGet,
100 "/maven2/org/slf4j/slf4j-api/2.0.13/slf4j-api-2.0.13.jar")
101 req.URL.Path = "/maven2/org/slf4j/slf4j-api/2.0.13/slf4j-api-2.0.13%2ejar"
102 m := MavenMatcher{}.Match(req)
103 assert.False(t, m.Matched, "matcher must not perform a second percent-decode of req.URL.Path")
106func TestMavenVersionPrefixDoesNotMatchLongerFilenameVersionFailsClosed(t *testing.T) {
107 t.Parallel()
108 // The version directory is "1" but the filename is version "10". A plain
109 // prefix check accepts "foo-10.jar" for version "1" because it starts with
110 // "foo-1"; the matcher requires a boundary after the version so it does not
111 // report the wrong coordinate. The request still fetches a primary artifact
112 // (.jar), so rather than forwarding it uninspected it fails closed.
113 m := MavenMatcher{}.Match(mvn(http.MethodGet,
114 "/maven2/com/example/foo/1/foo-10.jar"))
115 assert.True(t, m.Matched)
116 assert.False(t, m.Pass, "a primary artifact whose coordinate cannot be parsed must fail closed")
117 assert.Equal(t, "maven", m.Coordinate.Ecosystem)
120func TestMavenClassifierArtifactIsMatched(t *testing.T) {
121 t.Parallel()
122 // "<artifact>-<version>-<classifier>.<ext>" is a valid Maven filename; the
123 // "-" after the version is a legitimate boundary and must still match.
124 m := MavenMatcher{}.Match(mvn(http.MethodGet,
125 "/maven2/org/slf4j/slf4j-api/2.0.13/slf4j-api-2.0.13-sources.jar"))
126 assert.True(t, m.Matched)
127 assert.Equal(t, policy.Coordinate{Ecosystem: "maven", Name: "org.slf4j:slf4j-api", Version: "2.0.13"}, m.Coordinate)
130func TestMavenAarAndWarAreMatched(t *testing.T) {
131 t.Parallel()
132 aar := MavenMatcher{}.Match(mvn(http.MethodGet,
133 "/maven2/com/example/widget/1.2.3/widget-1.2.3.aar"))
134 assert.True(t, aar.Matched)
135 assert.Equal(t, "com.example:widget", aar.Coordinate.Name)
137 war := MavenMatcher{}.Match(mvn(http.MethodGet,
138 "/maven2/com/example/app/1.2.3/app-1.2.3.war"))
139 assert.True(t, war.Matched)
140 assert.Equal(t, "com.example:app", war.Coordinate.Name)
143func TestMavenPrimaryOutsideRepositoryLayoutIsForwarded(t *testing.T) {
144 t.Parallel()
145 // The matcher sees every request in a gradle or mvn session, not only
146 // repository traffic. A primary extension whose path does not name a
147 // group/artifact/version coordinate is an ordinary download, so it is
148 // forwarded rather than blocked.
149 urls := map[string]string{
150 "github release": "https://github.com/foo/bar/releases/download/v1.2.3/bar-all.jar",
151 "ivy layout": "https://repo.typesafe.com/typesafe/ivy-releases/org.scala-sbt/sbt/1.0.0/jars/sbt.jar",
152 "tool download": "https://download.example.com/tools/lombok.jar",
153 "too few segments": "https://repo.maven.apache.org/maven2/foo-1.0.jar",
154 "other artifact dir": "https://repo.maven.apache.org/maven2/com/example/foo/1.0/bar-1.0.jar",
155 }
156 for name, u := range urls {
157 t.Run(name, func(t *testing.T) {
158 t.Parallel()
159 m := MavenMatcher{}.Match(httptest.NewRequest(http.MethodGet, u, http.NoBody))
160 assert.False(t, m.Matched)
161 })
162 }
165func TestMavenFilenameClaimingOtherVersionFailsClosed(t *testing.T) {
166 t.Parallel()
167 // The filename names the artifact in its directory but a different
168 // version, so the coordinate cannot be trusted: fail closed.
169 m := MavenMatcher{}.Match(mvn(http.MethodGet, "/maven2/com/example/foo/1.0/foo-2.0.jar"))
170 assert.True(t, m.Matched)
171 assert.False(t, m.Pass)
172 assert.Equal(t, "maven", m.Coordinate.Ecosystem)
Diagnostics7 git calls · 30 ms
CallStatusSizeTime030 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=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
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=4
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 · 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=3
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: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=18
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=7503ef36a75d3595b473035c69e639fd0ce41598
Open pack map
fetch2002.0 KiB · hit18 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=2030
git.response.cache=HIT
git.response.duration_ms=18
git.fetch.wants=3aea0dfd9a21ea810d6f0ec9e4830c837a6b86cd
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=2030
git.pkt.payload.max=2003
git.sideband.band1.bytes=2003
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
a5ea93410d0ba87d
Request
8f6ebf3e…
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