Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace stereoscope with using go-containerregistry directly #836

Merged
merged 27 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1f06426
PoC of image scanning complete
another-rex Feb 23, 2024
b634879
fix lints
another-rex Feb 27, 2024
1bca578
Efficiently load image
another-rex Feb 28, 2024
89bb36a
remove-profile
another-rex Mar 4, 2024
d1260b7
Add additional comments
another-rex Mar 4, 2024
da8fa15
test
another-rex Mar 4, 2024
9beeea3
Merge remote-tracking branch 'upstream/main' into image-branch
another-rex Mar 4, 2024
2716149
Fix test, prevent decompression bomb
another-rex Mar 4, 2024
212d0a0
Update snapshot and address PR comments
another-rex Mar 4, 2024
62a5e4b
Fix windows test
another-rex Mar 4, 2024
6a29430
Fix zip slip
another-rex Mar 4, 2024
a642f96
Fix trim prefix
another-rex Mar 4, 2024
455d7f4
Fix error logging
another-rex Mar 4, 2024
cee2693
Fix wrong error logging
another-rex Mar 4, 2024
607e262
Add additional close calls
another-rex Mar 5, 2024
c8a956d
Merge remote-tracking branch 'upstream/main' into push-xnmmrmklymzv
another-rex Mar 5, 2024
fd8d8e7
Everything world writable
another-rex Mar 5, 2024
a87a223
clean file paths
another-rex Mar 5, 2024
478f263
Revert mode change
another-rex Mar 5, 2024
2152af7
Refactor to check if extractor exists before opening
another-rex Mar 5, 2024
9370f6e
Always close file even on error, and save permission in filenode rath…
another-rex Mar 5, 2024
faa6aa7
Minor refactoring into separate files, add some more comments, fix pe…
another-rex Mar 5, 2024
224879a
Fix errors
another-rex Mar 6, 2024
2bec30f
Merge branch 'main' into push-xnmmrmklymzv
another-rex Mar 6, 2024
48acfdd
Hide flag
another-rex Mar 6, 2024
9fc27a9
Merge branch 'main' into push-xnmmrmklymzv
another-rex Mar 6, 2024
24c1a97
Fix lints
another-rex Mar 6, 2024
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
Prev Previous commit
Next Next commit
Fix test, prevent decompression bomb
  • Loading branch information
another-rex committed Mar 4, 2024
commit 271614901ee07c322ed3ab2edeb714deff22a921
3 changes: 2 additions & 1 deletion internal/image/filemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func (filemap *FileMap) OpenFile(path string) (fs.File, error) {

func (filemap *FileMap) AllFiles() []FileNode {
allFiles := []FileNode{}
filemap.fileNodeTrie.Walk(func(key string, value interface{}) error {
// No need to check error since we are not returning any errors
_ = filemap.fileNodeTrie.Walk(func(key string, value interface{}) error {
allFiles = append(allFiles, value.(FileNode))
return nil
})
Expand Down
25 changes: 16 additions & 9 deletions internal/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

const whiteoutPrefix = ".wh."

// 2 GB
const fileReadLimit = 2 * 1 << (10 * 3)

type ScanResults struct {
Lockfiles []lockfile.Lockfile
ImagePath string
Expand Down Expand Up @@ -127,7 +130,7 @@
tarReader := tar.NewReader(layerReader)

for {
header, err := tarReader.Next()
github-advanced-security[bot] marked this conversation as resolved.
Dismissed
Show resolved Hide resolved
if errors.Is(err, io.EOF) {
break
}
Expand All @@ -136,14 +139,14 @@
}
// Some tools prepend everything with "./", so if we don't Clean the
// name, we may have duplicate entries, which angers tar-split.
header.Name = filepath.Clean(header.Name)
cleanedFileName := filepath.Clean(header.Name)
oliverchang marked this conversation as resolved.
Show resolved Hide resolved
// force PAX format to remove Name/Linkname length limit of 100 characters
// required by USTAR and to not depend on internal tar package guess which
// prefers USTAR over PAX
header.Format = tar.FormatPAX

basename := filepath.Base(header.Name)
dirname := filepath.Dir(header.Name)
basename := filepath.Base(cleanedFileName)
dirname := filepath.Dir(cleanedFileName)
tombstone := strings.HasPrefix(basename, whiteoutPrefix)
if tombstone { // TODO: Handle Opaque Whiteouts
basename = basename[len(whiteoutPrefix):]
Expand All @@ -153,15 +156,15 @@
// if we're checking a directory, don't filepath.Join names
var virtualPath string
if header.Typeflag == tar.TypeDir {
virtualPath = "/" + header.Name
virtualPath = "/" + cleanedFileName
} else {
virtualPath = "/" + filepath.Join(dirname, basename)
}

// where the file will be written to disk
absoluteDiskPath := filepath.Join(dirPath, header.Name)
absoluteDiskPath := filepath.Join(dirPath, cleanedFileName)

var fileType FileType = RegularFile
var fileType FileType
// write out the file/dir to disk
switch header.Typeflag {
case tar.TypeDir:
Expand All @@ -177,9 +180,12 @@
if err != nil {
return Image{}, err
}

if _, err := io.Copy(f, tarReader); err != nil {
return Image{}, err
numBytes, err := io.Copy(f, io.LimitReader(tarReader, fileReadLimit))
if numBytes >= fileReadLimit || errors.Is(err, io.EOF) {
return Image{}, fmt.Errorf("file exceeds read limit (potential decompression bomb attack)")
}
if err != nil {
return Image{}, fmt.Errorf("unable to copy file: %w", err)
}
fileType = RegularFile
f.Close()
Expand Down Expand Up @@ -232,6 +238,7 @@
}
filePath = dirname
}

return false
}

Expand Down
10 changes: 5 additions & 5 deletions internal/sourceanalysis/__snapshots__/integration_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 1675,
"line": 1674,
"column": 17
}
},
Expand All @@ -200,7 +200,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 2045,
"line": 2044,
"column": 18
}
},
Expand All @@ -213,7 +213,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 3285,
"line": 3286,
"column": 3
}
},
Expand All @@ -226,7 +226,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 3184,
"line": 3183,
"column": 18
}
},
Expand All @@ -238,7 +238,7 @@
"position": {
"filename": "\u003cAny value\u003e",
"offset": -1,
"line": 3438,
"line": 3441,
"column": 30
}
},
Expand Down
Loading