-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker_socket_proxy_test.go
More file actions
338 lines (301 loc) · 10 KB
/
docker_socket_proxy_test.go
File metadata and controls
338 lines (301 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package bbox
import (
"archive/tar"
"bytes"
"net/http/httptest"
"strings"
"testing"
)
func TestEvaluateDockerBuildRejectsRemoteContextParameters(t *testing.T) {
compiled, err := compileDockerSocketPolicy(DockerSocketPolicy{
DefaultAction: DockerRuleActionDeny,
Rules: []DockerSocketRule{
{
Action: DockerRuleActionAllow,
Operations: []DockerOperation{"build"},
Build: &DockerBuildMatch{
Context: DockerBuildContextMatchLocalOnly,
DockerfilePaths: []string{
"^Dockerfile$",
},
},
},
},
})
if err != nil {
t.Fatalf("compile policy: %v", err)
}
req := httptest.NewRequest("POST", "/v1.52/build?remote=https://example.com/context.tar.gz", nil)
buildReq, err := inspectDockerBuildRequest(req, nil)
if err != nil {
t.Fatalf("inspect build request: %v", err)
}
got := compiled.evaluate(dockerSocketRequest{
Method: req.Method,
Path: req.URL.Path,
Operation: DockerOperation("build"),
Build: &buildReq,
})
if got != DockerRuleActionDeny {
t.Fatalf("expected remote context build to be denied, got %q", got)
}
}
func TestEvaluateDockerBuildRejectsRegistryExporters(t *testing.T) {
compiled, err := compileDockerSocketPolicy(DockerSocketPolicy{
DefaultAction: DockerRuleActionDeny,
Rules: []DockerSocketRule{
{
Action: DockerRuleActionAllow,
Operations: []DockerOperation{"build"},
Build: &DockerBuildMatch{
Context: DockerBuildContextMatchLocalOnly,
DockerfilePaths: []string{
"^Dockerfile$",
},
},
},
},
})
if err != nil {
t.Fatalf("compile policy: %v", err)
}
req := httptest.NewRequest("POST", "/v1.52/build?outputs=%5B%7B%22type%22%3A%22registry%22%2C%22name%22%3A%22ghcr.io%2Facme%2Fdemo%3Alatest%22%7D%5D", nil)
buildReq, err := inspectDockerBuildRequest(req, nil)
if err != nil {
t.Fatalf("inspect build request: %v", err)
}
got := compiled.evaluate(dockerSocketRequest{
Method: req.Method,
Path: req.URL.Path,
Operation: DockerOperation("build"),
Build: &buildReq,
})
if got != DockerRuleActionDeny {
t.Fatalf("expected registry exporter build to be denied, got %q", got)
}
}
func TestEvaluateDockerBuildAllowsLocalTarContextWithApprovedDockerfile(t *testing.T) {
compiled, err := compileDockerSocketPolicy(DockerSocketPolicy{
DefaultAction: DockerRuleActionDeny,
Rules: []DockerSocketRule{
{
Action: DockerRuleActionAllow,
Operations: []DockerOperation{"build"},
Build: &DockerBuildMatch{
Context: DockerBuildContextMatchLocalOnly,
DockerfilePaths: []string{
"^Dockerfile$",
"^docker/.*$",
},
},
},
},
})
if err != nil {
t.Fatalf("compile policy: %v", err)
}
body := buildContextTar(t, "docker/app.Dockerfile")
req := httptest.NewRequest("POST", "/v1.52/build?dockerfile=docker%2Fapp.Dockerfile&t=acme%2Fdemo%3Alatest", bytes.NewReader(body))
req.Header.Set("Content-Type", "Application/X-Tar")
buildReq, err := inspectDockerBuildRequest(req, body)
if err != nil {
t.Fatalf("inspect build request: %v", err)
}
got := compiled.evaluate(dockerSocketRequest{
Method: req.Method,
Path: req.URL.Path,
Operation: DockerOperation("build"),
Build: &buildReq,
})
if got != DockerRuleActionAllow {
t.Fatalf("expected local tar build to be allowed, got %q", got)
}
}
func TestEvaluateDockerBuildRejectsDockerfilePathTraversal(t *testing.T) {
body := buildContextTar(t, "Dockerfile")
req := httptest.NewRequest("POST", "/build?dockerfile=../Dockerfile", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/x-tar")
_, err := inspectDockerBuildRequest(req, body)
if err == nil {
t.Fatal("expected dockerfile path traversal to fail")
}
if err.Error() != "build dockerfile path cannot be empty" {
t.Fatalf("expected dockerfile path error, got %q", err.Error())
}
}
func TestEvaluateDockerBuildRejectsMissingDockerfileInTar(t *testing.T) {
body := buildContextTar(t, "docker/Otherfile")
req := httptest.NewRequest("POST", "/build", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/x-tar")
_, err := inspectDockerBuildRequest(req, body)
if err == nil {
t.Fatal("expected missing dockerfile in tar to fail")
}
if err.Error() != "docker build context missing dockerfile \"Dockerfile\"" {
t.Fatalf("expected missing dockerfile error, got %q", err.Error())
}
}
func TestEvaluateDockerBuildRejectsNonTarContentType(t *testing.T) {
body := buildContextTar(t, "Dockerfile")
req := httptest.NewRequest("POST", "/build", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
_, err := inspectDockerBuildRequest(req, body)
if err == nil {
t.Fatal("expected non-tar content type to fail")
}
if err.Error() != "unsupported docker build content type \"application/json\"" {
t.Fatalf("expected content type error, got %q", err.Error())
}
}
func TestEvaluateDockerBuildRejectsMalformedOutputsJSON(t *testing.T) {
body := buildContextTar(t, "Dockerfile")
req := httptest.NewRequest("POST", "/build?outputs=%5B", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/x-tar")
_, err := inspectDockerBuildRequest(req, body)
if err == nil {
t.Fatal("expected malformed outputs JSON to fail")
}
if !strings.HasPrefix(err.Error(), "parse docker build outputs:") {
t.Fatalf("expected outputs parse error, got %q", err.Error())
}
}
func TestEvaluateDockerBuildRejectsAmbiguousPushParameter(t *testing.T) {
for _, rawQuery := range []string{"push", "push="} {
t.Run(rawQuery, func(t *testing.T) {
body := buildContextTar(t, "Dockerfile")
req := httptest.NewRequest("POST", "/build?"+rawQuery, bytes.NewReader(body))
req.Header.Set("Content-Type", "application/x-tar")
_, err := inspectDockerBuildRequest(req, body)
if err == nil {
t.Fatal("expected ambiguous push parameter to fail")
}
if err.Error() != "docker build push parameter cannot be empty" {
t.Fatalf("expected push ambiguity error, got %q", err.Error())
}
})
}
}
func TestEvaluateDockerBuildAllowsPushZeroForLocalTarContext(t *testing.T) {
body := buildContextTar(t, "Dockerfile")
req := httptest.NewRequest("POST", "/build?push=0", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/x-tar")
buildReq, err := inspectDockerBuildRequest(req, body)
if err != nil {
t.Fatalf("inspect build request: %v", err)
}
if buildReq.BodyKind != "tar" {
t.Fatalf("expected push=0 to preserve tar body semantics, got %q", buildReq.BodyKind)
}
}
func TestEvaluateDockerBuildRejectsAmbiguousMultiValuePushParameter(t *testing.T) {
for _, tc := range []struct {
name string
query string
wantErr string
}{
{
name: "truthy and empty",
query: "push=1&push=",
wantErr: "docker build push parameter cannot be empty",
},
{
name: "truthy and ambiguous",
query: "push=1&push=maybe",
wantErr: "docker build push parameter \"maybe\" is ambiguous",
},
} {
t.Run(tc.name, func(t *testing.T) {
body := buildContextTar(t, "Dockerfile")
req := httptest.NewRequest("POST", "/build?"+tc.query, bytes.NewReader(body))
req.Header.Set("Content-Type", "application/x-tar")
_, err := inspectDockerBuildRequest(req, body)
if err == nil {
t.Fatal("expected multi-valued push parameter to fail")
}
if err.Error() != tc.wantErr {
t.Fatalf("expected multi-valued push error %q, got %q", tc.wantErr, err.Error())
}
})
}
}
func TestEvaluateDockerBuildAllowsFalseyMultiValuePushParameter(t *testing.T) {
body := buildContextTar(t, "Dockerfile")
req := httptest.NewRequest("POST", "/build?push=0&push=false", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/x-tar")
buildReq, err := inspectDockerBuildRequest(req, body)
if err != nil {
t.Fatalf("inspect build request: %v", err)
}
if buildReq.BodyKind != "tar" {
t.Fatalf("expected falsey multi-valued push to preserve tar body semantics, got %q", buildReq.BodyKind)
}
}
func TestEvaluateDockerBuildRejectsConflictingRemoteAndTarContextSignals(t *testing.T) {
body := buildContextTar(t, "Dockerfile")
req := httptest.NewRequest("POST", "/build?remote=https://example.com/context.tar.gz", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/x-tar")
_, err := inspectDockerBuildRequest(req, body)
if err == nil {
t.Fatal("expected conflicting remote and tar signals to fail")
}
if err.Error() != "conflicting docker build context signals: remote and tar" {
t.Fatalf("expected conflicting signal error, got %q", err.Error())
}
}
func TestEvaluateDockerBuildHTTPPathMatchDoesNotBypassBuildGuards(t *testing.T) {
compiled, err := compileDockerSocketPolicy(DockerSocketPolicy{
DefaultAction: DockerRuleActionDeny,
Rules: []DockerSocketRule{
{
Action: DockerRuleActionAllow,
Operations: []DockerOperation{"build"},
HTTP: &DockerHTTPMatch{
Methods: []string{"POST"},
PathPatterns: []string{"^/build$"},
},
Build: &DockerBuildMatch{
Context: DockerBuildContextMatchLocalOnly,
DockerfilePaths: []string{
"^Dockerfile$",
},
},
},
},
})
if err != nil {
t.Fatalf("compile policy: %v", err)
}
req := httptest.NewRequest("POST", "/build?remote=https://example.com/context.tar.gz", nil)
buildReq, err := inspectDockerBuildRequest(req, nil)
if err != nil {
t.Fatalf("inspect build request: %v", err)
}
got := compiled.evaluate(dockerSocketRequest{
Method: req.Method,
Path: req.URL.Path,
Operation: DockerOperation("build"),
Build: &buildReq,
})
if got != DockerRuleActionDeny {
t.Fatalf("expected remote context build to be denied even when the HTTP path matches, got %q", got)
}
}
func buildContextTar(t *testing.T, dockerfilePath string) []byte {
t.Helper()
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
if err := tw.WriteHeader(&tar.Header{
Name: dockerfilePath,
Mode: 0o644,
Size: int64(len("FROM scratch\n")),
}); err != nil {
t.Fatalf("write tar header: %v", err)
}
if _, err := tw.Write([]byte("FROM scratch\n")); err != nil {
t.Fatalf("write tar body: %v", err)
}
if err := tw.Close(); err != nil {
t.Fatalf("close tar writer: %v", err)
}
return buf.Bytes()
}