mirror of
https://github.com/anchore/syft.git
synced 2026-11-02 23:46:35 +01:00
* fix(generic): recover from parser and processor panics - a panic while parsing one file becomes an unknown for that location instead of failing the whole SBOM - a panic in a package processor is logged as a warning and the unprocessed results are kept - stacks go to the trace log, not into the unknown text Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix(unknown): keep errors that unwrap to nil `sync.PanicError` with a non-error panic value (e.g. `panic("boom")`) unwraps to nil, and was being dropped entirely from the error graph instead of being kept as a leaf. Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix(task): don't put the stack trace in task panic errors Task panics are still fatal, but the error now reads `panic in task "<name>": <value>` and the stack goes to the debug log. Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * test(generic): cover chained processors when a later one panics Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix(file): recover from panics in the executable and file digest catalogers A panic while reading one file is now recorded as an unknown for that file instead of failing the whole SBOM, the same as generic catalogers. Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
230 lines
6.0 KiB
Go
230 lines
6.0 KiB
Go
package filedigest
|
|
|
|
import (
|
|
"context"
|
|
"crypto"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
stereoscopeFile "github.com/anchore/stereoscope/pkg/file"
|
|
"github.com/anchore/stereoscope/pkg/imagetest"
|
|
intFile "github.com/anchore/syft/internal/file"
|
|
"github.com/anchore/syft/internal/unknown"
|
|
"github.com/anchore/syft/syft/file"
|
|
"github.com/anchore/syft/syft/source"
|
|
"github.com/anchore/syft/syft/source/directorysource"
|
|
"github.com/anchore/syft/syft/source/stereoscopesource"
|
|
)
|
|
|
|
func testDigests(t testing.TB, root string, files []string, hashes ...crypto.Hash) map[file.Coordinates][]file.Digest {
|
|
digests := make(map[file.Coordinates][]file.Digest)
|
|
|
|
for _, f := range files {
|
|
fh, err := os.Open(filepath.Join(root, f))
|
|
if err != nil {
|
|
t.Fatalf("could not open %q : %+v", f, err)
|
|
}
|
|
b, err := io.ReadAll(fh)
|
|
if err != nil {
|
|
t.Fatalf("could not read %q : %+v", f, err)
|
|
}
|
|
|
|
// Calculate digests for all files, including empty files
|
|
for _, hash := range hashes {
|
|
h := hash.New()
|
|
h.Write(b)
|
|
digests[file.NewLocation(f).Coordinates] = append(digests[file.NewLocation(f).Coordinates], file.Digest{
|
|
Algorithm: intFile.CleanDigestAlgorithmName(hash.String()),
|
|
Value: fmt.Sprintf("%x", h.Sum(nil)),
|
|
})
|
|
}
|
|
}
|
|
|
|
return digests
|
|
}
|
|
|
|
func TestDigestsCataloger(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
name string
|
|
digests []crypto.Hash
|
|
files []string
|
|
expected map[file.Coordinates][]file.Digest
|
|
}{
|
|
{
|
|
name: "md5",
|
|
digests: []crypto.Hash{crypto.MD5},
|
|
files: []string{"testdata/last/empty/empty", "testdata/last/path.txt"},
|
|
expected: testDigests(t, "testdata/last", []string{"empty/empty", "path.txt"}, crypto.MD5),
|
|
},
|
|
{
|
|
name: "md5-sha1-sha256",
|
|
digests: []crypto.Hash{crypto.MD5, crypto.SHA1, crypto.SHA256},
|
|
files: []string{"testdata/last/empty/empty", "testdata/last/path.txt"},
|
|
expected: testDigests(t, "testdata/last", []string{"empty/empty", "path.txt"}, crypto.MD5, crypto.SHA1, crypto.SHA256),
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
c := NewCataloger(test.digests)
|
|
|
|
src, err := directorysource.NewFromPath("testdata/last/")
|
|
require.NoError(t, err)
|
|
|
|
resolver, err := src.FileResolver(source.SquashedScope)
|
|
require.NoError(t, err)
|
|
|
|
actual, err := c.Catalog(context.Background(), resolver)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, test.expected, actual, "mismatched digests")
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDigestsCataloger_MixFileTypes(t *testing.T) {
|
|
testImage := "image-file-type-mix"
|
|
|
|
img := imagetest.GetFixtureImage(t, "docker-archive", testImage)
|
|
|
|
src := stereoscopesource.New(img, stereoscopesource.ImageConfig{
|
|
Reference: testImage,
|
|
})
|
|
|
|
resolver, err := src.FileResolver(source.SquashedScope)
|
|
if err != nil {
|
|
t.Fatalf("could not create resolver: %+v", err)
|
|
}
|
|
|
|
tests := []struct {
|
|
path string
|
|
expected string
|
|
}{
|
|
{
|
|
path: "/file-1.txt",
|
|
expected: "888c139e550867814eb7c33b84d76e4d",
|
|
},
|
|
// this is difficult to reproduce in a cross-platform way
|
|
//{
|
|
// path: "/hardlink-1",
|
|
//},
|
|
{
|
|
path: "/symlink-1",
|
|
},
|
|
{
|
|
path: "/char-device-1",
|
|
},
|
|
{
|
|
path: "/block-device-1",
|
|
},
|
|
{
|
|
path: "/fifo-1",
|
|
},
|
|
{
|
|
path: "/bin",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.path, func(t *testing.T) {
|
|
c := NewCataloger([]crypto.Hash{crypto.MD5})
|
|
|
|
actual, err := c.Catalog(context.Background(), resolver)
|
|
if err != nil {
|
|
t.Fatalf("could not catalog: %+v", err)
|
|
}
|
|
|
|
_, ref, err := img.SquashedTree().File(stereoscopeFile.Path(test.path))
|
|
if err != nil {
|
|
t.Fatalf("unable to get file=%q : %+v", test.path, err)
|
|
}
|
|
l := file.NewLocationFromImage(test.path, *ref.Reference, img)
|
|
|
|
if len(actual[l.Coordinates]) == 0 {
|
|
if test.expected != "" {
|
|
t.Fatalf("no digest found, but expected one")
|
|
}
|
|
|
|
} else {
|
|
assert.Equal(t, actual[l.Coordinates][0].Value, test.expected, "mismatched digests")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFileDigestCataloger_GivenCoordinates(t *testing.T) {
|
|
testImage := "image-file-type-mix"
|
|
|
|
img := imagetest.GetFixtureImage(t, "docker-archive", testImage)
|
|
|
|
c := NewCataloger([]crypto.Hash{crypto.SHA256})
|
|
|
|
src := stereoscopesource.New(img, stereoscopesource.ImageConfig{
|
|
Reference: testImage,
|
|
})
|
|
|
|
resolver, err := src.FileResolver(source.SquashedScope)
|
|
require.NoError(t, err)
|
|
|
|
tests := []struct {
|
|
path string
|
|
exists bool
|
|
expected string
|
|
}{
|
|
{
|
|
path: "/file-1.txt",
|
|
exists: true,
|
|
expected: "b089629781f05ef805b4511e93717f2ffa4c9d991771d5cbfa4b7242b4ef5fff",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.path, func(t *testing.T) {
|
|
_, ref, err := img.SquashedTree().File(stereoscopeFile.Path(test.path))
|
|
require.NoError(t, err)
|
|
|
|
l := file.NewLocationFromImage(test.path, *ref.Reference, img)
|
|
|
|
// note: an important difference between this test and the previous is that this test is using a list
|
|
// of specific coordinates to catalog
|
|
actual, err := c.Catalog(context.Background(), resolver, l.Coordinates)
|
|
require.NoError(t, err)
|
|
require.Len(t, actual, 1)
|
|
|
|
assert.Equal(t, test.expected, actual[l.Coordinates][0].Value, "mismatched digests")
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
type panicReader struct{}
|
|
|
|
func (panicReader) Read([]byte) (int, error) { panic("boom") }
|
|
|
|
type panickingContentsResolver struct {
|
|
*file.MockResolver
|
|
}
|
|
|
|
func (r panickingContentsResolver) FileContentsByLocation(file.Location) (io.ReadCloser, error) {
|
|
return io.NopCloser(panicReader{}), nil
|
|
}
|
|
|
|
func TestDigestsCataloger_panicBecomesUnknown(t *testing.T) {
|
|
resolver := panickingContentsResolver{file.NewMockResolverForPaths("cataloger_test.go")}
|
|
|
|
results, err := NewCataloger([]crypto.Hash{crypto.SHA256}).Catalog(context.Background(), resolver, file.NewLocation("cataloger_test.go").Coordinates)
|
|
|
|
require.Empty(t, results)
|
|
unknowns, remaining := unknown.ExtractCoordinateErrors(err)
|
|
require.NoError(t, remaining)
|
|
require.Len(t, unknowns, 1)
|
|
require.ErrorContains(t, unknowns[0].Reason, "recovered from panic while digesting file: boom")
|
|
}
|