-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): Add support for cargo-auditable (#2675)
- Loading branch information
Showing
20 changed files
with
180 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package binary | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"os" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"github.com/aquasecurity/go-dep-parser/pkg/rust/binary" | ||
"github.com/aquasecurity/trivy/pkg/fanal/analyzer" | ||
"github.com/aquasecurity/trivy/pkg/fanal/analyzer/language" | ||
"github.com/aquasecurity/trivy/pkg/fanal/types" | ||
"github.com/aquasecurity/trivy/pkg/fanal/utils" | ||
) | ||
|
||
func init() { | ||
analyzer.RegisterAnalyzer(&rustBinaryLibraryAnalyzer{}) | ||
} | ||
|
||
const version = 1 | ||
|
||
type rustBinaryLibraryAnalyzer struct{} | ||
|
||
func (a rustBinaryLibraryAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) { | ||
p := binary.NewParser() | ||
libs, deps, err := p.Parse(input.Content) | ||
if errors.Is(err, binary.ErrUnrecognizedExe) || errors.Is(err, binary.ErrNonRustBinary) { | ||
return nil, nil | ||
} else if err != nil { | ||
return nil, xerrors.Errorf("rust binary parse error: %w", err) | ||
} | ||
|
||
return language.ToAnalysisResult(types.RustBinary, input.FilePath, "", libs, deps), nil | ||
} | ||
|
||
func (a rustBinaryLibraryAnalyzer) Required(_ string, fileInfo os.FileInfo) bool { | ||
return utils.IsExecutable(fileInfo) | ||
} | ||
|
||
func (a rustBinaryLibraryAnalyzer) Type() analyzer.Type { | ||
return analyzer.TypeRustBinary | ||
} | ||
|
||
func (a rustBinaryLibraryAnalyzer) Version() int { | ||
return version | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package binary | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aquasecurity/trivy/pkg/fanal/analyzer" | ||
"github.com/aquasecurity/trivy/pkg/fanal/types" | ||
) | ||
|
||
func Test_rustBinaryLibraryAnalyzer_Analyze(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputFile string | ||
want *analyzer.AnalysisResult | ||
}{ | ||
{ | ||
name: "happy path", | ||
inputFile: "testdata/executable_rust", | ||
want: &analyzer.AnalysisResult{ | ||
Applications: []types.Application{ | ||
{ | ||
Type: types.RustBinary, | ||
FilePath: "testdata/executable_rust", | ||
Libraries: []types.Package{ | ||
{Name: "crate_with_features", Version: "0.1.0"}, | ||
{Name: "library_crate", Version: "0.1.0"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "not rust binary", | ||
inputFile: "testdata/executable_bash", | ||
}, | ||
{ | ||
name: "broken elf", | ||
inputFile: "testdata/broken_elf", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f, err := os.Open(tt.inputFile) | ||
require.NoError(t, err) | ||
defer f.Close() | ||
|
||
a := rustBinaryLibraryAnalyzer{} | ||
ctx := context.Background() | ||
got, err := a.Analyze(ctx, analyzer.AnalysisInput{ | ||
FilePath: tt.inputFile, | ||
Content: f, | ||
}) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func Test_rustBinaryLibraryAnalyzer_Required(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
filePath string | ||
want bool | ||
}{ | ||
{ | ||
name: "file perm 0755", | ||
filePath: "testdata/0755", | ||
want: true, | ||
}, | ||
{ | ||
name: "file perm 0644", | ||
filePath: "testdata/0644", | ||
want: false, | ||
}, | ||
{ | ||
name: "symlink", | ||
filePath: "testdata/symlink", | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
a := rustBinaryLibraryAnalyzer{} | ||
fileInfo, err := os.Lstat(tt.filePath) | ||
require.NoError(t, err) | ||
got := a.Required(tt.filePath, fileInfo) | ||
assert.Equal(t, tt.want, got, fileInfo.Mode().Perm()) | ||
}) | ||
} | ||
} |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ELF |
3 changes: 3 additions & 0 deletions
3
pkg/fanal/analyzer/language/rust/binary/testdata/executable_bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
echo "hello" |
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters