-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add osv output lockfile + refactor (#505)
Introduces the osv-scanner results lockfile format (feel free to suggest a better name for this), This also refactors the models folder slightly to move some of the helper functions out to a separate internal crate. This stops the models package from pulling in the lockfile package, causing cyclic imports. I also added another field to `PackageDetails`, `Source`, a currently optional string to specify the location the dependency is actually specified. The only times it will be different from the lockfile location currently is: - Python requirements.txt `-r` - This new osv-scanner format, it stores the lockfile path parsed in the output results.
- Loading branch information
1 parent
99b9bfc
commit 540b301
Showing
9 changed files
with
684 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"results": [] | ||
} |
504 changes: 504 additions & 0 deletions
504
pkg/lockfile/fixtures/osvscannerresults/multi-packages-with-vulns.json
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
this is not valid json! (I think) |
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,21 @@ | ||
{ | ||
"results": [ | ||
{ | ||
"source": { | ||
"path": "/path/to/Gemfile.lock", | ||
"type": "lockfile" | ||
}, | ||
"packages": [ | ||
{ | ||
"package": { | ||
"name": "activesupport", | ||
"version": "7.0.7", | ||
"ecosystem": "RubyGems" | ||
}, | ||
"vulnerabilities": [], | ||
"groups": [] | ||
} | ||
] | ||
} | ||
] | ||
} |
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,87 @@ | ||
package lockfile_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/osv-scanner/pkg/lockfile" | ||
) | ||
|
||
func TestParseOSVScannerResults_FileDoesNotExist(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseOSVScannerResults("fixtures/osvscannerresults/does-not-exist") | ||
|
||
expectErrContaining(t, err, "no such file or directory") | ||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseOSVScannerResults_InvalidJSON(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseOSVScannerResults("fixtures/osvscannerresults/not-json.txt") | ||
|
||
expectErrContaining(t, err, "could not extract from") | ||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseOSVScannerResults_NoPackages(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseOSVScannerResults("fixtures/osvscannerresults/empty.json") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{}) | ||
} | ||
|
||
func TestParseOSVScannerResults_OnePackage(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseOSVScannerResults("fixtures/osvscannerresults/one-package.json") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{ | ||
{ | ||
Name: "activesupport", | ||
Version: "7.0.7", | ||
Ecosystem: lockfile.BundlerEcosystem, | ||
CompareAs: lockfile.BundlerEcosystem, | ||
}, | ||
}) | ||
} | ||
|
||
func TestParseOSVScannerResults_MultiPackages(t *testing.T) { | ||
t.Parallel() | ||
|
||
packages, err := lockfile.ParseOSVScannerResults("fixtures/osvscannerresults/multi-packages-with-vulns.json") | ||
|
||
if err != nil { | ||
t.Errorf("Got unexpected error: %v", err) | ||
} | ||
|
||
expectPackages(t, packages, []lockfile.PackageDetails{ | ||
{ | ||
Name: "crossbeam-utils", | ||
Version: "0.6.6", | ||
Ecosystem: lockfile.CargoEcosystem, | ||
CompareAs: lockfile.CargoEcosystem, | ||
}, | ||
{ | ||
Name: "memoffset", | ||
Version: "0.5.6", | ||
Ecosystem: lockfile.CargoEcosystem, | ||
CompareAs: lockfile.CargoEcosystem, | ||
}, | ||
{ | ||
Name: "smallvec", | ||
Version: "1.6.0", | ||
Ecosystem: lockfile.CargoEcosystem, | ||
CompareAs: lockfile.CargoEcosystem, | ||
}, | ||
}) | ||
} |
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 @@ | ||
package lockfile | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/google/osv-scanner/pkg/models" | ||
) | ||
|
||
func ParseOSVScannerResults(pathToLockfile string) ([]PackageDetails, error) { | ||
return extractFromFile(pathToLockfile, OSVScannerResultsExtractor{}) | ||
} | ||
|
||
type OSVScannerResultsExtractor struct{} | ||
|
||
func (e OSVScannerResultsExtractor) ShouldExtract(path string) bool { | ||
// The output will always be a custom json file, so don't return a default should extract | ||
return false | ||
} | ||
|
||
func (e OSVScannerResultsExtractor) Extract(f DepFile) ([]PackageDetails, error) { | ||
parsedResults := models.VulnerabilityResults{} | ||
err := json.NewDecoder(f).Decode(&parsedResults) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("could not extract from %s: %w", f.Path(), err) | ||
} | ||
|
||
packages := []PackageDetails{} | ||
for _, res := range parsedResults.Results { | ||
for _, pkg := range res.Packages { | ||
packages = append(packages, PackageDetails{ | ||
Name: pkg.Package.Name, | ||
Ecosystem: Ecosystem(pkg.Package.Ecosystem), | ||
Version: pkg.Package.Version, | ||
CompareAs: Ecosystem(pkg.Package.Ecosystem), | ||
}) | ||
} | ||
} | ||
|
||
return packages, nil | ||
} | ||
|
||
var _ Extractor = OSVScannerResultsExtractor{} | ||
|
||
// FromOSVScannerResults attempts to extract packages stored in the OSVScannerResults format | ||
func FromOSVScannerResults(pathToInstalled string) (Lockfile, error) { | ||
packages, err := extractFromFile(pathToInstalled, OSVScannerResultsExtractor{}) | ||
|
||
return Lockfile{ | ||
FilePath: pathToInstalled, | ||
ParsedAs: "osv-scanner-results", | ||
Packages: packages, | ||
}, err | ||
} |
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