Skip to content

Commit 035bfc0

Browse files
committed
internal/vulns: disable display of vulns for stdlib pages at master
The resolved version for stdlib pages at master is a v0-prefixed psuedoversion. The result is that all vulns for stdlib pages that begin at v0 are listed as affecting even though they may have been fixed in later versions of Go. This change removes vuln data for stdlib pages when requested version is master. Fixes golang/go#57327. Change-Id: Ie9df8cb25bc3126397a49af349d401d3a1e2e994 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/460817 Run-TryBot: Jamal Carvalho <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> TryBot-Result: kokoro <[email protected]>
1 parent 90d4957 commit 035bfc0

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

internal/vulns/vulns.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"golang.org/x/mod/semver"
1515
"golang.org/x/pkgsite/internal/derrors"
1616
"golang.org/x/pkgsite/internal/stdlib"
17+
"golang.org/x/pkgsite/internal/version"
1718
"golang.org/x/vuln/osv"
1819
)
1920

@@ -48,12 +49,19 @@ func VulnsForPackage(ctx context.Context, modulePath, version, packagePath strin
4849
return vs
4950
}
5051

51-
func vulnsForPackage(ctx context.Context, modulePath, version, packagePath string, getVulnEntries VulnEntriesFunc) (_ []Vuln, err error) {
52-
defer derrors.Wrap(&err, "vulns(%q, %q, %q)", modulePath, version, packagePath)
52+
func vulnsForPackage(ctx context.Context, modulePath, vers, packagePath string, getVulnEntries VulnEntriesFunc) (_ []Vuln, err error) {
53+
defer derrors.Wrap(&err, "vulns(%q, %q, %q)", modulePath, vers, packagePath)
5354

5455
if getVulnEntries == nil {
5556
return nil, nil
5657
}
58+
// Stdlib pages requested at master will map to a pseudo version that puts
59+
// all vulns in range. We can't really tell you're at master so version.IsPseudo
60+
// is the best we can do. The result is vulns won't be reported for a pseudoversion
61+
// that refers to a commit that is in a vulnerable range.
62+
if modulePath == stdlib.ModulePath && version.IsPseudo(vers) {
63+
return nil, nil
64+
}
5765
if modulePath == stdlib.ModulePath && strings.HasPrefix(packagePath, "cmd/go") {
5866
modulePath = vulnCmdGoModulePath
5967
} else if modulePath == stdlib.ModulePath {
@@ -68,7 +76,7 @@ func vulnsForPackage(ctx context.Context, modulePath, version, packagePath strin
6876
// package at this version.
6977
var vulns []Vuln
7078
for _, e := range entries {
71-
if vuln, ok := entryVuln(e, modulePath, packagePath, version); ok {
79+
if vuln, ok := entryVuln(e, modulePath, packagePath, vers); ok {
7280
vulns = append(vulns, vuln)
7381
}
7482
}
@@ -107,10 +115,10 @@ func (e OSVEntry) AffectedModulesAndPackages() []string {
107115
return affected
108116
}
109117

110-
func entryVuln(e *osv.Entry, modulePath, packagePath, version string) (Vuln, bool) {
118+
func entryVuln(e *osv.Entry, modulePath, packagePath, ver string) (Vuln, bool) {
111119
for _, a := range e.Affected {
112120
// a.Package.Name is Go "module" name. Go package path is a.EcosystemSpecific.Imports.Path.
113-
if a.Package.Name != modulePath || !a.Ranges.AffectsSemver(version) {
121+
if a.Package.Name != modulePath || !a.Ranges.AffectsSemver(ver) {
114122
continue
115123
}
116124
if packageMatches := func() bool {

internal/vulns/vulns_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,30 @@ func TestVulnsForPackage(t *testing.T) {
4444
},
4545
}},
4646
}
47+
stdlib := osv.Entry{
48+
ID: "GO-2",
49+
Affected: []osv.Affected{{
50+
Package: osv.Package{Name: "stdlib"},
51+
Ranges: []osv.AffectsRange{{
52+
Type: osv.TypeSemver,
53+
Events: []osv.RangeEvent{{Introduced: "0"}, {Fixed: "1.19.4"}},
54+
}},
55+
EcosystemSpecific: osv.EcosystemSpecific{
56+
Imports: []osv.EcosystemSpecificImport{{
57+
Path: "net/http",
58+
}},
59+
},
60+
}},
61+
}
4762

4863
get := func(_ context.Context, modulePath string) ([]*osv.Entry, error) {
4964
switch modulePath {
5065
case "good.com":
5166
return nil, nil
5267
case "bad.com", "unfixable.com":
5368
return []*osv.Entry{&e}, nil
69+
case "stdlib":
70+
return []*osv.Entry{&stdlib}, nil
5471
default:
5572
return nil, fmt.Errorf("unknown module %q", modulePath)
5673
}
@@ -89,6 +106,16 @@ func TestVulnsForPackage(t *testing.T) {
89106
{
90107
"unfixable.com", "", "v1.999.999", []Vuln{{ID: "GO-1"}},
91108
},
109+
// Vulns for stdlib
110+
{
111+
"std", "net/http", "go1.19.3", []Vuln{{ID: "GO-2"}},
112+
},
113+
{
114+
"std", "net/http", "v0.0.0-20230104211531-bae7d772e800", nil,
115+
},
116+
{
117+
"std", "net/http", "go1.20", nil,
118+
},
92119
}
93120
for _, tc := range testCases {
94121
got := VulnsForPackage(ctx, tc.mod, tc.version, tc.pkg, get)

0 commit comments

Comments
 (0)