Skip to content

Commit

Permalink
fix: ensure that vulnerability results are ordered deterministically (#…
Browse files Browse the repository at this point in the history
…220)

Resolves #182
  • Loading branch information
G-Rath authored and hayleycd committed Mar 9, 2023
1 parent 7e5b851 commit cae0944
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
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
{
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
}

0 comments on commit cae0944

Please sign in to comment.