Skip to content
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

fix: zip slip issues #381

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 8 additions & 2 deletions pkg/files/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package files
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
)

// untargz a tarball to a target, from
Expand Down Expand Up @@ -65,10 +67,10 @@ func UnTargzAll(tarball, target string) error {
defer zreader.Close()

reader, err := gzip.NewReader(zreader)
defer reader.Close()
if err != nil {
panic(err)
}
defer reader.Close()

tarReader := tar.NewReader(reader)

Expand All @@ -81,6 +83,10 @@ func UnTargzAll(tarball, target string) error {
}

p := filepath.Join(target, header.Name)
// Source: https://snyk.io/research/zip-slip-vulnerability
if !strings.HasPrefix(p, filepath.Clean(p)+ string(os.PathListSeparator)) {
return fmt.Errorf("illegal file path %s", p)
}
err = UnTarFile(header, p, tarReader)
if err != nil {
return err
Expand All @@ -100,7 +106,7 @@ func UnTarFile(header *tar.Header, target string, tarReader io.Reader) error {
}
// In a normal archive, directories are mentionned before their files
// But in an archive generated by helm, no directories are mentionned
if err := os.MkdirAll(path.Dir(target), 0755); err != nil {
if err := os.MkdirAll(path.Dir(target), 0o755); err != nil {
return err
}

Expand Down