|
| 1 | +package archive |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/chainguard-dev/malcontent/pkg/malcontent" |
| 11 | +) |
| 12 | + |
| 13 | +// FuzzExtractTar tests tar extraction with random inputs to find crashes, |
| 14 | +// path traversal vulnerabilities, and other issues. |
| 15 | +func FuzzExtractTar(f *testing.F) { |
| 16 | + testdata := []string{ |
| 17 | + "../../pkg/action/testdata/apko.tar.gz", |
| 18 | + "../../pkg/action/testdata/apko_nested.tar.gz", |
| 19 | + } |
| 20 | + |
| 21 | + for _, td := range testdata { |
| 22 | + if data, err := os.ReadFile(td); err == nil { |
| 23 | + f.Add(data) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + f.Add([]byte{}) // empty file |
| 28 | + f.Add([]byte("not a tar file")) |
| 29 | + f.Add([]byte{0x1f, 0x8b, 0x08, 0x00}) // gzip magic bytes only |
| 30 | + |
| 31 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 32 | + tmpFile, err := os.CreateTemp("", "fuzz-tar-*.tar.gz") |
| 33 | + if err != nil { |
| 34 | + t.Skip("failed to create temp file") |
| 35 | + } |
| 36 | + defer os.Remove(tmpFile.Name()) |
| 37 | + |
| 38 | + if _, err := tmpFile.Write(data); err != nil { |
| 39 | + t.Skip("failed to write to temp file") |
| 40 | + } |
| 41 | + tmpFile.Close() |
| 42 | + |
| 43 | + tmpDir, err := os.MkdirTemp("", "fuzz-extract-*") |
| 44 | + if err != nil { |
| 45 | + t.Skip("failed to create temp dir") |
| 46 | + } |
| 47 | + defer os.RemoveAll(tmpDir) |
| 48 | + |
| 49 | + ctx := context.Background() |
| 50 | + _ = ExtractTar(ctx, tmpDir, tmpFile.Name()) |
| 51 | + |
| 52 | + err = filepath.WalkDir(tmpDir, func(path string, _ os.DirEntry, err error) error { |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + if !IsValidPath(path, tmpDir) { |
| 57 | + t.Fatalf("path traversal detected: %s is outside %s", path, tmpDir) |
| 58 | + } |
| 59 | + return nil |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + return |
| 63 | + } |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +// FuzzExtractZip tests zip extraction with random inputs. |
| 68 | +func FuzzExtractZip(f *testing.F) { |
| 69 | + testdata := []string{ |
| 70 | + "../../pkg/action/testdata/apko.zip", |
| 71 | + "../../pkg/action/testdata/conflict.zip", |
| 72 | + "../../pkg/action/testdata/17419.zip", |
| 73 | + } |
| 74 | + |
| 75 | + for _, td := range testdata { |
| 76 | + if data, err := os.ReadFile(td); err == nil { |
| 77 | + f.Add(data) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + f.Add([]byte{}) // empty file |
| 82 | + f.Add([]byte("PK")) // zip magic bytes only |
| 83 | + f.Add([]byte{0x50, 0x4b, 0x03, 0x04}) // full zip signature |
| 84 | + |
| 85 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 86 | + tmpFile, err := os.CreateTemp("", "fuzz-zip-*.zip") |
| 87 | + if err != nil { |
| 88 | + t.Skip("failed to create temp file") |
| 89 | + } |
| 90 | + defer os.Remove(tmpFile.Name()) |
| 91 | + |
| 92 | + if _, err := tmpFile.Write(data); err != nil { |
| 93 | + t.Skip("failed to write to temp file") |
| 94 | + } |
| 95 | + tmpFile.Close() |
| 96 | + |
| 97 | + tmpDir, err := os.MkdirTemp("", "fuzz-extract-*") |
| 98 | + if err != nil { |
| 99 | + t.Skip("failed to create temp dir") |
| 100 | + } |
| 101 | + defer os.RemoveAll(tmpDir) |
| 102 | + |
| 103 | + ctx := context.Background() |
| 104 | + _ = ExtractZip(ctx, tmpDir, tmpFile.Name()) |
| 105 | + |
| 106 | + err = filepath.WalkDir(tmpDir, func(path string, _ os.DirEntry, err error) error { |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + if !IsValidPath(path, tmpDir) { |
| 111 | + t.Fatalf("path traversal detected: %s is outside %s", path, tmpDir) |
| 112 | + } |
| 113 | + return nil |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + return |
| 117 | + } |
| 118 | + }) |
| 119 | +} |
| 120 | + |
| 121 | +// FuzzExtractArchive tests archive extraction via the top-level ExtractArchiveToTempDir |
| 122 | +// function which handles initialization properly. |
| 123 | +func FuzzExtractArchive(f *testing.F) { |
| 124 | + testdata := []string{ |
| 125 | + "../../pkg/action/testdata/apko.tar.gz", |
| 126 | + "../../pkg/action/testdata/apko_nested.tar.gz", |
| 127 | + "../../pkg/action/testdata/apko.zip", |
| 128 | + "../../pkg/action/testdata/apko.gz", |
| 129 | + } |
| 130 | + |
| 131 | + for _, td := range testdata { |
| 132 | + if data, err := os.ReadFile(td); err == nil { |
| 133 | + switch { |
| 134 | + case strings.HasSuffix(td, ".tar.gz"): |
| 135 | + f.Add(data, ".tar.gz") |
| 136 | + case strings.HasSuffix(td, ".zip"): |
| 137 | + f.Add(data, ".zip") |
| 138 | + case strings.HasSuffix(td, ".gz"): |
| 139 | + f.Add(data, ".gz") |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + f.Add([]byte{}, ".tar.gz") // empty file |
| 145 | + f.Add([]byte("not a tar file"), ".tar.gz") // invalid content |
| 146 | + f.Add([]byte{0x1f, 0x8b, 0x08, 0x00}, ".tar.gz") // gzip magic bytes only |
| 147 | + f.Add([]byte{0x50, 0x4b, 0x03, 0x04}, ".zip") // zip magic bytes |
| 148 | + f.Add([]byte{0x1f, 0x8b, 0x08, 0x00}, ".gz") // gzip header |
| 149 | + |
| 150 | + f.Fuzz(func(t *testing.T, data []byte, ext string) { |
| 151 | + if ext != ".tar.gz" && ext != ".zip" && ext != ".gz" && ext != ".tar" { |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + tmpFile, err := os.CreateTemp("", "fuzz-archive-*"+ext) |
| 156 | + if err != nil { |
| 157 | + t.Skip("failed to create temp file") |
| 158 | + } |
| 159 | + defer os.Remove(tmpFile.Name()) |
| 160 | + |
| 161 | + if _, err := tmpFile.Write(data); err != nil { |
| 162 | + t.Skip("failed to write to temp file") |
| 163 | + } |
| 164 | + tmpFile.Close() |
| 165 | + |
| 166 | + ctx := context.Background() |
| 167 | + cfg := malcontent.Config{} |
| 168 | + extractedDir, err := ExtractArchiveToTempDir(ctx, cfg, tmpFile.Name()) |
| 169 | + if err == nil && extractedDir != "" { |
| 170 | + defer os.RemoveAll(extractedDir) |
| 171 | + |
| 172 | + walkErr := filepath.WalkDir(extractedDir, func(path string, _ os.DirEntry, err error) error { |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + if !IsValidPath(path, extractedDir) { |
| 177 | + t.Fatalf("path traversal detected: %s is outside %s", path, extractedDir) |
| 178 | + } |
| 179 | + return nil |
| 180 | + }) |
| 181 | + if walkErr != nil { |
| 182 | + return |
| 183 | + } |
| 184 | + } |
| 185 | + }) |
| 186 | +} |
| 187 | + |
| 188 | +// FuzzIsValidPath tests path validation via the IsValidPath function. |
| 189 | +func FuzzIsValidPath(f *testing.F) { |
| 190 | + tmpDir, err := os.MkdirTemp("", "fuzz-path-") |
| 191 | + if err != nil { |
| 192 | + f.Fatal(err) |
| 193 | + } |
| 194 | + defer os.RemoveAll(tmpDir) |
| 195 | + |
| 196 | + f.Add(tmpDir, filepath.Join(tmpDir, "safe.txt")) |
| 197 | + f.Add(tmpDir, filepath.Join(tmpDir, "..", "etc", "passwd")) |
| 198 | + f.Add(tmpDir, "/etc/passwd") |
| 199 | + f.Add(tmpDir, filepath.Join(tmpDir, ".", ".", "safe.txt")) |
| 200 | + f.Add(tmpDir, filepath.Join(tmpDir, "subdir", "..", "..", "etc", "passwd")) |
| 201 | + f.Add(tmpDir, filepath.Join(tmpDir, "deeply", "nested", "path", "file.txt")) |
| 202 | + |
| 203 | + f.Fuzz(func(t *testing.T, baseDir, targetPath string) { |
| 204 | + if len(baseDir) < 3 { |
| 205 | + return |
| 206 | + } |
| 207 | + |
| 208 | + result := IsValidPath(targetPath, baseDir) |
| 209 | + |
| 210 | + if result { |
| 211 | + cleanTarget := filepath.Clean(targetPath) |
| 212 | + cleanBase := filepath.Clean(baseDir) |
| 213 | + |
| 214 | + if cleanBase == "" || cleanTarget == "" { |
| 215 | + return |
| 216 | + } |
| 217 | + |
| 218 | + if filepath.IsAbs(cleanTarget) && filepath.IsAbs(cleanBase) { |
| 219 | + rel, err := filepath.Rel(cleanBase, cleanTarget) |
| 220 | + if err == nil && (rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator))) { |
| 221 | + t.Fatalf("IsValidPath returned true but path %q escapes base %q (rel=%q)", targetPath, baseDir, rel) |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + }) |
| 226 | +} |
0 commit comments