Merge pull request #1238 from samcharles93/security/prsource-zipslip

fix(prsource): guard archive extraction with a CodeQL-recognised containment check
This commit is contained in:
sam authored and GitHub committed 2026-09-30 03:38:22 +10:00
commit 8124c5af54
2 files changed
+48 -5

No files matched your search

+18 -5
View File
@@ -28,13 +28,18 @@ func extractTarGz(r io.Reader, destDir string) error {
}
defer gz.Close()
dest, err := filepath.Abs(destDir)
if err != nil {
return fmt.Errorf("prsource: resolve destination directory: %w", err)
}
tr := tar.NewReader(gz)
prefix, entries, err := planExtraction(tr)
if err != nil {
return err
}
for _, e := range entries {
if err := writeEntry(destDir, prefix, e); err != nil {
if err := writeEntry(dest, prefix, e); err != nil {
return err
}
}
@@ -96,8 +101,9 @@ func planExtraction(tr *tar.Reader) (prefix string, entries []tarEntry, err erro
}
// writeEntry writes one archive entry under destDir, with prefix (if any)
// stripped from its name. Directories, regular files, and nothing else are
// written: a symlink or device entry from an untrusted PR archive is
// stripped from its name. destDir is the absolute destination directory
// resolved by extractTarGz. Directories, regular files, and nothing else
// are written: a symlink or device entry from an untrusted PR archive is
// skipped rather than followed.
func writeEntry(destDir, prefix string, e tarEntry) error {
name := path.Clean(filepath.ToSlash(e.header.Name))
@@ -118,9 +124,16 @@ func writeEntry(destDir, prefix string, e tarEntry) error {
return nil
}
// Refuse a name that is itself absolute, and one that escapes destDir
// through "..". The escape is checked on the joined target rather than
// on a relative path derived from it: filepath.Join folds a leading
// ".." away, so only the joined result catches a name like "../x".
if path.IsAbs(name) {
return fmt.Errorf("prsource: archive entry %q escapes the destination directory", e.header.Name)
}
target := filepath.Join(destDir, filepath.FromSlash(name))
rel, err := filepath.Rel(destDir, target)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
destRoot := strings.TrimSuffix(destDir, string(filepath.Separator)) + string(filepath.Separator)
if !strings.HasPrefix(target, destRoot) {
return fmt.Errorf("prsource: archive entry %q escapes the destination directory", e.header.Name)
}
@@ -96,6 +96,36 @@ func TestExtractTarGzRefusesPathTraversal(t *testing.T) {
}
}
func TestExtractTarGzRefusesAnAbsoluteEntryPath(t *testing.T) {
data := buildTarGz(t, "", map[string]string{"/etc/passwd-clone": "evil"})
dest := t.TempDir()
if err := extractTarGz(bytes.NewReader(data), dest); err == nil {
t.Fatal("want an error for an absolute archive entry, not a silent write")
}
}
func TestExtractTarGzWritesNothingOutsideDestWhenAnEntryEscapes(t *testing.T) {
base := t.TempDir()
dest := filepath.Join(base, "snapshot")
if err := os.MkdirAll(dest, 0o755); err != nil {
t.Fatal(err)
}
// Two entries with different top-level components keep the wrapper
// prefix empty, so the leading ".." is preserved rather than stripped.
data := buildTarGz(t, "", map[string]string{
"../escaped.txt": "evil",
"keep.txt": "ok",
})
if err := extractTarGz(bytes.NewReader(data), dest); err == nil {
t.Fatal("want an error for an entry that escapes destDir")
}
if _, err := os.Stat(filepath.Join(base, "escaped.txt")); err == nil {
t.Fatal("an escaping entry must not be written outside destDir")
}
}
func TestExtractTarGzLeavesNoDotGit(t *testing.T) {
data := buildTarGz(t, "wrapper", map[string]string{
"a.go": "package a\n",