-
Notifications
You must be signed in to change notification settings - Fork 106
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
feat(rust): Add support for cargo-auditable binaries #119
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
84c4c6c
Add support for cargo-auditable binaries
tofay 9cb41d3
add comment about source of test binaries
tofay 7828e9a
add windows test fixture
tofay e3b7b9b
close test fixture files
tofay 0da4084
Merge branch 'main' into cargo-auditable-support
tofay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Detects dependencies from Rust binaries built with https://github.com/rust-secure-code/cargo-auditable | ||
package binary | ||
|
||
import ( | ||
"golang.org/x/xerrors" | ||
|
||
dio "github.com/aquasecurity/go-dep-parser/pkg/io" | ||
"github.com/aquasecurity/go-dep-parser/pkg/types" | ||
rustaudit "github.com/microsoft/go-rustaudit" | ||
) | ||
|
||
var ( | ||
ErrUnrecognizedExe = xerrors.New("unrecognized executable format") | ||
ErrNonRustBinary = xerrors.New("non Rust auditable binary") | ||
) | ||
|
||
// convertError detects rustaudit.ErrUnknownFileFormat and convert to | ||
// ErrUnrecognizedExe and convert rustaudit.ErrNoRustDepInfo to ErrNonRustBinary | ||
func convertError(err error) error { | ||
if err == rustaudit.ErrUnknownFileFormat { | ||
return ErrUnrecognizedExe | ||
} | ||
if err == rustaudit.ErrNoRustDepInfo { | ||
return ErrNonRustBinary | ||
} | ||
|
||
return err | ||
} | ||
|
||
type Parser struct{} | ||
|
||
func NewParser() types.Parser { | ||
return &Parser{} | ||
} | ||
|
||
// Parse scans files to try to report Rust crates and version injected into Rust binaries | ||
// via https://github.com/rust-secure-code/cargo-auditable | ||
func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) { | ||
info, err := rustaudit.GetDependencyInfo(r) | ||
if err != nil { | ||
return nil, nil, convertError(err) | ||
} | ||
|
||
var libs []types.Library | ||
for _, dep := range info.Packages { | ||
if dep.Kind == rustaudit.Runtime { | ||
libs = append(libs, types.Library{ | ||
Name: dep.Name, | ||
Version: dep.Version, | ||
}) | ||
} | ||
} | ||
|
||
return libs, nil, nil | ||
} |
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,89 @@ | ||
package binary_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aquasecurity/go-dep-parser/pkg/rust/binary" | ||
"github.com/aquasecurity/go-dep-parser/pkg/types" | ||
) | ||
|
||
// Test binaries generated from cargo-auditable test fixture | ||
// https://github.com/rust-secure-code/cargo-auditable/tree/6b77151/cargo-auditable/tests/fixtures/workspace | ||
|
||
func TestParse(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputFile string | ||
want []types.Library | ||
wantErr string | ||
}{ | ||
{ | ||
name: "ELF", | ||
inputFile: "testdata/test.elf", | ||
want: []types.Library{ | ||
{ | ||
Name: "crate_with_features", | ||
Version: "0.1.0", | ||
}, | ||
{ | ||
Name: "library_crate", | ||
Version: "0.1.0", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "PE", | ||
inputFile: "testdata/test.exe", | ||
want: []types.Library{ | ||
{ | ||
Name: "crate_with_features", | ||
Version: "0.1.0", | ||
}, | ||
{ | ||
Name: "library_crate", | ||
Version: "0.1.0", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "Mach-O", | ||
inputFile: "testdata/test.macho", | ||
want: []types.Library{ | ||
{ | ||
Name: "crate_with_features", | ||
Version: "0.1.0", | ||
}, | ||
{ | ||
Name: "library_crate", | ||
Version: "0.1.0", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "sad path", | ||
inputFile: "testdata/dummy", | ||
wantErr: "unrecognized executable format", | ||
}, | ||
} | ||
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() | ||
|
||
got, _, err := binary.NewParser().Parse(f) | ||
if tt.wantErr != "" { | ||
require.NotNil(t, err) | ||
assert.Contains(t, err.Error(), tt.wantErr) | ||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
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 @@ | ||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like defer f.Close() is missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added here, and to the golang binary tests which I based these off
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!