diff --git a/Client/io/io.go b/Client/io/io.go index 681567a..01e4ca5 100644 --- a/Client/io/io.go +++ b/Client/io/io.go @@ -74,15 +74,29 @@ func (fc *IO) CopyDirToZip(src string, dstZipPath string) error { } // Unzip extracts a zip file's contents into a specified destination directory. -func (fc *IO) Unzip(srcZipPath, dstDir string) error { +func (fc *IO) Unzip(srcZipPath, dstDir string, ignoredPaths ...string) error { r, err := zip.OpenReader(srcZipPath) if err != nil { return err } defer r.Close() + ignoredSet := make(map[string]struct{}) + for _, ignoredPath := range ignoredPaths { + ignoredSet[filepath.Clean(ignoredPath)] = struct{}{} + } + for _, file := range r.File { fPath := filepath.Join(dstDir, file.Name) + relativePath, err := filepath.Rel(dstDir, fPath) + if err != nil { + return err + } + + // Ignore specified paths + if _, found := ignoredSet[relativePath]; found { + continue + } // Check for ZipSlip (Directory traversal) if !strings.HasPrefix(fPath, filepath.Clean(dstDir)+string(os.PathSeparator)) { @@ -100,6 +114,7 @@ func (fc *IO) Unzip(srcZipPath, dstDir string) error { return err } + fmt.Println("extracting :"+fPath) outFile, err := os.OpenFile(fPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode()) if err != nil { return err @@ -125,10 +140,5 @@ func (fc *IO) Unzip(srcZipPath, dstDir string) error { } } - // Once all files have been extracted, delete the source zip file - if err := os.Remove(srcZipPath); err != nil { - return fmt.Errorf("failed to delete the source zip file: %s, error: %w", srcZipPath, err) - } - return nil } \ No newline at end of file diff --git a/Client/kaniko/cmd.go b/Client/kaniko/cmd.go index d5a4710..5b49b94 100644 --- a/Client/kaniko/cmd.go +++ b/Client/kaniko/cmd.go @@ -48,6 +48,7 @@ func (kd *KanikoDocker) ParseDockerImageTag(imageTag string) (shared.DockerImage func (kd *KanikoDocker) BuildImage(options shared.BuildOptions, contextPath string, dockerfilePath string) { fileHandler := io.New() + fmt.Println("Copying root") fileHandler.CopyDirToZip("/", "/kaniko/root.zip") stages := []string{} if kanikoExecutor, ok := kd.Executor.(*KanikoExecutor); ok { @@ -94,7 +95,8 @@ func (kd *KanikoDocker) BuildImage(options shared.BuildOptions, contextPath stri } else { fmt.Println("Executor is not of type *KanikoExecutor and does not have a Context field.") } - fileHandler.Unzip("/kaniko/root.zip","/") + fmt.Println("Replacing root") + fileHandler.Unzip("/kaniko/root.zip","/","/kaniko","/azp") fmt.Println("Kaniko build complete.") }