-
Notifications
You must be signed in to change notification settings - Fork 487
fix: Fix lookup of golang packages with major versions #2259
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
Open
alexg-axis
wants to merge
2
commits into
google:main
Choose a base branch
from
alexg-axis:fix/golang-major-false-positives
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+29
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,13 @@ | |
| "context" | ||
| "errors" | ||
| "net/http" | ||
| "regexp" | ||
| "time" | ||
|
|
||
| "github.com/google/osv-scalibr/extractor" | ||
| "github.com/google/osv-scanner/v2/internal/cmdlogger" | ||
| "github.com/google/osv-scanner/v2/internal/imodels" | ||
| "github.com/ossf/osv-schema/bindings/go/osvconstants" | ||
| "github.com/ossf/osv-schema/bindings/go/osvschema" | ||
| "golang.org/x/sync/errgroup" | ||
| "osv.dev/bindings/go/api" | ||
|
|
@@ -20,6 +22,17 @@ | |
| maxConcurrentRequests = 1000 | ||
| ) | ||
|
|
||
| // goVersionSuffixRegexp matches a Golang major suffix in a PURL's subpath. | ||
| // | ||
| // Matches: | ||
| // - v4 - v4 | ||
| // - /v5/sdk/internal - v5 | ||
| // | ||
| // Does not match: | ||
| // - sdk/internal | ||
| // - /sdk/resourcemanager/iothub/armiothub | ||
| var goVersionSuffixRegexp = regexp.MustCompile(`^/?(v\d+)`) | ||
|
|
||
| // OSVMatcher implements the VulnerabilityMatcher interface with an osv.dev client. | ||
| // It sends out requests for every package version and does not perform caching. | ||
| type OSVMatcher struct { | ||
|
|
@@ -121,9 +134,24 @@ | |
|
|
||
| func pkgToQuery(pkg imodels.PackageInfo) *api.Query { | ||
| if pkg.Name() != "" && !pkg.Ecosystem().IsEmpty() && pkg.Version() != "" { | ||
| name := pkg.Name() | ||
|
|
||
| // Tools like Syft create Go PURLs where the module's major suffix is part | ||
| // of the subpath as opposed to the package name: | ||
| // | ||
| // pkg:golang/github.com/go-jose/[email protected]#v4 | ||
| // | ||
| // For a correct match we need to add the major suffix back | ||
| if pkg.Ecosystem().Ecosystem == osvconstants.EcosystemGo && pkg.PURL().Subpath != "" { | ||
| match := goVersionSuffixRegexp.FindStringSubmatch(pkg.PURL().Subpath) | ||
| if match != nil { | ||
| name += "/" + match[1] | ||
| } | ||
| } | ||
|
|
||
| return &api.Query{ | ||
| Package: &osvschema.Package{ | ||
| Name: pkg.Name(), | ||
| Name: name, | ||
| Ecosystem: pkg.Ecosystem().String(), | ||
| }, | ||
| Param: &api.Query_Version{ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a few examples about what type of url this is supposed to match, and what it is supposed to reject. (Just copying one of the links you posted as reject would be great!)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. I added it as a separate commit for easier review, I can squash it or you can do it when merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to squash it, it gets squashed on merge anyway.