Skip to content
Open
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
30 changes: 29 additions & 1 deletion internal/clients/clientimpl/osvmatcher/osvmatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"context"
"errors"
"net/http"
"regexp"

Check failure on line 7 in internal/clients/clientimpl/osvmatcher/osvmatcher.go

View workflow job for this annotation

GitHub Actions / golangci-lint

import 'regexp' is not allowed from list 'regexp': Use github.com/google/osv-scanner/v2/internal/cachedregexp instead (depguard)
"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"
Expand All @@ -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+)`)
Copy link
Collaborator

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

Copy link
Author

@alexg-axis alexg-axis Dec 1, 2025

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.

Copy link
Collaborator

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.


// 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 {
Expand Down Expand Up @@ -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{
Expand Down
Loading