Skip to content

Commit 141de13

Browse files
kuscaranother-rex
andauthored
fix: Support osv-scanner-custom.json as a scannable file type (#2174) (#2177)
Added support for recognizing osv-scanner-custom.json with the -r option. I wasn’t sure if `"osv-scanner-custom.json": {osvscannerjson.Name},` also needs to be added to lockfile.go, since it seems to work without it. Please review and advise on any improvements. I will update the snapshots after. Do I also need to update the webpage that lists supported files? --------- Co-authored-by: Rex P <106129829+another-rex@users.noreply.github.com> Co-authored-by: Rex P <rexpan@google.com>
1 parent 07656e6 commit 141de13

8 files changed

Lines changed: 169 additions & 19 deletions

File tree

cmd/osv-scanner/scan/source/__snapshots__/command_test.snap

Lines changed: 112 additions & 16 deletions
Large diffs are not rendered by default.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"results": [
3+
{
4+
"source": {},
5+
"packages": [
6+
{
7+
"package": {
8+
"name": "stdlib",
9+
"version": "1.99.9",
10+
"ecosystem": "Go"
11+
}
12+
}
13+
]
14+
},
15+
{
16+
"source": {},
17+
"packages": [
18+
{
19+
"package": {
20+
"name": "toolchain",
21+
"version": "1.99.9",
22+
"ecosystem": "Go"
23+
}
24+
}
25+
]
26+
}
27+
]
28+
}

docs/supported_languages_and_lockfiles.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,10 @@ Then pass this to `osv-scanner` with this:
135135
```
136136
osv-scanner --lockfile osv-scanner:/path/to/osv-scanner.json
137137
```
138+
139+
For automatic scan detection, you can create an `osv-scanner-custom.json` file using the same procedure described above for `osv-scanner.json`.
140+
Run the command below for detection:
141+
142+
```
143+
./osv-scanner scan source -r /path/to/folder/you/want/to/scan
144+
```

internal/scalibrextract/language/osv/osvscannerjson/extractor.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"path/filepath"
89

910
"github.com/google/osv-scalibr/extractor"
1011
"github.com/google/osv-scalibr/extractor/filesystem"
@@ -32,9 +33,14 @@ func (e Extractor) Requirements() *plugin.Capabilities {
3233
return &plugin.Capabilities{}
3334
}
3435

35-
// FileRequired never returns true, as this is for the osv-scanner json output.
36-
func (e Extractor) FileRequired(_ filesystem.FileAPI) bool {
37-
return false
36+
func New() filesystem.Extractor {
37+
return Extractor{}
38+
}
39+
40+
// FileRequired returns true only for osv-scanner-custom.json files,
41+
// since this is specific to the osv-scanner JSON output
42+
func (e Extractor) FileRequired(fapi filesystem.FileAPI) bool {
43+
return filepath.Base(fapi.Path()) == "osv-scanner-custom.json"
3844
}
3945

4046
// Extract extracts packages from yarn.lock files passed through the scan input.
@@ -56,6 +62,7 @@ func (e Extractor) Extract(_ context.Context, input *filesystem.ScanInput) (inve
5662
Ecosystem: pkg.Package.Ecosystem,
5763
SourceInfo: res.Source,
5864
},
65+
PURLType: "placeholder",
5966
Locations: []string{input.Path},
6067
Plugins: []string{"osv/osvscannerjson"},
6168
}

internal/scalibrextract/language/osv/osvscannerjson/extractor_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestExtractor_Extract(t *testing.T) {
3939
Name: "activesupport",
4040
Version: "7.0.7",
4141
Locations: []string{"testdata/one-package.json"},
42+
PURLType: "placeholder",
4243
Plugins: []string{"osv/osvscannerjson"},
4344
Metadata: &osvscannerjson.Metadata{
4445
Ecosystem: "RubyGems",
@@ -58,6 +59,7 @@ func TestExtractor_Extract(t *testing.T) {
5859
WantPackages: []*extractor.Package{
5960
{
6061
Locations: []string{"testdata/one-package-commit.json"},
62+
PURLType: "placeholder",
6163
Plugins: []string{"osv/osvscannerjson"},
6264
SourceCode: &extractor.SourceCodeIdentifier{
6365
Commit: "9a6bd55c9d0722cb101fe85a3b22d89e4ff4fe52",
@@ -81,6 +83,7 @@ func TestExtractor_Extract(t *testing.T) {
8183
Name: "crossbeam-utils",
8284
Version: "0.6.6",
8385
Locations: []string{"testdata/multiple-packages-with-vulns.json"},
86+
PURLType: "placeholder",
8487
Plugins: []string{"osv/osvscannerjson"},
8588
Metadata: &osvscannerjson.Metadata{
8689
Ecosystem: "crates.io",
@@ -94,6 +97,7 @@ func TestExtractor_Extract(t *testing.T) {
9497
Name: "memoffset",
9598
Version: "0.5.6",
9699
Locations: []string{"testdata/multiple-packages-with-vulns.json"},
100+
PURLType: "placeholder",
97101
Plugins: []string{"osv/osvscannerjson"},
98102
Metadata: &osvscannerjson.Metadata{
99103
Ecosystem: "crates.io",
@@ -107,6 +111,7 @@ func TestExtractor_Extract(t *testing.T) {
107111
Name: "smallvec",
108112
Version: "1.6.0",
109113
Locations: []string{"testdata/multiple-packages-with-vulns.json"},
114+
PURLType: "placeholder",
110115
Plugins: []string{"osv/osvscannerjson"},
111116
Metadata: &osvscannerjson.Metadata{
112117
Ecosystem: "crates.io",

internal/scalibrplugin/__snapshots__/resolve_test.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ javascript/bunlock
5555
javascript/packagelockjson
5656
javascript/pnpmlock
5757
javascript/yarnlock
58+
osv/osvscannerjson
5859
php/composerlock
5960
python/pdmlock
6061
python/pipfilelock

internal/scalibrplugin/presets.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/google/osv-scanner/v2/internal/scalibrextract/filesystem/vendored"
3838
"github.com/google/osv-scanner/v2/internal/scalibrextract/language/java/pomxmlenhanceable"
3939
"github.com/google/osv-scanner/v2/internal/scalibrextract/language/javascript/nodemodules"
40+
"github.com/google/osv-scanner/v2/internal/scalibrextract/language/osv/osvscannerjson"
4041
"github.com/google/osv-scanner/v2/internal/scalibrextract/language/python/requirementsenhancable"
4142
"github.com/google/osv-scanner/v2/internal/scalibrextract/vcs/gitrepo"
4243
)
@@ -104,6 +105,8 @@ var ExtractorPresets = map[string]extractors.InitMap{
104105
// Haskell
105106
cabal.Name: {cabal.NewDefault},
106107
stacklock.Name: {stacklock.NewDefault},
108+
109+
osvscannerjson.Name: {osvscannerjson.New},
107110
},
108111
"directory": {
109112
gitrepo.Name: {gitrepo.New},

internal/scalibrplugin/resolve.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/google/osv-scanner/v2/internal/scalibrextract/filesystem/vendored"
1111
"github.com/google/osv-scanner/v2/internal/scalibrextract/language/java/pomxmlenhanceable"
1212
"github.com/google/osv-scanner/v2/internal/scalibrextract/language/javascript/nodemodules"
13+
"github.com/google/osv-scanner/v2/internal/scalibrextract/language/osv/osvscannerjson"
1314
"github.com/google/osv-scanner/v2/internal/scalibrextract/language/python/requirementsenhancable"
1415
"github.com/google/osv-scanner/v2/internal/scalibrextract/vcs/gitrepo"
1516
)
@@ -36,6 +37,8 @@ func resolveFromName(name string) (plugin.Plugin, error) {
3637
return vendored.New(), nil
3738
case gitrepo.Name:
3839
return gitrepo.New(), nil
40+
case osvscannerjson.Name:
41+
return osvscannerjson.New(), nil
3942
default:
4043
return nil, fmt.Errorf("not an exact name for a plugin: %q", name)
4144
}

0 commit comments

Comments
 (0)