Skip to content

fix: Arbitrary file access during archive extraction a filepath.Join Path Traversal #6762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,33 @@ func CheckForMissingFiles(chartLocation string) error {
}

func ExtractTarGz(gzipStream io.Reader, chartDir string) error {
// Helper to safely join and validate tar entry names
safeJoin := func(destDir, filePath string) (string, error) {
cleanName := filepath.Clean(filePath)
// Prevent absolute paths
if filepath.IsAbs(cleanName) {
return "", fmt.Errorf("tar entry %q is absolute path", filePath)
}
// Prevent path traversal
if strings.Contains(cleanName, "..") {
return "", fmt.Errorf("tar entry %q contains parent directory traversal", filePath)
}
fullPath := filepath.Join(destDir, cleanName)
// Ensure the resulting path is within destDir
absDest, err := filepath.Abs(destDir)
if err != nil {
return "", err
}
absFull, err := filepath.Abs(fullPath)
if err != nil {
return "", err
}
if !strings.HasPrefix(absFull, absDest+string(os.PathSeparator)) && absFull != absDest {
return "", fmt.Errorf("tar entry %q escapes target directory", filePath)
}
return fullPath, nil
}

uncompressedStream, err := gzip.NewReader(gzipStream)
if err != nil {
return err
Expand All @@ -192,25 +219,35 @@ func ExtractTarGz(gzipStream io.Reader, chartDir string) error {
if err != nil {
return err
}

outPath, err := safeJoin(chartDir, header.Name)
if err != nil {
return err
}

switch header.Typeflag {
case tar.TypeDir:
if _, err := os.Stat(filepath.Join(chartDir, header.Name)); os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Join(chartDir, header.Name), 0755); err != nil {
if _, err := os.Stat(outPath); os.IsNotExist(err) {
if err := os.MkdirAll(outPath, 0755); err != nil {
return err
}
} else {
break
}

case tar.TypeReg:
outFile, err := os.Create(filepath.Join(chartDir, header.Name))
outFile, err := os.Create(outPath)
if err != nil {
dirName := filepath.Dir(header.Name)
if _, err1 := os.Stat(filepath.Join(chartDir, dirName)); os.IsNotExist(err1) {
if err1 = os.MkdirAll(filepath.Join(chartDir, dirName), 0755); err1 != nil {
dirPath, dirErr := safeJoin(chartDir, dirName)
if dirErr != nil {
return dirErr
}
if _, err1 := os.Stat(dirPath); os.IsNotExist(err1) {
if err1 = os.MkdirAll(dirPath, 0755); err1 != nil {
return err1
}
outFile, err = os.Create(filepath.Join(chartDir, header.Name))
outFile, err = os.Create(outPath)
if err != nil {
return err
}
Expand Down