Skip to content

Commit

Permalink
Merge branch 'main' into add-formatting-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayley Denbraver authored Dec 28, 2023
2 parents f376401 + e95b8a7 commit 8bfca2d
Show file tree
Hide file tree
Showing 30 changed files with 325 additions and 286 deletions.
34 changes: 0 additions & 34 deletions .github/workflows/osv-scanner-pr.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,37 @@
name: OSV-Scanner Scheduled Scan

on:
pull_request:
branches: ["main"]
merge_group:
branches: ["main"]
schedule:
- cron: "12 12 * * 1"
push:
branches: ["main"]

permissions:
# Require writing security events to upload SARIF file to security tab
security-events: write
# Read commit contents
contents: read

jobs:
scan-scheduled:
if: ${{ github.event_name == 'push' || github.event_name == 'schedule' }}
uses: "./.github/workflows/osv-scanner-reusable.yml"
with:
# Just scan the root directory and docs, since everything else is fixtures
scan-args: |-
--skip-git
./
./docs/
permissions:
# Require writing security events to upload SARIF file to security tab
security-events: write
# Read commit contents
contents: read
scan-pr:
if: ${{ github.event_name == 'pull_request' || github.event_name == 'merge_group' }}
uses: "./.github/workflows/osv-scanner-reusable-pr.yml"
with:
# Just scan the root directory and docs, since everything else is fixtures
scan-args: |-
--skip-git
./
./docs/
9 changes: 9 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ linters:
- unused

linters-settings:
govet:
settings:
printf:
funcs:
- (github.com/google/osv-scanner/pkg/reporter.Reporter).PrintErrorf
- (github.com/google/osv-scanner/pkg/reporter.Reporter).PrintTextf
depguard:
rules:
regexp:
Expand All @@ -66,6 +72,9 @@ linters-settings:

issues:
exclude-rules:
- path: pkg/reporter
linters:
- dupl
- path: _test\.go
linters:
- goerr113
Expand Down
6 changes: 3 additions & 3 deletions cmd/osv-reporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func run(args []string, stdout, stderr io.Writer) int {
cli.VersionPrinter = func(ctx *cli.Context) {
// Use the app Writer and ErrWriter since they will be the writers to keep parallel tests consistent
tableReporter = reporter.NewTableReporter(ctx.App.Writer, ctx.App.ErrWriter, false, 0)
tableReporter.PrintText(fmt.Sprintf("osv-scanner version: %s\ncommit: %s\nbuilt at: %s\n", ctx.App.Version, commit, date))
tableReporter.PrintTextf("osv-scanner version: %s\ncommit: %s\nbuilt at: %s\n", ctx.App.Version, commit, date)
}

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

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

tableReporter.PrintError(fmt.Sprintf("%v\n", err))
tableReporter.PrintErrorf("%v\n", err)
}

// if we've been told to print an error, and not already exited with
Expand Down
8 changes: 4 additions & 4 deletions cmd/osv-scanner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func run(args []string, stdout, stderr io.Writer) int {
cli.VersionPrinter = func(ctx *cli.Context) {
// Use the app Writer and ErrWriter since they will be the writers to keep parallel tests consistent
r = reporter.NewTableReporter(ctx.App.Writer, ctx.App.ErrWriter, false, 0)
r.PrintText(fmt.Sprintf("osv-scanner version: %s\ncommit: %s\nbuilt at: %s\n", ctx.App.Version, commit, date))
r.PrintTextf("osv-scanner version: %s\ncommit: %s\nbuilt at: %s\n", ctx.App.Version, commit, date)
}

osv.RequestUserAgent = "osv-scanner/" + version.OSVVersion
Expand Down Expand Up @@ -185,7 +185,7 @@ func run(args []string, stdout, stderr io.Writer) int {
var callAnalysisStates map[string]bool
if context.IsSet("experimental-call-analysis") {
callAnalysisStates = createCallAnalysisStates([]string{"all"}, context.StringSlice("no-call-analysis"))
r.PrintText("Warning: the experimental-call-analysis flag has been replaced. Please use the call-analysis and no-call-analysis flags instead.\n")
r.PrintTextf("Warning: the experimental-call-analysis flag has been replaced. Please use the call-analysis and no-call-analysis flags instead.\n")
} else {
callAnalysisStates = createCallAnalysisStates(context.StringSlice("call-analysis"), context.StringSlice("no-call-analysis"))
}
Expand Down Expand Up @@ -236,10 +236,10 @@ func run(args []string, stdout, stderr io.Writer) int {
case errors.Is(err, osvscanner.VulnerabilitiesFoundErr):
return 1
case errors.Is(err, osvscanner.NoPackagesFoundErr):
r.PrintError("No package sources found, --help for usage information.\n")
r.PrintErrorf("No package sources found, --help for usage information.\n")
return 128
}
r.PrintError(fmt.Sprintf("%v\n", err))
r.PrintErrorf("%v\n", err)
}

// if we've been told to print an error, and not already exited with
Expand Down
2 changes: 1 addition & 1 deletion cmd/osv-scanner/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ func TestRun_LockfileWithExplicitParseAs(t *testing.T) {
args: []string{"", "--lockfile=go.mod:./fixtures/locks-many/replace-local.mod"},
wantExitCode: 0,
wantStdout: `
Scanned <rootdir>/fixtures/locks-many/replace-local.mod file as a go.mod and found 2 packages
Scanned <rootdir>/fixtures/locks-many/replace-local.mod file as a go.mod and found 1 package
Filtered 1 local package/s from the scan.
No issues found
`,
Expand Down
186 changes: 88 additions & 98 deletions docs/github-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ nav_order: 7

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

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.
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.
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.
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.

## Scans on PRs
## Scan on pull request

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.
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.

### Instructions

In your project repository, create a new file `.github/workflows/osv-scanner-pr.yml`.

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

```yml
name: OSV-Scanner PR Scan
Expand All @@ -51,104 +49,20 @@ permissions:

jobs:
scan-pr:
uses: "google/osv-scanner/.github/workflows/osv-scanner-reusable-pr.yml@main"
uses: "google/osv-scanner-action/.github/workflows/osv-scanner-reusable.yml@v1.5.0"
```
### View results
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.
### Customization
`osv-scanner-reusable.yml` takes two optional inputs:

- `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.
Importantly `--format` and `--output` flags are already set by the reusable workflow and should not be overridden here.
Default:
```bash
--recursive # Recursively scan subdirectories
--skip-git=true # Skip commit scanning to focus on dependencies
./ # Start the scan from the root of the repository
```
- `results-file-name`: This is the name of the final SARIF file uploaded to Github.
Default: `results.sarif`
- `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)).
- `upload-sarif`: Whether to upload the results to Security > Code Scanning. Defaults to `true`.

<details markdown="block">
<summary>
Examples
</summary>

##### Scan specific lockfiles

```yml
jobs:
scan-pr:
uses: "google/osv-scanner/.github/workflows/osv-scanner-reusable.yml"
with:
scan-args: |-
--lockfile=./path/to/lockfile1
--lockfile=requirements.txt:./path/to/python-lockfile2.txt
```

##### Default arguments

```yml
jobs:
scan-pr:
uses: "google/osv-scanner/.github/workflows/osv-scanner-reusable.yml"
with:
scan-args: |-
--recursive
--skip-git=true
./
```

##### Using download-artifact input to support preprocessing

```yml
jobs:
extract-deps:
name: Extract Dependencies
# ...
steps:
# ... Steps to extract your dependencies
- name: "upload osv-scanner deps" # Upload the deps
uses: actions/upload-artifact@v4
with:
name: converted-OSV-Scanner-deps
path: osv-scanner-deps.json
retention-days: 2
vuln-scan:
name: Vulnerability scanning
# makes sure the extraction step is completed before running the scanner
needs: extract-deps
uses: "google/osv-scanner/.github/workflows/osv-scanner-reusable.yml@main"
with:
# Download the artifact uploaded in extract-deps step
download-artifact: converted-OSV-Scanner-deps
# Scan only the file inside the uploaded artifact
scan-args: |-
--lockfile=osv-scanner:osv-scanner-deps.json
permissions:
# Needed to upload the SARIF results to code-scanning dashboard.
security-events: write
contents: read
```

</details>

## Scheduled scans
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.
### Instructions
In your project repository, create a new file `.github/workflows/osv-scanner-scheduled.yml`.

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

```yml
name: OSV-Scanner Scheduled Scan
Expand All @@ -168,15 +82,11 @@ permissions:
jobs:
scan-scheduled:
uses: "google/osv-scanner/.github/workflows/osv-scanner-reusable.yml@main"
uses: "google/osv-scanner-action/.github/workflows/osv-scanner-reusable.yml@v1.5.0"
```

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).

### Customization

`osv-scanner-reusable-pr.yml` has the same customization options as `osv-scanner-reusable.yml`, which is described [here](./github-action.md#customization).

### View results

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.
Expand Down Expand Up @@ -223,3 +133,83 @@ jobs:
### View results
Results may be viewed by clicking on the details of the failed release action from the action tab.
## Customization
The GitHub Actions have the following optional inputs:
- `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.
Default:
```bash
--recursive # Recursively scan subdirectories
--skip-git=true # Skip commit scanning to focus on dependencies
./ # Start the scan from the root of the repository
```
- `results-file-name`: This is the name of the final SARIF file uploaded to Github.
Default: `results.sarif`
- `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)).
- `upload-sarif`: Whether to upload the results to Security > Code Scanning. Defaults to `true`.

<details markdown="block">
<summary>
Examples
</summary>

#### Scan specific lockfiles

```yml
jobs:
scan-pr:
uses: "google/osv-scanner-action/.github/workflows/[email protected]"
with:
scan-args: |-
--lockfile=./path/to/lockfile1
--lockfile=requirements.txt:./path/to/python-lockfile2.txt
```

#### Default arguments

```yml
jobs:
scan-pr:
uses: "google/osv-scanner-action/.github/workflows/[email protected]"
with:
scan-args: |-
--recursive
--skip-git=true
./
```

#### Using download-artifact input to support preprocessing

```yml
jobs:
extract-deps:
name: Extract Dependencies
# ...
steps:
# ... Steps to extract your dependencies
- name: "upload osv-scanner deps" # Upload the deps
uses: actions/upload-artifact@v4
with:
name: converted-OSV-Scanner-deps
path: osv-scanner-deps.json
retention-days: 2
vuln-scan:
name: Vulnerability scanning
# makes sure the extraction step is completed before running the scanner
needs: extract-deps
uses: "google/osv-scanner-action/.github/workflows/[email protected]"
with:
# Download the artifact uploaded in extract-deps step
download-artifact: converted-OSV-Scanner-deps
# Scan only the file inside the uploaded artifact
scan-args: |-
--lockfile=osv-scanner:osv-scanner-deps.json
permissions:
# Needed to upload the SARIF results to code-scanning dashboard.
security-events: write
contents: read
```

</details>
Loading

0 comments on commit 8bfca2d

Please sign in to comment.