2023-05-02 13:49:51 +02:00
|
|
|
package archive
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-03-17 15:43:27 +02:00
|
|
|
// Extract extracts the zip archive at "src" to "dest".
|
|
|
|
//
|
|
|
|
// Note that only dirs and regular files will be extracted.
|
|
|
|
// Symbolic links, named pipes, sockets, or any other irregular files
|
|
|
|
// are skipped because they come with too many edge cases and ambiguities.
|
2023-05-02 13:49:51 +02:00
|
|
|
func Extract(src, dest string) error {
|
|
|
|
zr, err := zip.OpenReader(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer zr.Close()
|
|
|
|
|
|
|
|
// normalize dest path to check later for Zip Slip
|
|
|
|
dest = filepath.Clean(dest) + string(os.PathSeparator)
|
|
|
|
|
|
|
|
for _, f := range zr.File {
|
|
|
|
err := extractFile(f, dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractFile extracts the provided zipFile into "basePath/zipFileName" path,
|
|
|
|
// creating all the necessary path directories.
|
|
|
|
func extractFile(zipFile *zip.File, basePath string) error {
|
|
|
|
path := filepath.Join(basePath, zipFile.Name)
|
|
|
|
|
|
|
|
// check for Zip Slip
|
|
|
|
if !strings.HasPrefix(path, basePath) {
|
|
|
|
return fmt.Errorf("invalid file path: %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := zipFile.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
|
2024-03-17 15:43:27 +02:00
|
|
|
// allow only dirs or regular files
|
2023-05-02 13:49:51 +02:00
|
|
|
if zipFile.FileInfo().IsDir() {
|
|
|
|
if err := os.MkdirAll(path, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-17 15:43:27 +02:00
|
|
|
} else if zipFile.FileInfo().Mode().IsRegular() {
|
2023-05-02 13:49:51 +02:00
|
|
|
// ensure that the file path directories are created
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, zipFile.Mode())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(f, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|