GitLabgitlab-org
cli
main
1//go:build !integration
3package download
5import (
6 "errors"
7 "io"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 "go.uber.org/mock/gomock"
17 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
18 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
20 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
21)
23const (
24 fileContents = "Hello"
25 fileContentsChecksum = "185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969"
26 repoName = "OWNER/REPO"
27 fileName = "localfile.txt"
28)
30func Test_SecurefileDownload(t *testing.T) {
31 testCases := []struct {
32 Name string
33 ExpectedMsg []string
34 expectedFileName string
35 wantErr bool
36 cli string
37 wantStderr string
38 setupMocks func(*gitlabtesting.TestClient)
39 }{
40 {
41 Name: "Download secure file to current folder with checksum verification via id arg",
42 ExpectedMsg: []string{"Downloaded secure file 'downloaded.tmp' (ID: 1)\n"},
43 expectedFileName: "downloaded.tmp",
44 cli: "1",
45 setupMocks: func(testClient *gitlabtesting.TestClient) {
46 testClient.MockSecureFiles.EXPECT().
47 DownloadSecureFile(repoName, int64(1), gomock.Any()).
48 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
49 testClient.MockSecureFiles.EXPECT().
50 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
51 Return(&gitlab.SecureFile{
52 ID: 1,
53 Name: fileName,
54 Checksum: fileContentsChecksum,
55 }, nil, nil)
56 },
57 },
58 {
59 Name: "Download secure file to current folder with checksum verification via id flag",
60 ExpectedMsg: []string{"Downloaded secure file 'downloaded.tmp' (ID: 1)\n"},
61 expectedFileName: "downloaded.tmp",
62 cli: "--id 1",
63 setupMocks: func(testClient *gitlabtesting.TestClient) {
64 testClient.MockSecureFiles.EXPECT().
65 DownloadSecureFile(repoName, int64(1), gomock.Any()).
66 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
67 testClient.MockSecureFiles.EXPECT().
68 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
69 Return(&gitlab.SecureFile{
70 ID: 1,
71 Name: fileName,
72 Checksum: fileContentsChecksum,
73 }, nil, nil)
74 },
75 },
76 {
77 Name: "Download secure file to custom folder",
78 ExpectedMsg: []string{"Downloaded secure file 'new.txt' (ID: 1)\n"},
79 expectedFileName: "newdir/new.txt",
80 cli: "1 --path=newdir/new.txt",
81 setupMocks: func(testClient *gitlabtesting.TestClient) {
82 testClient.MockSecureFiles.EXPECT().
83 DownloadSecureFile(repoName, int64(1), gomock.Any()).
84 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
85 testClient.MockSecureFiles.EXPECT().
86 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
87 Return(&gitlab.SecureFile{
88 ID: 1,
89 Name: fileName,
90 Checksum: fileContentsChecksum,
91 }, nil, nil)
92 },
93 },
94 {
95 Name: "Download secure file to custom folder with path traversal",
96 ExpectedMsg: []string{"Downloaded secure file 'new.txt' (ID: 1)\n"},
97 cli: "1 --path=../../newdir/new.txt",
98 wantErr: true,
99 wantStderr: `relative path "../../newdir/new.txt" escapes the working directory`,
100 setupMocks: func(testClient *gitlabtesting.TestClient) {},
101 },
102 {
103 Name: "Download secure file without checksum verification",
104 ExpectedMsg: []string{"Downloaded secure file 'downloaded.tmp' (ID: 1)\n"},
105 expectedFileName: "downloaded.tmp",
106 cli: "1 --no-verify",
107 setupMocks: func(testClient *gitlabtesting.TestClient) {
108 testClient.MockSecureFiles.EXPECT().
109 DownloadSecureFile(repoName, int64(1), gomock.Any()).
110 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
111 },
112 },
113 {
114 Name: "Download secure file with invalid checksum",
115 ExpectedMsg: []string{"checksum verification failed for localfile.txt: expected invalid_checksum, got 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969\n"},
116 expectedFileName: "downloaded.tmp",
117 cli: "1",
118 wantErr: true,
119 wantStderr: "checksum verification failed for localfile.txt: expected invalid_checksum, got 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969",
120 setupMocks: func(testClient *gitlabtesting.TestClient) {
121 testClient.MockSecureFiles.EXPECT().
122 DownloadSecureFile(repoName, int64(1), gomock.Any()).
123 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
124 testClient.MockSecureFiles.EXPECT().
125 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
126 Return(&gitlab.SecureFile{
127 ID: 1,
128 Name: fileName,
129 Checksum: "invalid_checksum",
130 }, nil, nil)
131 },
132 },
133 {
134 Name: "Force download secure file with invalid checksum",
135 ExpectedMsg: []string{
136 "Checksum verification failed for localfile.txt: expected invalid_checksum, got 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969\n",
137 "Force-download selected, continuing to download file.\n",
138 "Downloaded secure file 'downloaded.tmp' (ID: 1)\n",
139 },
140 expectedFileName: "downloaded.tmp",
141 cli: "1 --force-download",
142 setupMocks: func(testClient *gitlabtesting.TestClient) {
143 testClient.MockSecureFiles.EXPECT().
144 DownloadSecureFile(repoName, int64(1), gomock.Any()).
145 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
146 testClient.MockSecureFiles.EXPECT().
147 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
148 Return(&gitlab.SecureFile{
149 ID: 1,
150 Name: fileName,
151 Checksum: "invalid_checksum",
152 }, nil, nil)
153 },
154 },
155 {
156 Name: "Download secure file by name to current directory",
157 ExpectedMsg: []string{
158 "Downloaded secure file 'file2.txt' (Name: file2.txt)\n",
159 },
160 cli: "--name file2.txt",
161 expectedFileName: "file2.txt",
162 setupMocks: func(testClient *gitlabtesting.TestClient) {
163 testClient.MockSecureFiles.EXPECT().
164 ListProjectSecureFiles(repoName, &gitlab.ListProjectSecureFilesOptions{
165 ListOptions: gitlab.ListOptions{
166 Page: 1,
167 PerPage: 100,
168 },
169 }, nil).
170 Return([]*gitlab.SecureFile{
171 {ID: 1, Name: "file1.txt", Checksum: fileContentsChecksum},
172 {ID: 2, Name: "file2.txt", Checksum: fileContentsChecksum},
173 }, nil, nil)
175 testClient.MockSecureFiles.EXPECT().
176 DownloadSecureFile(repoName, int64(2), gomock.Any()).
177 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
178 testClient.MockSecureFiles.EXPECT().
179 ShowSecureFileDetails(repoName, int64(2), gomock.Any()).
180 Return(&gitlab.SecureFile{
181 ID: 2,
182 Name: "file2.txt",
183 Checksum: fileContentsChecksum,
184 }, nil, nil)
185 },
186 },
187 {
188 Name: "Download secure file by name to custom output directory",
189 ExpectedMsg: []string{
190 "Downloaded secure file 'file1.txt' (Name: file1.txt)",
191 },
192 cli: "--name file1.txt --path=secure_files/file1.txt",
193 expectedFileName: "secure_files/file1.txt",
194 setupMocks: func(testClient *gitlabtesting.TestClient) {
195 testClient.MockSecureFiles.EXPECT().
196 ListProjectSecureFiles(repoName, &gitlab.ListProjectSecureFilesOptions{
197 ListOptions: gitlab.ListOptions{
198 Page: 1,
199 PerPage: 100,
200 },
201 }, nil).
202 Return([]*gitlab.SecureFile{
203 {ID: 1, Name: "file2.pdf", Checksum: fileContentsChecksum},
204 {ID: 2, Name: "file1.txt", Checksum: fileContentsChecksum},
205 }, nil, nil)
207 testClient.MockSecureFiles.EXPECT().
208 DownloadSecureFile(repoName, int64(2), gomock.Any()).
209 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
210 testClient.MockSecureFiles.EXPECT().
211 ShowSecureFileDetails(repoName, int64(2), gomock.Any()).
212 Return(&gitlab.SecureFile{
213 ID: 2,
214 Name: "file1.txt",
215 Checksum: fileContentsChecksum,
216 }, nil, nil)
217 },
218 },
219 {
220 Name: "Download secure file by name without checksum verification",
221 ExpectedMsg: []string{
222 "Downloaded secure file 'file1.txt' (Name: file1.txt)",
223 },
224 cli: "--name file1.txt --no-verify",
225 expectedFileName: "file1.txt",
226 setupMocks: func(testClient *gitlabtesting.TestClient) {
227 testClient.MockSecureFiles.EXPECT().
228 ListProjectSecureFiles(repoName, &gitlab.ListProjectSecureFilesOptions{
229 ListOptions: gitlab.ListOptions{
230 Page: 1,
231 PerPage: 100,
232 },
233 }, nil).
234 Return([]*gitlab.SecureFile{
235 {ID: 1, Name: "file1.txt", Checksum: fileContentsChecksum},
236 }, nil, nil)
238 testClient.MockSecureFiles.EXPECT().
239 DownloadSecureFile(repoName, int64(1), gomock.Any()).
240 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
241 },
242 },
243 {
244 Name: "Download secure file by name with force download on checksum failure",
245 ExpectedMsg: []string{
246 "Checksum verification failed for file1.txt: expected invalid_checksum, got 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969",
247 "Force-download selected, continuing to download file.",
248 "Downloaded secure file 'file1.txt' (Name: file1.txt)",
249 },
250 cli: "--name file1.txt --force-download",
251 expectedFileName: "file1.txt",
252 setupMocks: func(testClient *gitlabtesting.TestClient) {
253 testClient.MockSecureFiles.EXPECT().
254 ListProjectSecureFiles(repoName, &gitlab.ListProjectSecureFilesOptions{
255 ListOptions: gitlab.ListOptions{
256 Page: 1,
257 PerPage: 100,
258 },
259 }, nil).
260 Return([]*gitlab.SecureFile{
261 {ID: 1, Name: "file1.txt", Checksum: "invalid_checksum"},
262 }, nil, nil)
264 testClient.MockSecureFiles.EXPECT().
265 DownloadSecureFile(repoName, int64(1), gomock.Any()).
266 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
267 testClient.MockSecureFiles.EXPECT().
268 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
269 Return(&gitlab.SecureFile{
270 ID: 1,
271 Name: "file1.txt",
272 Checksum: "invalid_checksum",
273 }, nil, nil)
274 },
275 },
276 {
277 Name: "Handle empty secure files list",
278 ExpectedMsg: []string{},
279 cli: "--name notfound",
280 expectedFileName: "",
281 wantErr: true,
282 wantStderr: "couldn't locate secure file with name notfound",
283 setupMocks: func(testClient *gitlabtesting.TestClient) {
284 testClient.MockSecureFiles.EXPECT().
285 ListProjectSecureFiles(repoName, &gitlab.ListProjectSecureFilesOptions{
286 ListOptions: gitlab.ListOptions{
287 Page: 1,
288 PerPage: 100,
289 },
290 }, nil).
291 Return([]*gitlab.SecureFile{}, &gitlab.Response{
292 NextPage: 0,
293 NextLink: "",
294 }, nil)
295 },
296 },
297 {
298 Name: "Error when --name flag is used with fileID argument",
299 cli: "--name foobar 1",
300 wantErr: true,
301 wantStderr: "name flag is not compatible with arguments",
302 setupMocks: func(testClient *gitlabtesting.TestClient) {},
303 },
304 {
305 Name: "Error when --name flag is used with --id flag",
306 cli: "--name foobar --id 1",
307 wantErr: true,
308 wantStderr: "if any flags in the group [id name all] are set none of the others can be; [id name] were all set",
309 setupMocks: func(testClient *gitlabtesting.TestClient) {},
310 },
311 {
312 Name: "Error when output-dir flag is used without all flag",
313 cli: "1 --output-dir=secure_files",
314 wantErr: true,
315 wantStderr: "output-dir flag is only compatible with all flag",
316 setupMocks: func(testClient *gitlabtesting.TestClient) {},
317 },
318 }
320 for _, tc := range testCases {
321 t.Run(tc.Name, func(t *testing.T) {
322 tempDir := t.TempDir()
323 t.Chdir(tempDir)
325 testClient := gitlabtesting.NewTestClient(t)
326 tc.setupMocks(testClient)
328 exec := cmdtest.SetupCmdForTest(t, NewCmdDownload, false,
329 cmdtest.WithGitLabClient(testClient.Client),
330 )
331 out, err := exec(tc.cli)
333 if tc.wantErr {
334 if assert.Error(t, err) {
335 require.Equal(t, tc.wantStderr, err.Error())
336 }
338 if tc.expectedFileName != "" {
339 // Ensure downloaded file and temp files are not persisted/cleaned up following an error
340 files, err := os.ReadDir(filepath.Dir(tc.expectedFileName))
341 require.NoError(t, err, "Failed to read directory")
342 assert.Empty(t, files, "Directory should be empty after error")
343 }
345 return
346 }
347 require.NoError(t, err)
349 for _, msg := range tc.ExpectedMsg {
350 require.Contains(t, out.String(), msg)
351 }
353 _, err = os.Stat(tc.expectedFileName)
354 require.NoError(t, err)
356 actualContent, err := os.ReadFile(tc.expectedFileName)
357 require.NoError(t, err, "Failed to read downloaded test file")
359 assert.Equal(t, "Hello", string(actualContent), "File content should match")
360 })
361 }
362}
364func Test_SecurefileDownloadAll(t *testing.T) {
365 testCases := []struct {
366 Name string
367 ExpectedMsg []string
368 cli string
369 wantErr bool
370 wantStderr string
371 expectedFiles []string
372 setupMocks func(*gitlabtesting.TestClient)
373 }{
374 {
375 Name: "Download all secure files to current directory",
376 ExpectedMsg: []string{
377 "Downloaded secure file 'file1.txt' (ID: 1)",
378 "Downloaded secure file 'file2.pdf' (ID: 2)",
379 },
380 cli: "--all",
381 expectedFiles: []string{"file1.txt", "file2.pdf"},
382 setupMocks: func(testClient *gitlabtesting.TestClient) {
383 firstPage := testClient.MockSecureFiles.EXPECT().
384 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
385 Return([]*gitlab.SecureFile{
386 {ID: 1, Name: "file1.txt", Checksum: fileContentsChecksum},
387 }, &gitlab.Response{NextPage: 2}, nil)
389 testClient.MockSecureFiles.EXPECT().
390 DownloadSecureFile(repoName, int64(1), gomock.Any()).
391 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
392 testClient.MockSecureFiles.EXPECT().
393 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
394 Return(&gitlab.SecureFile{
395 ID: 1,
396 Name: "file1.txt",
397 Checksum: fileContentsChecksum,
398 }, nil, nil)
400 secondPage := testClient.MockSecureFiles.EXPECT().
401 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
402 Return([]*gitlab.SecureFile{
403 {ID: 2, Name: "file2.pdf", Checksum: fileContentsChecksum},
404 }, &gitlab.Response{}, nil)
405 gomock.InOrder(firstPage, secondPage)
407 testClient.MockSecureFiles.EXPECT().
408 DownloadSecureFile(repoName, int64(2), gomock.Any()).
409 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
410 testClient.MockSecureFiles.EXPECT().
411 ShowSecureFileDetails(repoName, int64(2), gomock.Any()).
412 Return(&gitlab.SecureFile{
413 ID: 2,
414 Name: "file2.pdf",
415 Checksum: fileContentsChecksum,
416 }, nil, nil)
417 },
418 },
419 {
420 Name: "Return an error from a subsequent page",
421 cli: "--all",
422 wantErr: true,
423 wantStderr: "error fetching secure files: next page unavailable",
424 setupMocks: func(testClient *gitlabtesting.TestClient) {
425 firstPage := testClient.MockSecureFiles.EXPECT().
426 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
427 Return([]*gitlab.SecureFile{
428 {ID: 1, Name: "file1.txt", Checksum: fileContentsChecksum},
429 }, &gitlab.Response{NextPage: 2}, nil)
430 testClient.MockSecureFiles.EXPECT().
431 DownloadSecureFile(repoName, int64(1), gomock.Any()).
432 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
433 testClient.MockSecureFiles.EXPECT().
434 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
435 Return(&gitlab.SecureFile{
436 ID: 1,
437 Name: "file1.txt",
438 Checksum: fileContentsChecksum,
439 }, nil, nil)
440 secondPage := testClient.MockSecureFiles.EXPECT().
441 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
442 Return(nil, nil, errors.New("next page unavailable"))
443 gomock.InOrder(firstPage, secondPage)
444 },
445 },
446 {
447 Name: "Download all secure files to custom output directory",
448 ExpectedMsg: []string{
449 "Downloaded secure file 'file1.txt' (ID: 1)",
450 "Downloaded secure file 'file2.pdf' (ID: 2)",
451 },
452 cli: "--all --output-dir=secure_files",
453 expectedFiles: []string{"secure_files/file1.txt", "secure_files/file2.pdf"},
454 setupMocks: func(testClient *gitlabtesting.TestClient) {
455 testClient.MockSecureFiles.EXPECT().
456 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
457 Return([]*gitlab.SecureFile{
458 {ID: 1, Name: "file1.txt", Checksum: fileContentsChecksum},
459 {ID: 2, Name: "file2.pdf", Checksum: fileContentsChecksum},
460 }, &gitlab.Response{}, nil)
462 testClient.MockSecureFiles.EXPECT().
463 DownloadSecureFile(repoName, int64(1), gomock.Any()).
464 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
465 testClient.MockSecureFiles.EXPECT().
466 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
467 Return(&gitlab.SecureFile{
468 ID: 1,
469 Name: "file1.txt",
470 Checksum: fileContentsChecksum,
471 }, nil, nil)
473 testClient.MockSecureFiles.EXPECT().
474 DownloadSecureFile(repoName, int64(2), gomock.Any()).
475 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
476 testClient.MockSecureFiles.EXPECT().
477 ShowSecureFileDetails(repoName, int64(2), gomock.Any()).
478 Return(&gitlab.SecureFile{
479 ID: 2,
480 Name: "file2.pdf",
481 Checksum: fileContentsChecksum,
482 }, nil, nil)
483 },
484 },
485 {
486 Name: "Download all secure files without checksum verification",
487 ExpectedMsg: []string{
488 "Downloaded secure file 'file1.txt' (ID: 1)",
489 },
490 cli: "--all --no-verify",
491 expectedFiles: []string{"file1.txt"},
492 setupMocks: func(testClient *gitlabtesting.TestClient) {
493 testClient.MockSecureFiles.EXPECT().
494 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
495 Return([]*gitlab.SecureFile{
496 {ID: 1, Name: "file1.txt", Checksum: fileContentsChecksum},
497 }, &gitlab.Response{}, nil)
499 testClient.MockSecureFiles.EXPECT().
500 DownloadSecureFile(repoName, int64(1), gomock.Any()).
501 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
502 },
503 },
504 {
505 Name: "Download all secure files with force download on checksum failure",
506 ExpectedMsg: []string{
507 "Checksum verification failed for file1.txt: expected invalid_checksum, got 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969",
508 "Force-download selected, continuing to download file.",
509 "Downloaded secure file 'file1.txt' (ID: 1)",
510 },
511 cli: "--all --force-download",
512 expectedFiles: []string{"file1.txt"},
513 setupMocks: func(testClient *gitlabtesting.TestClient) {
514 testClient.MockSecureFiles.EXPECT().
515 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
516 Return([]*gitlab.SecureFile{
517 {ID: 1, Name: "file1.txt", Checksum: "invalid_checksum"},
518 }, &gitlab.Response{}, nil)
520 testClient.MockSecureFiles.EXPECT().
521 DownloadSecureFile(repoName, int64(1), gomock.Any()).
522 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
523 testClient.MockSecureFiles.EXPECT().
524 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
525 Return(&gitlab.SecureFile{
526 ID: 1,
527 Name: "file1.txt",
528 Checksum: "invalid_checksum",
529 }, nil, nil)
530 },
531 },
532 {
533 Name: "Error when --all flag is used with fileID argument",
534 cli: "--all 1",
535 wantErr: true,
536 wantStderr: "all flag is not compatible with arguments",
537 setupMocks: func(testClient *gitlabtesting.TestClient) {},
538 },
539 {
540 Name: "Handle empty secure files list",
541 ExpectedMsg: []string{},
542 cli: "--all",
543 expectedFiles: []string{},
544 setupMocks: func(testClient *gitlabtesting.TestClient) {
545 testClient.MockSecureFiles.EXPECT().
546 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
547 Return([]*gitlab.SecureFile{}, &gitlab.Response{}, nil)
548 },
549 },
550 {
551 Name: "Handle secure file with path traversal",
552 ExpectedMsg: []string{
553 "Downloaded secure file 'passwd' (ID: 1)\n",
554 },
555 cli: "--all --output-dir=../../secure_files",
556 wantErr: true,
557 wantStderr: "error downloading secure file '/etc/passwd' (ID: 1): name would write outside the output directory",
558 setupMocks: func(testClient *gitlabtesting.TestClient) {
559 testClient.MockSecureFiles.EXPECT().
560 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
561 Return([]*gitlab.SecureFile{
562 {ID: 1, Name: "/etc/passwd", Checksum: fileContentsChecksum},
563 }, &gitlab.Response{}, nil)
564 },
565 },
566 {
567 Name: "Download all secure files with checksum error",
568 cli: "--all",
569 wantErr: true,
570 wantStderr: "error downloading secure file 'file2.pdf' (ID: 2): checksum verification failed for file2.pdf: expected invalid_checksum, got 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969",
571 setupMocks: func(testClient *gitlabtesting.TestClient) {
572 testClient.MockSecureFiles.EXPECT().
573 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
574 Return([]*gitlab.SecureFile{
575 {ID: 1, Name: "file1.txt", Checksum: fileContentsChecksum},
576 {ID: 2, Name: "file2.pdf", Checksum: "invalid_checksum"},
577 }, &gitlab.Response{}, nil)
579 testClient.MockSecureFiles.EXPECT().
580 DownloadSecureFile(repoName, int64(1), gomock.Any()).
581 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
582 testClient.MockSecureFiles.EXPECT().
583 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
584 Return(&gitlab.SecureFile{
585 ID: 1,
586 Name: "file1.txt",
587 Checksum: fileContentsChecksum,
588 }, nil, nil)
590 testClient.MockSecureFiles.EXPECT().
591 DownloadSecureFile(repoName, int64(2), gomock.Any()).
592 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
593 testClient.MockSecureFiles.EXPECT().
594 ShowSecureFileDetails(repoName, int64(2), gomock.Any()).
595 Return(&gitlab.SecureFile{
596 ID: 2,
597 Name: "file2.pdf",
598 Checksum: "invalid_checksum",
599 }, nil, nil)
600 },
601 },
602 }
604 for _, tc := range testCases {
605 t.Run(tc.Name, func(t *testing.T) {
606 tempDir := t.TempDir()
607 t.Chdir(tempDir)
609 testClient := gitlabtesting.NewTestClient(t)
610 tc.setupMocks(testClient)
612 exec := cmdtest.SetupCmdForTest(t, NewCmdDownload, false,
613 cmdtest.WithGitLabClient(testClient.Client),
614 )
615 out, err := exec(tc.cli)
617 if tc.wantErr {
618 if assert.Error(t, err) {
619 require.Equal(t, tc.wantStderr, err.Error())
620 }
621 return
622 }
623 require.NoError(t, err)
625 for _, msg := range tc.ExpectedMsg {
626 require.Contains(t, out.String(), msg)
627 }
629 for _, expectedFile := range tc.expectedFiles {
630 _, err = os.Stat(expectedFile)
631 require.NoError(t, err, "Expected file %s should exist", expectedFile)
633 actualContent, err := os.ReadFile(expectedFile)
634 require.NoError(t, err, "Failed to read downloaded test file %s", expectedFile)
635 assert.Equal(t, "Hello", string(actualContent), "File content should match for %s", expectedFile)
636 }
637 })
638 }
639}
641// The table-driven cases above can only carry static CLI strings, so the
642// absolute-path cases live here where a t.TempDir() can be interpolated.
643func Test_SecurefileDownload_AbsoluteDestination(t *testing.T) {
644 newFile := func(t *testing.T, checksum string) *gitlabtesting.TestClient {
645 t.Helper()
646 testClient := gitlabtesting.NewTestClient(t)
647 testClient.MockSecureFiles.EXPECT().
648 DownloadSecureFile(repoName, int64(1), gomock.Any()).
649 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
650 testClient.MockSecureFiles.EXPECT().
651 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
652 Return(&gitlab.SecureFile{ID: 1, Name: fileName, Checksum: checksum}, nil, nil)
653 return testClient
654 }
656 t.Run("absolute path creates missing parents and writes the file", func(t *testing.T) {
657 t.Chdir(t.TempDir())
658 dest := filepath.Join(t.TempDir(), "nested", "deeper", "secure.txt")
660 exec := cmdtest.SetupCmdForTest(t, NewCmdDownload, false,
661 cmdtest.WithGitLabClient(newFile(t, fileContentsChecksum).Client))
662 out, err := exec("1 --path=" + dest)
664 require.NoError(t, err)
665 assert.Contains(t, out.String(), "Downloaded secure file 'secure.txt' (ID: 1)")
667 got, err := os.ReadFile(dest)
668 require.NoError(t, err)
669 assert.Equal(t, fileContents, string(got))
670 })
672 t.Run("absolute path with --name", func(t *testing.T) {
673 t.Chdir(t.TempDir())
674 dest := filepath.Join(t.TempDir(), "out", "renamed.pem")
676 testClient := gitlabtesting.NewTestClient(t)
677 testClient.MockSecureFiles.EXPECT().
678 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any()).
679 Return([]*gitlab.SecureFile{{ID: 1, Name: fileName, Checksum: fileContentsChecksum}}, &gitlab.Response{}, nil)
680 testClient.MockSecureFiles.EXPECT().
681 DownloadSecureFile(repoName, int64(1), gomock.Any()).
682 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
683 testClient.MockSecureFiles.EXPECT().
684 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
685 Return(&gitlab.SecureFile{ID: 1, Name: fileName, Checksum: fileContentsChecksum}, nil, nil)
687 exec := cmdtest.SetupCmdForTest(t, NewCmdDownload, false,
688 cmdtest.WithGitLabClient(testClient.Client))
689 out, err := exec("--name " + fileName + " --path=" + dest)
691 require.NoError(t, err)
692 assert.Contains(t, out.String(), "Downloaded secure file 'renamed.pem' (Name: "+fileName+")")
694 got, err := os.ReadFile(dest)
695 require.NoError(t, err)
696 assert.Equal(t, fileContents, string(got))
697 })
699 // Cleanup has to work through the absolute root too: this is the case that
700 // leaked a temporary file in the previous attempt at this fix (!2761).
701 t.Run("failed checksum leaves no temporary file behind", func(t *testing.T) {
702 t.Chdir(t.TempDir())
703 destDir := t.TempDir()
704 dest := filepath.Join(destDir, "secure.txt")
706 exec := cmdtest.SetupCmdForTest(t, NewCmdDownload, false,
707 cmdtest.WithGitLabClient(newFile(t, "invalid_checksum").Client))
708 _, err := exec("1 --path=" + dest)
710 require.Error(t, err)
711 entries, readErr := os.ReadDir(destDir)
712 require.NoError(t, readErr)
713 assert.Empty(t, entries, "destination directory should hold no temporary leftovers")
714 })
716 t.Run("absolute --output-dir with --all", func(t *testing.T) {
717 t.Chdir(t.TempDir())
718 destDir := t.TempDir()
720 testClient := gitlabtesting.NewTestClient(t)
721 testClient.MockSecureFiles.EXPECT().
722 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
723 Return([]*gitlab.SecureFile{{ID: 1, Name: "file1.txt", Checksum: fileContentsChecksum}}, &gitlab.Response{}, nil)
724 testClient.MockSecureFiles.EXPECT().
725 DownloadSecureFile(repoName, int64(1), gomock.Any()).
726 Return(io.NopCloser(strings.NewReader(fileContents)), nil, nil)
727 testClient.MockSecureFiles.EXPECT().
728 ShowSecureFileDetails(repoName, int64(1), gomock.Any()).
729 Return(&gitlab.SecureFile{ID: 1, Name: "file1.txt", Checksum: fileContentsChecksum}, nil, nil)
731 exec := cmdtest.SetupCmdForTest(t, NewCmdDownload, false,
732 cmdtest.WithGitLabClient(testClient.Client))
733 _, err := exec("--all --output-dir=" + destDir)
735 require.NoError(t, err)
736 got, err := os.ReadFile(filepath.Join(destDir, "file1.txt"))
737 require.NoError(t, err)
738 assert.Equal(t, fileContents, string(got))
739 })
741 // An absolute --output-dir removes the working-directory root that would
742 // otherwise reject a traversing name, so the name is checked directly.
743 t.Run("absolute --output-dir rejects a traversing server name", func(t *testing.T) {
744 t.Chdir(t.TempDir())
745 destDir := t.TempDir()
746 // The name below climbs two levels, so this is where it would land if
747 // --output-dir did not confine it.
748 outputDir := filepath.Join(destDir, "a", "b")
749 escaped := filepath.Join(destDir, "outside.txt")
751 testClient := gitlabtesting.NewTestClient(t)
752 // The name is rejected before anything is fetched, so no download or
753 // details call should be made.
754 testClient.MockSecureFiles.EXPECT().
755 ListProjectSecureFiles(repoName, gomock.Any(), gomock.Any(), gomock.Any()).
756 Return([]*gitlab.SecureFile{{ID: 1, Name: "../../outside.txt", Checksum: fileContentsChecksum}}, &gitlab.Response{}, nil)
758 exec := cmdtest.SetupCmdForTest(t, NewCmdDownload, false,
759 cmdtest.WithGitLabClient(testClient.Client))
760 _, err := exec("--all --output-dir=" + outputDir)
762 require.Error(t, err)
763 assert.Equal(t, "error downloading secure file '../../outside.txt' (ID: 1): name would write outside the output directory", err.Error())
764 assert.NoFileExists(t, escaped, "server-supplied name must not escape --output-dir")
765 assert.NoFileExists(t, filepath.Join(outputDir, "outside.txt"))
766 })
767}
Diagnostics8 git calls · 242 ms
CallStatusSizeTime0242 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 KiB9 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=UPDATING git.response.duration_ms=9 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=3 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 · hit5 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=5 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:02001.8 KiB · hit4 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=1857 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 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=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200353 B · 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=353 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=d9f8b91b7f75f6920fea2e149ed3fba5f61766da 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=353 git.pkt.payload.max=326 git.sideband.band1.bytes=326 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=326 git.pack.trailer=353875a8d3dc1310585685b9eadd95754bcf8d98Open pack map
fetchtree:0200141 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=141 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=e0642c2a3d3bc785eabed06d920f5c4643b09ab4 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=141 git.pkt.payload.max=114 git.sideband.band1.bytes=114 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=114 git.pack.trailer=cce222a9c5a43e5630585282913fb178ee6774f4Open pack map
fetch2003.6 KiB222 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=222 git.response.bytes=3638 git.response.cache=MISS git.response.duration_ms=222 git.fetch.wants=c5b8d8b142360ad052302b98d993bbccd3817c62 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=3638 git.pkt.payload.max=3611 git.sideband.band1.bytes=3611 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
- a5e31d1e6e558682
- Request
- f1297cc2…
- 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