Skip to content

Commit 0a213a0

Browse files
authored
Merge branch 'google:main' into main
2 parents 9062d56 + ff7f2d6 commit 0a213a0

33 files changed

+1002
-265
lines changed

.golangci.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ linters:
4242
- unused
4343

4444
linters-settings:
45+
govet:
46+
settings:
47+
printf:
48+
funcs:
49+
- (github.com/google/osv-scanner/pkg/reporter.Reporter).PrintErrorf
50+
- (github.com/google/osv-scanner/pkg/reporter.Reporter).PrintTextf
4551
depguard:
4652
rules:
4753
regexp:
@@ -66,6 +72,9 @@ linters-settings:
6672

6773
issues:
6874
exclude-rules:
75+
- path: pkg/reporter
76+
linters:
77+
- dupl
6978
- path: _test\.go
7079
linters:
7180
- goerr113

cmd/osv-reporter/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func run(args []string, stdout, stderr io.Writer) int {
4242
cli.VersionPrinter = func(ctx *cli.Context) {
4343
// Use the app Writer and ErrWriter since they will be the writers to keep parallel tests consistent
4444
tableReporter = reporter.NewTableReporter(ctx.App.Writer, ctx.App.ErrWriter, false, 0)
45-
tableReporter.PrintText(fmt.Sprintf("osv-scanner version: %s\ncommit: %s\nbuilt at: %s\n", ctx.App.Version, commit, date))
45+
tableReporter.PrintTextf("osv-scanner version: %s\ncommit: %s\nbuilt at: %s\n", ctx.App.Version, commit, date)
4646
}
4747

4848
app := &cli.App{
@@ -179,11 +179,11 @@ func run(args []string, stdout, stderr io.Writer) int {
179179
}
180180

181181
if errors.Is(err, osvscanner.NoPackagesFoundErr) {
182-
tableReporter.PrintError("No package sources found, --help for usage information.\n")
182+
tableReporter.PrintErrorf("No package sources found, --help for usage information.\n")
183183
return 128
184184
}
185185

186-
tableReporter.PrintError(fmt.Sprintf("%v\n", err))
186+
tableReporter.PrintErrorf("%v\n", err)
187187
}
188188

189189
// if we've been told to print an error, and not already exited with

cmd/osv-scanner/main.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/google/osv-scanner/pkg/osv"
1313
"github.com/google/osv-scanner/pkg/osvscanner"
1414
"github.com/google/osv-scanner/pkg/reporter"
15+
"github.com/google/osv-scanner/pkg/spdx"
1516
"golang.org/x/term"
1617

1718
"github.com/urfave/cli/v2"
@@ -28,7 +29,7 @@ func run(args []string, stdout, stderr io.Writer) int {
2829
cli.VersionPrinter = func(ctx *cli.Context) {
2930
// Use the app Writer and ErrWriter since they will be the writers to keep parallel tests consistent
3031
r = reporter.NewTableReporter(ctx.App.Writer, ctx.App.ErrWriter, false, 0)
31-
r.PrintText(fmt.Sprintf("osv-scanner version: %s\ncommit: %s\nbuilt at: %s\n", ctx.App.Version, commit, date))
32+
r.PrintTextf("osv-scanner version: %s\ncommit: %s\nbuilt at: %s\n", ctx.App.Version, commit, date)
3233
}
3334

3435
osv.RequestUserAgent = "osv-scanner/" + version.OSVVersion
@@ -171,12 +172,15 @@ func run(args []string, stdout, stderr io.Writer) int {
171172
return fmt.Errorf("--experimental-licenses-summary and --experimental-licenses flags cannot be set")
172173
}
173174
allowlist := context.StringSlice("experimental-licenses")
174-
if context.IsSet("experimental-licenses") &&
175-
(len(allowlist) == 0 ||
176-
(len(allowlist) == 1 && allowlist[0] == "")) {
177-
return fmt.Errorf("--experimental-licenses requires at least one value")
175+
if context.IsSet("experimental-licenses") {
176+
if len(allowlist) == 0 ||
177+
(len(allowlist) == 1 && allowlist[0] == "") {
178+
return fmt.Errorf("--experimental-licenses requires at least one value")
179+
}
180+
if unrecognized := spdx.Unrecognized(allowlist); len(unrecognized) > 0 {
181+
return fmt.Errorf("--experimental-licenses requires comma-separated spdx licenses. The following license(s) are not recognized as spdx: %s", strings.Join(unrecognized, ","))
182+
}
178183
}
179-
// TODO: verify that the licenses they passed in are indeed spdx.
180184

181185
if r, err = reporter.New(format, stdout, stderr, termWidth); err != nil {
182186
return err
@@ -185,7 +189,7 @@ func run(args []string, stdout, stderr io.Writer) int {
185189
var callAnalysisStates map[string]bool
186190
if context.IsSet("experimental-call-analysis") {
187191
callAnalysisStates = createCallAnalysisStates([]string{"all"}, context.StringSlice("no-call-analysis"))
188-
r.PrintText("Warning: the experimental-call-analysis flag has been replaced. Please use the call-analysis and no-call-analysis flags instead.\n")
192+
r.PrintTextf("Warning: the experimental-call-analysis flag has been replaced. Please use the call-analysis and no-call-analysis flags instead.\n")
189193
} else {
190194
callAnalysisStates = createCallAnalysisStates(context.StringSlice("call-analysis"), context.StringSlice("no-call-analysis"))
191195
}
@@ -236,10 +240,10 @@ func run(args []string, stdout, stderr io.Writer) int {
236240
case errors.Is(err, osvscanner.VulnerabilitiesFoundErr):
237241
return 1
238242
case errors.Is(err, osvscanner.NoPackagesFoundErr):
239-
r.PrintError("No package sources found, --help for usage information.\n")
243+
r.PrintErrorf("No package sources found, --help for usage information.\n")
240244
return 128
241245
}
242-
r.PrintError(fmt.Sprintf("%v\n", err))
246+
r.PrintErrorf("%v\n", err)
243247
}
244248

245249
// if we've been told to print an error, and not already exited with

cmd/osv-scanner/main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ func TestRun_LockfileWithExplicitParseAs(t *testing.T) {
623623
args: []string{"", "--lockfile=go.mod:./fixtures/locks-many/replace-local.mod"},
624624
wantExitCode: 0,
625625
wantStdout: `
626-
Scanned <rootdir>/fixtures/locks-many/replace-local.mod file as a go.mod and found 2 packages
626+
Scanned <rootdir>/fixtures/locks-many/replace-local.mod file as a go.mod and found 1 package
627627
Filtered 1 local package/s from the scan.
628628
No issues found
629629
`,

docs/README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ Here are other [pre-requisites] and instructions for running the [docs locally].
1111
[pre-requisites]: https://docs.github.com/en/pages/setting-up-a-github-pages-site-with-jekyll/testing-your-github-pages-site-locally-with-jekyll#prerequisites
1212
[docs locally]: https://docs.github.com/en/pages/setting-up-a-github-pages-site-with-jekyll/testing-your-github-pages-site-locally-with-jekyll#building-your-site-locally
1313

14-
## Contributing to the docs
14+
## Formatting docs
1515

16-
Please see [CONTRIBUTING.md](https://github.com/google/osv-scanner/blob/main/CONTRIBUTING.md/#contributing-documentation) for information on contributing documentation.
16+
We use - [Prettier](https://prettier.io/) to standardize the format of markdown and config files.
17+
18+
This requires [node/npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) to be installed.
19+
20+
### Running the formatter
21+
22+
Run the following in the project directory:
23+
24+
```shell
25+
./scripts/run_formatters.sh
26+
```
1727

1828
## Documentation theme
1929

docs/github-action.md

+88-98
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@ nav_order: 7
2020

2121
OSV-Scanner is offered as a GitHub Action. We currently have two different GitHub Actions:
2222

23-
1. An action that triggers a scan with each [pull request](./github-action.md#scans-on-prs) and will only check for new vulnerabilities introduced through the pull request.
24-
2. An action that performs a single vulnerability scan, which can be configured to scan on a [regular schedule](./github-action.md#scheduled-scans), or used as a check [on releases](./github-action.md#scan-on-release) to prevent releasing with known vulnerabilities in dependencies.
23+
1. An action that triggers a scan with each [pull request](./github-action.md#scan-on-pull-request) and will only report new vulnerabilities introduced through the pull request.
24+
2. An action that performs a full vulnerability scan, which can be configured to scan on a [regular schedule](./github-action.md#scheduled-scans). The full vulnerability scan can also be configured to run [on release](./github-action.md#scan-on-release) to prevent releasing with known vulnerabilities in dependencies.
2525

26-
## Scans on PRs
26+
## Scan on pull request
2727

28-
Scanning your project on each pull request can help you keep vulnerabilities out of your project. This GitHub Action compares a vulnerability scan of the target branch to a vulnerability scan of the feature branch, and will fail if there are new vulnerabilities found which doesn't exist in the target branch. You will be notified of any new vulnerabilities introduced through the feature branch. You can also choose to [prevent merging](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches#require-status-checks-before-merging) if new vulnerabilities are introduced through the feature branch.
28+
Scanning your project on each pull request can help you keep vulnerabilities out of your project. This GitHub Action compares a vulnerability scan of the target branch to a vulnerability scan of the feature branch, and will fail if there are new vulnerabilities introduced through the feature branch. You may choose to [prevent merging](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches#require-status-checks-before-merging) if new vulnerabilities are introduced, but by default the check will only warn users.
2929

3030
### Instructions
3131

32-
In your project repository, create a new file `.github/workflows/osv-scanner-pr.yml`.
33-
34-
Include the following in the [`osv-scanner-pr.yml`](https://github.com/google/osv-scanner/blob/main/.github/workflows/osv-scanner-pr.yml) file:
32+
In your project repository, create a new file `.github/workflows/osv-scanner-pr.yml` and include the following:
3533

3634
```yml
3735
name: OSV-Scanner PR Scan
@@ -51,104 +49,20 @@ permissions:
5149

5250
jobs:
5351
scan-pr:
54-
uses: "google/osv-scanner/.github/workflows/osv-scanner-reusable-pr.yml@main"
52+
uses: "google/osv-scanner-action/.github/workflows/osv-scanner-reusable.yml@v1.5.0"
5553
```
5654
5755
### View results
5856
5957
Results may be viewed by clicking on the details of the failed action, either from your project's actions tab or directly on the PR. Results are also included in GitHub annotations on the "Files changed" tab for the PR.
6058
61-
### Customization
62-
63-
`osv-scanner-reusable.yml` takes two optional inputs:
64-
65-
- `scan-args`: This value is passed to `osv-scanner` CLI after being split by each line. See the [usage](./usage) page for the available options.
66-
Importantly `--format` and `--output` flags are already set by the reusable workflow and should not be overridden here.
67-
Default:
68-
```bash
69-
--recursive # Recursively scan subdirectories
70-
--skip-git=true # Skip commit scanning to focus on dependencies
71-
./ # Start the scan from the root of the repository
72-
```
73-
- `results-file-name`: This is the name of the final SARIF file uploaded to Github.
74-
Default: `results.sarif`
75-
- `download-artifact`: Optional artifact to download for scanning. Can be used if you need to do some preprocessing to prepare the lockfiles for scanning.
76-
If the file names in the artifact are not standard lockfile names, make sure to add custom scan-args to specify the lockfile type and path (see [specify lockfiles](./usage#specify-lockfiles)).
77-
- `upload-sarif`: Whether to upload the results to Security > Code Scanning. Defaults to `true`.
78-
79-
<details markdown="block">
80-
<summary>
81-
Examples
82-
</summary>
83-
84-
##### Scan specific lockfiles
85-
86-
```yml
87-
jobs:
88-
scan-pr:
89-
uses: "google/osv-scanner/.github/workflows/osv-scanner-reusable.yml"
90-
with:
91-
scan-args: |-
92-
--lockfile=./path/to/lockfile1
93-
--lockfile=requirements.txt:./path/to/python-lockfile2.txt
94-
```
95-
96-
##### Default arguments
97-
98-
```yml
99-
jobs:
100-
scan-pr:
101-
uses: "google/osv-scanner/.github/workflows/osv-scanner-reusable.yml"
102-
with:
103-
scan-args: |-
104-
--recursive
105-
--skip-git=true
106-
./
107-
```
108-
109-
##### Using download-artifact input to support preprocessing
110-
111-
```yml
112-
jobs:
113-
extract-deps:
114-
name: Extract Dependencies
115-
# ...
116-
steps:
117-
# ... Steps to extract your dependencies
118-
- name: "upload osv-scanner deps" # Upload the deps
119-
uses: actions/upload-artifact@v4
120-
with:
121-
name: converted-OSV-Scanner-deps
122-
path: osv-scanner-deps.json
123-
retention-days: 2
124-
vuln-scan:
125-
name: Vulnerability scanning
126-
# makes sure the extraction step is completed before running the scanner
127-
needs: extract-deps
128-
uses: "google/osv-scanner/.github/workflows/osv-scanner-reusable.yml@main"
129-
with:
130-
# Download the artifact uploaded in extract-deps step
131-
download-artifact: converted-OSV-Scanner-deps
132-
# Scan only the file inside the uploaded artifact
133-
scan-args: |-
134-
--lockfile=osv-scanner:osv-scanner-deps.json
135-
permissions:
136-
# Needed to upload the SARIF results to code-scanning dashboard.
137-
security-events: write
138-
contents: read
139-
```
140-
141-
</details>
142-
14359
## Scheduled scans
14460
14561
Regularly scanning your project for vulnerabilities can alert you to new vulnerabilities in your dependency tree. This GitHub Action will scan your project on a set schedule and report all known vulnerabilities. If vulnerabilities are found the action will return a failed status.
14662
14763
### Instructions
14864
149-
In your project repository, create a new file `.github/workflows/osv-scanner-scheduled.yml`.
150-
151-
Include the following in the [`osv-scanner-scheduled.yml`](https://github.com/google/osv-scanner/blob/main/.github/workflows/osv-scanner-scheduled.yml) file:
65+
In your project repository, create a new file `.github/workflows/osv-scanner-scheduled.yml` and include the following:
15266

15367
```yml
15468
name: OSV-Scanner Scheduled Scan
@@ -168,15 +82,11 @@ permissions:
16882
16983
jobs:
17084
scan-scheduled:
171-
uses: "google/osv-scanner/.github/workflows/osv-scanner-reusable.yml@main"
85+
uses: "google/osv-scanner-action/.github/workflows/osv-scanner-reusable.yml@v1.5.0"
17286
```
17387

17488
As written, the scanner will run on 12:30 pm UTC every Monday, and also on every push to the main branch. You can change the schedule by following the instructions [here](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule).
17589

176-
### Customization
177-
178-
`osv-scanner-reusable-pr.yml` has the same customization options as `osv-scanner-reusable.yml`, which is described [here](./github-action.md#customization).
179-
18090
### View results
18191

18292
Maintainers can review results of the scan by navigating to their project's `security > code scanning` tab. Vulnerability details can also be viewed by clicking on the details of the failed action.
@@ -223,3 +133,83 @@ jobs:
223133
### View results
224134
225135
Results may be viewed by clicking on the details of the failed release action from the action tab.
136+
137+
## Customization
138+
139+
The GitHub Actions have the following optional inputs:
140+
141+
- `scan-args`: This value is passed to `osv-scanner` CLI after being split by each line. See the [usage](./usage) page for the available options. The `--format` and `--output` flags are already set by the reusable workflow and should not be overridden here.
142+
Default:
143+
```bash
144+
--recursive # Recursively scan subdirectories
145+
--skip-git=true # Skip commit scanning to focus on dependencies
146+
./ # Start the scan from the root of the repository
147+
```
148+
- `results-file-name`: This is the name of the final SARIF file uploaded to Github.
149+
Default: `results.sarif`
150+
- `download-artifact`: Optional artifact to download for scanning. Can be used if you need to do some preprocessing to prepare the lockfiles for scanning. If the file names in the artifact are not standard lockfile names, make sure to add custom scan-args to specify the lockfile type and path (see [specify lockfiles](./usage#specify-lockfiles)).
151+
- `upload-sarif`: Whether to upload the results to Security > Code Scanning. Defaults to `true`.
152+
153+
<details markdown="block">
154+
<summary>
155+
Examples
156+
</summary>
157+
158+
#### Scan specific lockfiles
159+
160+
```yml
161+
jobs:
162+
scan-pr:
163+
uses: "google/osv-scanner-action/.github/workflows/[email protected]"
164+
with:
165+
scan-args: |-
166+
--lockfile=./path/to/lockfile1
167+
--lockfile=requirements.txt:./path/to/python-lockfile2.txt
168+
```
169+
170+
#### Default arguments
171+
172+
```yml
173+
jobs:
174+
scan-pr:
175+
uses: "google/osv-scanner-action/.github/workflows/[email protected]"
176+
with:
177+
scan-args: |-
178+
--recursive
179+
--skip-git=true
180+
./
181+
```
182+
183+
#### Using download-artifact input to support preprocessing
184+
185+
```yml
186+
jobs:
187+
extract-deps:
188+
name: Extract Dependencies
189+
# ...
190+
steps:
191+
# ... Steps to extract your dependencies
192+
- name: "upload osv-scanner deps" # Upload the deps
193+
uses: actions/upload-artifact@v4
194+
with:
195+
name: converted-OSV-Scanner-deps
196+
path: osv-scanner-deps.json
197+
retention-days: 2
198+
vuln-scan:
199+
name: Vulnerability scanning
200+
# makes sure the extraction step is completed before running the scanner
201+
needs: extract-deps
202+
uses: "google/osv-scanner-action/.github/workflows/[email protected]"
203+
with:
204+
# Download the artifact uploaded in extract-deps step
205+
download-artifact: converted-OSV-Scanner-deps
206+
# Scan only the file inside the uploaded artifact
207+
scan-args: |-
208+
--lockfile=osv-scanner:osv-scanner-deps.json
209+
permissions:
210+
# Needed to upload the SARIF results to code-scanning dashboard.
211+
security-events: write
212+
contents: read
213+
```
214+
215+
</details>

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ go 1.21
55
require (
66
deps.dev/api/v3alpha v0.0.0-20231114023923-e40c4d5c34e5
77
github.com/BurntSushi/toml v1.3.2
8-
github.com/CycloneDX/cyclonedx-go v0.7.2
8+
github.com/CycloneDX/cyclonedx-go v0.8.0
99
github.com/go-git/go-billy/v5 v5.5.0
10-
github.com/go-git/go-git/v5 v5.10.1
10+
github.com/go-git/go-git/v5 v5.11.0
1111
github.com/google/go-cmp v0.6.0
1212
github.com/hexops/gotextdiff v1.0.3
1313
github.com/ianlancetaylor/demangle v0.0.0-20231023195312-e2daf7ba7156

0 commit comments

Comments
 (0)