Skip to content
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

fix: ensure that vulnerability results are ordered deterministically #220

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/osv-scanner/fixtures/locks-insecure/my-yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1

ansi-html@^0.0.1:
version "0.0.1"
47 changes: 47 additions & 0 deletions cmd/osv-scanner/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,53 @@ func TestRun_LockfileWithExplicitParseAs(t *testing.T) {
`,
wantStderr: "",
},
// multiple, + output order is deterministic
G-Rath marked this conversation as resolved.
Show resolved Hide resolved
{
name: "",
args: []string{
"",
"-L", "package-lock.json:" + filepath.FromSlash("./fixtures/locks-insecure/my-package-lock.json"),
"-L", "yarn.lock:" + filepath.FromSlash("./fixtures/locks-insecure/my-yarn.lock"),
filepath.FromSlash("./fixtures/locks-insecure"),
},
wantExitCode: 1,
wantStdout: `
Scanned %%/fixtures/locks-insecure/my-package-lock.json file as a package-lock.json and found 1 packages
Scanned %%/fixtures/locks-insecure/my-yarn.lock file as a yarn.lock and found 1 packages
Scanning dir ./fixtures/locks-insecure
Scanned %%/fixtures/locks-insecure/composer.lock file and found 0 packages
+-------------------------------------+-----------+-----------+---------+----------------------------------------------+
| OSV URL (ID IN BOLD) | ECOSYSTEM | PACKAGE | VERSION | SOURCE |
+-------------------------------------+-----------+-----------+---------+----------------------------------------------+
| https://osv.dev/GHSA-whgm-jr23-g3j9 | npm | ansi-html | 0.0.1 | fixtures/locks-insecure/my-package-lock.json |
| https://osv.dev/GHSA-whgm-jr23-g3j9 | npm | ansi-html | 0.0.1 | fixtures/locks-insecure/my-yarn.lock |
+-------------------------------------+-----------+-----------+---------+----------------------------------------------+
`,
wantStderr: "",
},
{
name: "",
args: []string{
"",
"-L", "yarn.lock:" + filepath.FromSlash("./fixtures/locks-insecure/my-yarn.lock"),
"-L", "package-lock.json:" + filepath.FromSlash("./fixtures/locks-insecure/my-package-lock.json"),
filepath.FromSlash("./fixtures/locks-insecure"),
},
wantExitCode: 1,
wantStdout: `
Scanned %%/fixtures/locks-insecure/my-yarn.lock file as a yarn.lock and found 1 packages
Scanned %%/fixtures/locks-insecure/my-package-lock.json file as a package-lock.json and found 1 packages
Scanning dir ./fixtures/locks-insecure
Scanned %%/fixtures/locks-insecure/composer.lock file and found 0 packages
+-------------------------------------+-----------+-----------+---------+----------------------------------------------+
| OSV URL (ID IN BOLD) | ECOSYSTEM | PACKAGE | VERSION | SOURCE |
+-------------------------------------+-----------+-----------+---------+----------------------------------------------+
| https://osv.dev/GHSA-whgm-jr23-g3j9 | npm | ansi-html | 0.0.1 | fixtures/locks-insecure/my-package-lock.json |
| https://osv.dev/GHSA-whgm-jr23-g3j9 | npm | ansi-html | 0.0.1 | fixtures/locks-insecure/my-yarn.lock |
+-------------------------------------+-----------+-----------+---------+----------------------------------------------+
`,
wantStderr: "",
},
// files that error on parsing stop parsable files from being checked
{
name: "",
Expand Down
9 changes: 9 additions & 0 deletions pkg/osvscanner/vulnerability_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package osvscanner

import (
"fmt"
"sort"

"github.com/google/osv-scanner/internal/output"
"github.com/google/osv-scanner/pkg/grouper"
Expand Down Expand Up @@ -58,5 +59,13 @@ func groupResponseBySource(r *output.Reporter, query osv.BatchedQuery, resp *osv
})
}

sort.Slice(output.Results, func(i, j int) bool {
if output.Results[i].Source.Path == output.Results[j].Source.Path {
return output.Results[i].Source.Type < output.Results[j].Source.Type
}

return output.Results[i].Source.Path < output.Results[j].Source.Path
})

return output
}