(t *testing.T)
| 13 | ) |
| 14 | |
| 15 | func Test_extractZip(t *testing.T) { |
| 16 | tests := []struct { |
| 17 | name string |
| 18 | directories []string |
| 19 | entries []string |
| 20 | setup func(t *testing.T, extractPath, outside string) |
| 21 | wantExtract []string |
| 22 | wantDirs []string |
| 23 | wantErr string |
| 24 | }{ |
| 25 | { |
| 26 | name: "extracts nested file", |
| 27 | entries: []string{"src/main.go"}, |
| 28 | wantExtract: []string{"src/main.go"}, |
| 29 | }, |
| 30 | { |
| 31 | name: "skips lexical escape", |
| 32 | entries: []string{"../outside.txt"}, |
| 33 | }, |
| 34 | { |
| 35 | name: "skips escapes among safe entries", |
| 36 | entries: []string{ |
| 37 | "../outside-before.txt", |
| 38 | "safe-one.txt", |
| 39 | "../../outside-between.txt", |
| 40 | "nested/safe-two.txt", |
| 41 | }, |
| 42 | wantExtract: []string{"safe-one.txt", "nested/safe-two.txt"}, |
| 43 | }, |
| 44 | { |
| 45 | name: "extracts explicit directory", |
| 46 | directories: []string{"nested/"}, |
| 47 | wantDirs: []string{"nested"}, |
| 48 | }, |
| 49 | { |
| 50 | name: "explicit directory rejects planted link", |
| 51 | directories: []string{"nested/"}, |
| 52 | setup: func(t *testing.T, extractPath, outside string) { |
| 53 | t.Helper() |
| 54 | require.NoError(t, os.Symlink(outside, filepath.Join(extractPath, "nested"))) |
| 55 | }, |
| 56 | wantErr: "symbolic link", |
| 57 | }, |
| 58 | } |
| 59 | |
| 60 | for _, tt := range tests { |
| 61 | t.Run(tt.name, func(t *testing.T) { |
| 62 | var archive bytes.Buffer |
| 63 | writer := zip.NewWriter(&archive) |
| 64 | for _, name := range tt.directories { |
| 65 | header := &zip.FileHeader{Name: name} |
| 66 | header.SetMode(os.ModeDir | 0o755) |
| 67 | _, err := writer.CreateHeader(header) |
| 68 | require.NoError(t, err) |
| 69 | } |
| 70 | for _, name := range tt.entries { |
| 71 | file, err := writer.Create(name) |
| 72 | require.NoError(t, err) |
nothing calls this directly
no test coverage detected