Skip to content
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
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@

tr := tar.NewReader(imageStream)
for {
hdr, err := tr.Next()

Check failure

Code scanning / CodeQL

Arbitrary file access during archive extraction ("Zip Slip") High

Unsanitized archive entry, which may contain '..', is used in a
file system operation
.
Unsanitized archive entry, which may contain '..', is used in a
file system operation
.
Unsanitized archive entry, which may contain '..', is used in a file system operation.
if err == io.EOF {
break
}
Expand All @@ -271,7 +271,7 @@
os.MkdirAll(filepath.Join(outputDir, layerID), FilePerms)
ttr := tar.NewReader(tr)
for {
hdrr, err := ttr.Next()

Check failure

Code scanning / CodeQL

Arbitrary file access during archive extraction ("Zip Slip") High

Unsanitized archive entry, which may contain '..', is used in a
file system operation
.
Unsanitized archive entry, which may contain '..', is used in a
file system operation
.
if err == io.EOF {
break
}
Expand All @@ -279,13 +279,21 @@
color.Red("%s", err)
}
name := hdrr.Name
cleanPath := filepath.Clean(name)
absExtractDir := filepath.Join(outputDir, layerID)
finalPath := filepath.Join(absExtractDir, cleanPath)
// Ensure path is not absolute and stays within extract dir
if !strings.HasPrefix(finalPath, absExtractDir+string(os.PathSeparator)) && finalPath != absExtractDir {
color.Red("Skipping suspicious archive entry: %s", name)
continue
}
switch hdrr.Typeflag {
case tar.TypeDir:
os.MkdirAll(filepath.Join(outputDir, layerID, name), FilePerms)
os.MkdirAll(finalPath, FilePerms)
case tar.TypeReg:
data := make([]byte, hdrr.Size)
ttr.Read(data)
os.WriteFile(filepath.Join(outputDir, layerID, name), data, FilePerms)
os.WriteFile(finalPath, data, FilePerms)
}
}
}
Expand Down