|
| 1 | +// Copyright (c) 2025 Alibaba Group Holding Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package preprocess |
| 16 | + |
| 17 | +import ( |
| 18 | + "archive/tar" |
| 19 | + "compress/gzip" |
| 20 | + "io" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/alibaba/loongsuite-go-agent/tool/ex" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + unzippedPkgDir = "pkg" |
| 30 | +) |
| 31 | + |
| 32 | +func extractGZip(data []byte, targetDir string) error { |
| 33 | + err := os.MkdirAll(targetDir, 0755) |
| 34 | + if err != nil { |
| 35 | + return ex.Wrap(err) |
| 36 | + } |
| 37 | + |
| 38 | + gzReader, err := gzip.NewReader(strings.NewReader(string(data))) |
| 39 | + if err != nil { |
| 40 | + return ex.Wrap(err) |
| 41 | + } |
| 42 | + defer func() { |
| 43 | + err := gzReader.Close() |
| 44 | + if err != nil { |
| 45 | + ex.Fatal(err) |
| 46 | + } |
| 47 | + }() |
| 48 | + |
| 49 | + tarReader := tar.NewReader(gzReader) |
| 50 | + for { |
| 51 | + header, err := tarReader.Next() |
| 52 | + if err == io.EOF { |
| 53 | + break |
| 54 | + } |
| 55 | + if err != nil { |
| 56 | + return ex.Wrap(err) |
| 57 | + } |
| 58 | + |
| 59 | + // Skip AppleDouble files (._filename) and other hidden files |
| 60 | + if strings.HasPrefix(filepath.Base(header.Name), "._") || |
| 61 | + strings.HasPrefix(filepath.Base(header.Name), ".") { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + // Rename pkg_tmp to pkg in the path |
| 66 | + // Normalize path to Unix style for consistent string operations |
| 67 | + cleanName := filepath.ToSlash(filepath.Clean(header.Name)) |
| 68 | + if strings.HasPrefix(cleanName, "pkg_tmp/") { |
| 69 | + cleanName = strings.Replace(cleanName, "pkg_tmp/", "pkg/", 1) |
| 70 | + } else if cleanName == "pkg_tmp" { |
| 71 | + cleanName = unzippedPkgDir |
| 72 | + } |
| 73 | + |
| 74 | + // Sanitize the file path to prevent Zip Slip vulnerability |
| 75 | + if cleanName == "." || cleanName == ".." || |
| 76 | + strings.HasPrefix(cleanName, "..") { |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + // Ensure the resolved path is within the target directory |
| 81 | + targetPath := filepath.Join(targetDir, cleanName) |
| 82 | + resolvedPath, err := filepath.EvalSymlinks(targetPath) |
| 83 | + if err != nil { |
| 84 | + // If symlink evaluation fails, use the original path |
| 85 | + resolvedPath = targetPath |
| 86 | + } |
| 87 | + |
| 88 | + // Check if the resolved path is within the target directory |
| 89 | + relPath, err := filepath.Rel(targetDir, resolvedPath) |
| 90 | + if err != nil || strings.HasPrefix(relPath, "..") || |
| 91 | + filepath.IsAbs(relPath) { |
| 92 | + continue // Skip files that would be extracted outside target dir |
| 93 | + } |
| 94 | + switch header.Typeflag { |
| 95 | + case tar.TypeDir: |
| 96 | + err = os.MkdirAll(targetPath, os.FileMode(header.Mode)) |
| 97 | + if err != nil { |
| 98 | + return ex.Wrap(err) |
| 99 | + } |
| 100 | + |
| 101 | + case tar.TypeReg: |
| 102 | + file, err := os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR, |
| 103 | + os.FileMode(header.Mode)) |
| 104 | + if err != nil { |
| 105 | + return ex.Wrap(err) |
| 106 | + } |
| 107 | + |
| 108 | + _, err = io.Copy(file, tarReader) |
| 109 | + if err != nil { |
| 110 | + return ex.Wrap(err) |
| 111 | + } |
| 112 | + err = file.Close() |
| 113 | + if err != nil { |
| 114 | + return ex.Wrap(err) |
| 115 | + } |
| 116 | + |
| 117 | + default: |
| 118 | + return ex.Newf("unsupported file type: %c in %s", |
| 119 | + header.Typeflag, header.Name) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
0 commit comments