|
| 1 | +package apiv1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/pkg/errors" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + blockSize = 1 << 13 // arbitrary |
| 16 | +) |
| 17 | + |
| 18 | +// Decompressor handle the load of |
| 19 | +type Decompressor struct { |
| 20 | + *Options |
| 21 | +} |
| 22 | + |
| 23 | +type Options struct { |
| 24 | + MaxSize int64 |
| 25 | + |
| 26 | + currSize int64 |
| 27 | +} |
| 28 | + |
| 29 | +// NewDecompressor constructs a fresh Decompressor. |
| 30 | +func NewDecompressor(opts *Options) *Decompressor { |
| 31 | + if opts == nil { |
| 32 | + opts = &Options{} |
| 33 | + } |
| 34 | + return &Decompressor{ |
| 35 | + Options: opts, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Unzip extracts the content of the zip reader into cd. |
| 40 | +// It returns the directory it extracted into for Pulumi to use, |
| 41 | +// or an error if anything unexpected happens. |
| 42 | +func (dec *Decompressor) Unzip(r *zip.Reader, cd string) (string, error) { |
| 43 | + outDir := "" |
| 44 | + for _, f := range r.File { |
| 45 | + if f.FileInfo().IsDir() { |
| 46 | + continue |
| 47 | + } |
| 48 | + filePath, err := sanitizeArchivePath(cd, f.Name) |
| 49 | + if err != nil { |
| 50 | + return cd, err |
| 51 | + } |
| 52 | + |
| 53 | + // Save output directory i.e. the directory containing the Pulumi.yaml file, |
| 54 | + // the scenario entrypoint. |
| 55 | + base := filepath.Base(filePath) |
| 56 | + if base == "Pulumi.yaml" || base == "Pulumi.yml" { |
| 57 | + if outDir != "" { |
| 58 | + return cd, errors.New("archive contain multiple Pulumi yaml/yml file, can't easily determine entrypoint") |
| 59 | + } |
| 60 | + outDir = filepath.Dir(filePath) |
| 61 | + } |
| 62 | + |
| 63 | + // If the file is in a sub-directory, create it |
| 64 | + dir := filepath.Dir(filePath) |
| 65 | + if _, err := os.Stat(dir); err != nil { |
| 66 | + if err := os.MkdirAll(dir, os.ModePerm); err != nil { |
| 67 | + return cd, err |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Create and write the file |
| 72 | + if err := dec.copyTo(f, filePath); err != nil { |
| 73 | + return cd, err |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return outDir, nil |
| 78 | +} |
| 79 | + |
| 80 | +func sanitizeArchivePath(d, t string) (v string, err error) { |
| 81 | + v = filepath.Join(d, t) |
| 82 | + if strings.HasPrefix(v, filepath.Clean(d)) { |
| 83 | + return v, nil |
| 84 | + } |
| 85 | + return "", &ErrPathTainted{ |
| 86 | + Path: t, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func (dec *Decompressor) copyTo(f *zip.File, filePath string) error { |
| 91 | + outFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, f.Mode()) |
| 92 | + if err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + defer outFile.Close() |
| 96 | + |
| 97 | + rc, err := f.Open() |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + defer rc.Close() |
| 102 | + |
| 103 | + for { |
| 104 | + n, err := io.CopyN(outFile, rc, blockSize) |
| 105 | + if err != nil { |
| 106 | + if err == io.EOF { |
| 107 | + return nil |
| 108 | + } |
| 109 | + return err |
| 110 | + } |
| 111 | + dec.currSize += n |
| 112 | + |
| 113 | + if dec.MaxSize > 0 && dec.currSize > dec.MaxSize { |
| 114 | + return ErrTooLargeContent{ |
| 115 | + MaxSize: dec.MaxSize, |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// ErrPathTainted is returned when a potential zip slip is detected |
| 122 | +// through an unzip. |
| 123 | +type ErrPathTainted struct { |
| 124 | + Path string |
| 125 | +} |
| 126 | + |
| 127 | +func (err ErrPathTainted) Error() string { |
| 128 | + return fmt.Sprintf("filepath is tainted: %s", err.Path) |
| 129 | +} |
| 130 | + |
| 131 | +var _ error = (*ErrPathTainted)(nil) |
| 132 | + |
| 133 | +// ErrTooLargeContent is returned when a too large zip is processed |
| 134 | +// (e.g. a zip bomb). |
| 135 | +type ErrTooLargeContent struct { |
| 136 | + MaxSize int64 |
| 137 | +} |
| 138 | + |
| 139 | +func (err ErrTooLargeContent) Error() string { |
| 140 | + return fmt.Sprintf("too large archive content, maximum is %d", err.MaxSize) |
| 141 | +} |
| 142 | + |
| 143 | +var _ error = (*ErrTooLargeContent)(nil) |
0 commit comments