|
1 | 1 | package archive |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "archive/tar" |
| 5 | + "archive/zip" |
| 6 | + "compress/gzip" |
4 | 7 | "os" |
5 | 8 | "path/filepath" |
| 9 | + "strings" |
6 | 10 | "testing" |
7 | 11 |
|
8 | 12 | "golang.org/x/sys/unix" |
@@ -64,3 +68,290 @@ func TestExtract(t *testing.T) { |
64 | 68 | }) |
65 | 69 | } |
66 | 70 | } |
| 71 | + |
| 72 | +// TestLinkTraversal tests that extracting archives with symlinks or hardlinks pointing |
| 73 | +// outside the destination directory fails and doesn't create any files outside the dest. |
| 74 | +func TestLinkTraversal(t *testing.T) { |
| 75 | + tmpDir := t.TempDir() |
| 76 | + |
| 77 | + // Create a nested destination two levels deep so we can detect escapes |
| 78 | + // Structure: tmpDir/nested/extracted/ |
| 79 | + nestedDir := filepath.Join(tmpDir, "nested") |
| 80 | + err := os.MkdirAll(nestedDir, 0750) |
| 81 | + assert.NoError(t, err) |
| 82 | + |
| 83 | + // Create the malicious tarball with both symlink and hardlink escapes |
| 84 | + maliciousTarball := filepath.Join(tmpDir, "malicious.tar.gz") |
| 85 | + f, err := os.Create(maliciousTarball) |
| 86 | + assert.NoError(t, err) |
| 87 | + |
| 88 | + gw := gzip.NewWriter(f) |
| 89 | + tw := tar.NewWriter(gw) |
| 90 | + |
| 91 | + // Add a regular file first |
| 92 | + err = tw.WriteHeader(&tar.Header{ |
| 93 | + Name: "safe_file.txt", |
| 94 | + Mode: 0644, |
| 95 | + Size: 12, |
| 96 | + }) |
| 97 | + assert.NoError(t, err) |
| 98 | + _, err = tw.Write([]byte("safe content")) |
| 99 | + assert.NoError(t, err) |
| 100 | + |
| 101 | + // Add a malicious symlink that points one directory up (escaping dest) |
| 102 | + err = tw.WriteHeader(&tar.Header{ |
| 103 | + Name: "evil_symlink", |
| 104 | + Mode: 0777, |
| 105 | + Typeflag: tar.TypeSymlink, |
| 106 | + Linkname: "../escape_marker", |
| 107 | + }) |
| 108 | + assert.NoError(t, err) |
| 109 | + |
| 110 | + // Add a malicious hardlink that points one directory up (escaping dest) |
| 111 | + err = tw.WriteHeader(&tar.Header{ |
| 112 | + Name: "evil_hardlink", |
| 113 | + Mode: 0644, |
| 114 | + Typeflag: tar.TypeLink, |
| 115 | + Linkname: "../escape_marker", |
| 116 | + }) |
| 117 | + assert.NoError(t, err) |
| 118 | + |
| 119 | + assert.NoError(t, tw.Close()) |
| 120 | + assert.NoError(t, gw.Close()) |
| 121 | + assert.NoError(t, f.Close()) |
| 122 | + |
| 123 | + // Try to extract the malicious tarball into nested/extracted |
| 124 | + p, _ := ui.NewForTesting() |
| 125 | + dest := filepath.Join(nestedDir, "extracted") |
| 126 | + |
| 127 | + _, err = Extract( |
| 128 | + p.Task("extract"), |
| 129 | + maliciousTarball, |
| 130 | + &manifest.Package{Dest: dest, Source: "malicious.tar.gz"}, |
| 131 | + ) |
| 132 | + |
| 133 | + // Extraction should fail with an error about illegal link path |
| 134 | + assert.Error(t, err) |
| 135 | + assert.True(t, strings.Contains(err.Error(), "illegal") || strings.Contains(err.Error(), "symlink"), |
| 136 | + "expected error about illegal link path, got: %v", err) |
| 137 | + |
| 138 | + // Walk the entire tmpDir to verify nothing escaped |
| 139 | + // Only the tarball and nested directory should exist at tmpDir level |
| 140 | + err = filepath.Walk(tmpDir, func(path string, info os.FileInfo, err error) error { |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + // Get relative path from tmpDir |
| 145 | + relPath, err := filepath.Rel(tmpDir, path) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + // Skip the root |
| 150 | + if relPath == "." { |
| 151 | + return nil |
| 152 | + } |
| 153 | + // Allow only: malicious.tar.gz, nested/, nested/extracted/ |
| 154 | + // Nothing should exist in nested/ besides extracted/ (and its contents) |
| 155 | + allowedPrefixes := []string{"malicious.tar.gz", "nested"} |
| 156 | + allowed := false |
| 157 | + for _, prefix := range allowedPrefixes { |
| 158 | + if strings.HasPrefix(relPath, prefix) { |
| 159 | + allowed = true |
| 160 | + break |
| 161 | + } |
| 162 | + } |
| 163 | + assert.True(t, allowed, "unexpected file outside extraction directory: %s", path) |
| 164 | + // Specifically check that no "escape_marker" file was created |
| 165 | + assert.False(t, strings.Contains(relPath, "escape_marker"), "symlink/hardlink escape detected: %s", path) |
| 166 | + return nil |
| 167 | + }) |
| 168 | + assert.NoError(t, err) |
| 169 | +} |
| 170 | + |
| 171 | +// TestLinkTraversalWithStrip tests that symlinks don't escape when strip is applied. |
| 172 | +// Archive contains: foo/bar -> ../waz and foo/waz |
| 173 | +// With strip=1, this becomes: bar -> ../waz which would escape if not handled properly. |
| 174 | +func TestLinkTraversalWithStrip(t *testing.T) { |
| 175 | + tmpDir := t.TempDir() |
| 176 | + |
| 177 | + // Create a nested destination two levels deep so we can detect escapes |
| 178 | + nestedDir := filepath.Join(tmpDir, "nested") |
| 179 | + err := os.MkdirAll(nestedDir, 0750) |
| 180 | + assert.NoError(t, err) |
| 181 | + |
| 182 | + // Create tarball with internal symlink that escapes after stripping |
| 183 | + tarball := filepath.Join(tmpDir, "strip_escape.tar.gz") |
| 184 | + f, err := os.Create(tarball) |
| 185 | + assert.NoError(t, err) |
| 186 | + |
| 187 | + gw := gzip.NewWriter(f) |
| 188 | + tw := tar.NewWriter(gw) |
| 189 | + |
| 190 | + // Add foo/ directory |
| 191 | + err = tw.WriteHeader(&tar.Header{ |
| 192 | + Name: "foo/", |
| 193 | + Mode: 0755, |
| 194 | + Typeflag: tar.TypeDir, |
| 195 | + }) |
| 196 | + assert.NoError(t, err) |
| 197 | + |
| 198 | + // Add foo/waz file (the symlink target) |
| 199 | + err = tw.WriteHeader(&tar.Header{ |
| 200 | + Name: "foo/waz", |
| 201 | + Mode: 0644, |
| 202 | + Size: 11, |
| 203 | + }) |
| 204 | + assert.NoError(t, err) |
| 205 | + _, err = tw.Write([]byte("waz content")) |
| 206 | + assert.NoError(t, err) |
| 207 | + |
| 208 | + // Add foo/bar -> ../waz symlink |
| 209 | + // After strip=1, this becomes bar -> ../waz which escapes! |
| 210 | + err = tw.WriteHeader(&tar.Header{ |
| 211 | + Name: "foo/bar", |
| 212 | + Mode: 0777, |
| 213 | + Typeflag: tar.TypeSymlink, |
| 214 | + Linkname: "../waz", |
| 215 | + }) |
| 216 | + assert.NoError(t, err) |
| 217 | + |
| 218 | + assert.NoError(t, tw.Close()) |
| 219 | + assert.NoError(t, gw.Close()) |
| 220 | + assert.NoError(t, f.Close()) |
| 221 | + |
| 222 | + // Try to extract with strip=1 into nested/extracted |
| 223 | + p, _ := ui.NewForTesting() |
| 224 | + dest := filepath.Join(nestedDir, "extracted") |
| 225 | + |
| 226 | + _, err = Extract( |
| 227 | + p.Task("extract"), |
| 228 | + tarball, |
| 229 | + &manifest.Package{Dest: dest, Source: "strip_escape.tar.gz", Strip: 1}, |
| 230 | + ) |
| 231 | + |
| 232 | + // Extraction should fail because the symlink escapes after stripping |
| 233 | + assert.Error(t, err) |
| 234 | + assert.True(t, strings.Contains(err.Error(), "illegal") || strings.Contains(err.Error(), "symlink"), |
| 235 | + "expected error about illegal link path, got: %v", err) |
| 236 | + |
| 237 | + // Walk to verify nothing escaped |
| 238 | + err = filepath.Walk(tmpDir, func(path string, info os.FileInfo, err error) error { |
| 239 | + if err != nil { |
| 240 | + return err |
| 241 | + } |
| 242 | + relPath, err := filepath.Rel(tmpDir, path) |
| 243 | + if err != nil { |
| 244 | + return err |
| 245 | + } |
| 246 | + if relPath == "." { |
| 247 | + return nil |
| 248 | + } |
| 249 | + // Should not find "waz" outside of nested/extracted |
| 250 | + if relPath == "waz" || relPath == "nested/waz" { |
| 251 | + t.Errorf("symlink escape detected: %s", path) |
| 252 | + } |
| 253 | + return nil |
| 254 | + }) |
| 255 | + assert.NoError(t, err) |
| 256 | +} |
| 257 | + |
| 258 | +// TestZipInternalSymlink tests that relative symlinks pointing to sibling directories |
| 259 | +// within the archive are allowed. This is the pattern used by packages like bats-core |
| 260 | +// which contain test fixtures with symlinks like ../recursive/subsuite. |
| 261 | +func TestZipInternalSymlink(t *testing.T) { |
| 262 | + tmpDir := t.TempDir() |
| 263 | + |
| 264 | + zipPath := filepath.Join(tmpDir, "internal_symlink.zip") |
| 265 | + f, err := os.Create(zipPath) |
| 266 | + assert.NoError(t, err) |
| 267 | + |
| 268 | + zw := zip.NewWriter(f) |
| 269 | + |
| 270 | + // Add directory: suite/recursive/ |
| 271 | + _, err = zw.Create("suite/recursive/") |
| 272 | + assert.NoError(t, err) |
| 273 | + |
| 274 | + // Add file: suite/recursive/test.bats |
| 275 | + w, err := zw.Create("suite/recursive/test.bats") |
| 276 | + assert.NoError(t, err) |
| 277 | + _, err = w.Write([]byte("test content")) |
| 278 | + assert.NoError(t, err) |
| 279 | + |
| 280 | + // Add directory: suite/recursive/subsuite/ |
| 281 | + _, err = zw.Create("suite/recursive/subsuite/") |
| 282 | + assert.NoError(t, err) |
| 283 | + |
| 284 | + // Add file: suite/recursive/subsuite/sub.bats |
| 285 | + w, err = zw.Create("suite/recursive/subsuite/sub.bats") |
| 286 | + assert.NoError(t, err) |
| 287 | + _, err = w.Write([]byte("sub content")) |
| 288 | + assert.NoError(t, err) |
| 289 | + |
| 290 | + // Add directory: suite/recursive_with_symlinks/ |
| 291 | + _, err = zw.Create("suite/recursive_with_symlinks/") |
| 292 | + assert.NoError(t, err) |
| 293 | + |
| 294 | + // Add symlink: suite/recursive_with_symlinks/subsuite -> ../recursive/subsuite |
| 295 | + header := &zip.FileHeader{ |
| 296 | + Name: "suite/recursive_with_symlinks/subsuite", |
| 297 | + } |
| 298 | + header.SetMode(os.ModeSymlink | 0777) |
| 299 | + w, err = zw.CreateHeader(header) |
| 300 | + assert.NoError(t, err) |
| 301 | + _, err = w.Write([]byte("../recursive/subsuite")) |
| 302 | + assert.NoError(t, err) |
| 303 | + |
| 304 | + assert.NoError(t, zw.Close()) |
| 305 | + assert.NoError(t, f.Close()) |
| 306 | + |
| 307 | + p, _ := ui.NewForTesting() |
| 308 | + dest := filepath.Join(tmpDir, "extracted") |
| 309 | + |
| 310 | + _, err = Extract( |
| 311 | + p.Task("extract"), |
| 312 | + zipPath, |
| 313 | + &manifest.Package{Dest: dest, Source: "internal_symlink.zip"}, |
| 314 | + ) |
| 315 | + assert.NoError(t, err, "internal symlinks within the archive should be allowed") |
| 316 | + |
| 317 | + // Verify the symlink was created and points to the right target |
| 318 | + target, err := os.Readlink(filepath.Join(dest, "suite", "recursive_with_symlinks", "subsuite")) |
| 319 | + assert.NoError(t, err) |
| 320 | + assert.Equal(t, "../recursive/subsuite", target) |
| 321 | +} |
| 322 | + |
| 323 | +// TestZipEscapingSymlink tests that symlinks in zip archives that escape |
| 324 | +// the extraction root are rejected. |
| 325 | +func TestZipEscapingSymlink(t *testing.T) { |
| 326 | + tmpDir := t.TempDir() |
| 327 | + |
| 328 | + zipPath := filepath.Join(tmpDir, "escaping_symlink.zip") |
| 329 | + f, err := os.Create(zipPath) |
| 330 | + assert.NoError(t, err) |
| 331 | + |
| 332 | + zw := zip.NewWriter(f) |
| 333 | + |
| 334 | + header := &zip.FileHeader{ |
| 335 | + Name: "evil", |
| 336 | + } |
| 337 | + header.SetMode(os.ModeSymlink | 0777) |
| 338 | + w, err := zw.CreateHeader(header) |
| 339 | + assert.NoError(t, err) |
| 340 | + _, err = w.Write([]byte("../../etc/passwd")) |
| 341 | + assert.NoError(t, err) |
| 342 | + |
| 343 | + assert.NoError(t, zw.Close()) |
| 344 | + assert.NoError(t, f.Close()) |
| 345 | + |
| 346 | + p, _ := ui.NewForTesting() |
| 347 | + dest := filepath.Join(tmpDir, "extracted") |
| 348 | + |
| 349 | + _, err = Extract( |
| 350 | + p.Task("extract"), |
| 351 | + zipPath, |
| 352 | + &manifest.Package{Dest: dest, Source: "escaping_symlink.zip"}, |
| 353 | + ) |
| 354 | + assert.Error(t, err) |
| 355 | + assert.True(t, strings.Contains(err.Error(), "illegal symlink target"), |
| 356 | + "expected error about illegal symlink target, got: %v", err) |
| 357 | +} |
0 commit comments