| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package misc |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"fmt" |
| 11 |
"sort" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/google/go-cmp/cmp" |
| 17 |
|
| 18 |
"golang.org/x/tools/gopls/internal/cache" |
| 19 |
"golang.org/x/tools/gopls/internal/protocol" |
| 20 |
"golang.org/x/tools/gopls/internal/protocol/command" |
| 21 |
"golang.org/x/tools/gopls/internal/server" |
| 22 |
"golang.org/x/tools/gopls/internal/test/compare" |
| 23 |
. "golang.org/x/tools/gopls/internal/test/integration" |
| 24 |
"golang.org/x/tools/gopls/internal/vulncheck" |
| 25 |
"golang.org/x/tools/gopls/internal/vulncheck/vulntest" |
| 26 |
) |
| 27 |
|
| 28 |
func TestRunGovulncheckError(t *testing.T) { |
| 29 |
const files = ` |
| 30 |
-- go.mod -- |
| 31 |
module mod.com |
| 32 |
|
| 33 |
go 1.12 |
| 34 |
-- foo.go -- |
| 35 |
package foo |
| 36 |
` |
| 37 |
Run(t, files, func(t *testing.T, env *Env) { |
| 38 |
cmd := command.NewRunGovulncheckCommand("Run Vulncheck Exp", command.VulncheckArgs{ |
| 39 |
URI: "/invalid/file/url", |
| 40 |
}) |
| 41 |
params := &protocol.ExecuteCommandParams{ |
| 42 |
Command: command.RunGovulncheck.String(), |
| 43 |
Arguments: cmd.Arguments, |
| 44 |
} |
| 45 |
|
| 46 |
var result any |
| 47 |
err := env.Editor.ExecuteCommand(env.Ctx, params, &result) |
| 48 |
|
| 49 |
if err == nil { |
| 50 |
t.Errorf("got success, want invalid file URL error. Result: %v", result) |
| 51 |
} |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
func TestVulncheckError(t *testing.T) { |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
const files = ` |
| 60 |
-- go.mod -- |
| 61 |
module mod.com |
| 62 |
|
| 63 |
go 1.12 |
| 64 |
-- foo.go -- |
| 65 |
package foo |
| 66 |
|
| 67 |
func F() { // build error incomplete |
| 68 |
` |
| 69 |
WithOptions( |
| 70 |
EnvVars{ |
| 71 |
"_GOPLS_TEST_BINARY_RUN_AS_GOPLS": "true", |
| 72 |
}, |
| 73 |
Settings{ |
| 74 |
"codelenses": map[string]bool{ |
| 75 |
"run_govulncheck": true, |
| 76 |
"vulncheck": true, |
| 77 |
}, |
| 78 |
}, |
| 79 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 80 |
env.OpenFile("go.mod") |
| 81 |
var result command.VulncheckResult |
| 82 |
err := env.Editor.ExecuteCodeLensCommand(env.Ctx, "go.mod", command.Vulncheck, &result) |
| 83 |
if err == nil { |
| 84 |
t.Fatalf("govulncheck succeeded unexpectedly: %v", result) |
| 85 |
} |
| 86 |
var ws WorkStatus |
| 87 |
env.Await( |
| 88 |
CompletedProgress(server.GoVulncheckCommandTitle, &ws), |
| 89 |
) |
| 90 |
wantEndMsg, wantMsgPart := "failed", "There are errors with the provided package patterns:" |
| 91 |
if ws.EndMsg != "failed" || !strings.Contains(ws.Msg, wantMsgPart) || !strings.Contains(err.Error(), wantMsgPart) { |
| 92 |
t.Errorf("work status = %+v, want {EndMessage: %q, Message: %q}", ws, wantEndMsg, wantMsgPart) |
| 93 |
} |
| 94 |
}) |
| 95 |
} |
| 96 |
|
| 97 |
const vulnsData = ` |
| 98 |
-- GO-2022-01.yaml -- |
| 99 |
modules: |
| 100 |
- module: golang.org/amod |
| 101 |
versions: |
| 102 |
- introduced: 1.0.0 |
| 103 |
- fixed: 1.0.4 |
| 104 |
packages: |
| 105 |
- package: golang.org/amod/avuln |
| 106 |
symbols: |
| 107 |
- VulnData.Vuln1 |
| 108 |
- VulnData.Vuln2 |
| 109 |
description: > |
| 110 |
vuln in amod is found |
| 111 |
summary: vuln in amod |
| 112 |
references: |
| 113 |
- href: pkg.go.dev/vuln/GO-2022-01 |
| 114 |
-- GO-2022-03.yaml -- |
| 115 |
modules: |
| 116 |
- module: golang.org/amod |
| 117 |
versions: |
| 118 |
- introduced: 1.0.0 |
| 119 |
- fixed: 1.0.6 |
| 120 |
packages: |
| 121 |
- package: golang.org/amod/avuln |
| 122 |
symbols: |
| 123 |
- nonExisting |
| 124 |
description: > |
| 125 |
unaffecting vulnerability is found |
| 126 |
summary: unaffecting vulnerability |
| 127 |
-- GO-2022-02.yaml -- |
| 128 |
modules: |
| 129 |
- module: golang.org/bmod |
| 130 |
packages: |
| 131 |
- package: golang.org/bmod/bvuln |
| 132 |
symbols: |
| 133 |
- Vuln |
| 134 |
description: | |
| 135 |
vuln in bmod is found. |
| 136 |
|
| 137 |
This is a long description |
| 138 |
of this vulnerability. |
| 139 |
summary: vuln in bmod (no fix) |
| 140 |
references: |
| 141 |
- href: pkg.go.dev/vuln/GO-2022-03 |
| 142 |
-- GO-2022-04.yaml -- |
| 143 |
modules: |
| 144 |
- module: golang.org/bmod |
| 145 |
packages: |
| 146 |
- package: golang.org/bmod/unused |
| 147 |
symbols: |
| 148 |
- Vuln |
| 149 |
description: | |
| 150 |
vuln in bmod/somethingelse is found |
| 151 |
summary: vuln in bmod/somethingelse |
| 152 |
references: |
| 153 |
- href: pkg.go.dev/vuln/GO-2022-04 |
| 154 |
-- GOSTDLIB.yaml -- |
| 155 |
modules: |
| 156 |
- module: stdlib |
| 157 |
versions: |
| 158 |
- introduced: 1.18.0 |
| 159 |
packages: |
| 160 |
- package: archive/zip |
| 161 |
symbols: |
| 162 |
- OpenReader |
| 163 |
summary: vuln in GOSTDLIB |
| 164 |
references: |
| 165 |
- href: pkg.go.dev/vuln/GOSTDLIB |
| 166 |
` |
| 167 |
|
| 168 |
func TestRunGovulncheckStd(t *testing.T) { |
| 169 |
const files = ` |
| 170 |
-- go.mod -- |
| 171 |
module mod.com |
| 172 |
|
| 173 |
go 1.19 |
| 174 |
-- main.go -- |
| 175 |
package main |
| 176 |
|
| 177 |
import ( |
| 178 |
"archive/zip" |
| 179 |
"fmt" |
| 180 |
) |
| 181 |
|
| 182 |
func main() { |
| 183 |
_, err := zip.OpenReader("file.zip") // vulnerability id: GOSTDLIB |
| 184 |
fmt.Println(err) |
| 185 |
} |
| 186 |
` |
| 187 |
|
| 188 |
db, err := vulntest.NewDatabase(context.Background(), []byte(vulnsData)) |
| 189 |
if err != nil { |
| 190 |
t.Fatal(err) |
| 191 |
} |
| 192 |
defer db.Clean() |
| 193 |
|
| 194 |
for _, legacy := range []bool{false, true} { |
| 195 |
t.Run(fmt.Sprintf("legacy=%v", legacy), func(t *testing.T) { |
| 196 |
lenses := map[string]bool{"vulncheck": !legacy, "run_govulncheck": legacy} |
| 197 |
WithOptions( |
| 198 |
EnvVars{ |
| 199 |
|
| 200 |
"GOVULNDB": db.URI(), |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
cache.GoVersionForVulnTest: "go1.19", |
| 205 |
"_GOPLS_TEST_BINARY_RUN_AS_GOPLS": "true", |
| 206 |
}, |
| 207 |
Settings{ |
| 208 |
"codelenses": lenses, |
| 209 |
}, |
| 210 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 211 |
env.OpenFile("go.mod") |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
var result *vulncheck.Result |
| 216 |
var expectation Expectation |
| 217 |
if legacy { |
| 218 |
var r command.RunVulncheckResult |
| 219 |
env.ExecuteCodeLensCommand("go.mod", command.RunGovulncheck, &r) |
| 220 |
expectation = CompletedProgressToken(r.Token, nil) |
| 221 |
} else { |
| 222 |
var r command.VulncheckResult |
| 223 |
env.ExecuteCodeLensCommand("go.mod", command.Vulncheck, &r) |
| 224 |
result = r.Result |
| 225 |
expectation = CompletedProgress(server.GoVulncheckCommandTitle, nil) |
| 226 |
} |
| 227 |
|
| 228 |
env.OnceMet( |
| 229 |
expectation, |
| 230 |
ShownMessage("Found GOSTDLIB"), |
| 231 |
NoDiagnostics(ForFile("go.mod")), |
| 232 |
) |
| 233 |
testFetchVulncheckResult(t, env, "go.mod", result, map[string]fetchVulncheckResult{ |
| 234 |
"go.mod": {IDs: []string{"GOSTDLIB"}, Mode: vulncheck.ModeGovulncheck}, |
| 235 |
}) |
| 236 |
}) |
| 237 |
}) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
func TestFetchVulncheckResultStd(t *testing.T) { |
| 242 |
const files = ` |
| 243 |
-- go.mod -- |
| 244 |
module mod.com |
| 245 |
|
| 246 |
go 1.18 |
| 247 |
-- main.go -- |
| 248 |
package main |
| 249 |
|
| 250 |
import ( |
| 251 |
"archive/zip" |
| 252 |
"fmt" |
| 253 |
) |
| 254 |
|
| 255 |
func main() { |
| 256 |
_, err := zip.OpenReader("file.zip") // vulnerability id: GOSTDLIB |
| 257 |
fmt.Println(err) |
| 258 |
} |
| 259 |
` |
| 260 |
|
| 261 |
db, err := vulntest.NewDatabase(context.Background(), []byte(vulnsData)) |
| 262 |
if err != nil { |
| 263 |
t.Fatal(err) |
| 264 |
} |
| 265 |
defer db.Clean() |
| 266 |
WithOptions( |
| 267 |
EnvVars{ |
| 268 |
|
| 269 |
"GOVULNDB": db.URI(), |
| 270 |
|
| 271 |
|
| 272 |
cache.GoVersionForVulnTest: "go1.18", |
| 273 |
"_GOPLS_TEST_BINARY_RUN_AS_GOPLS": "true", |
| 274 |
}, |
| 275 |
Settings{"ui.diagnostic.vulncheck": "Imports"}, |
| 276 |
).Run(t, files, func(t *testing.T, env *Env) { |
| 277 |
env.OpenFile("go.mod") |
| 278 |
env.AfterChange( |
| 279 |
NoDiagnostics(ForFile("go.mod")), |
| 280 |
|
| 281 |
) |
| 282 |
testFetchVulncheckResult(t, env, "", nil, map[string]fetchVulncheckResult{ |
| 283 |
"go.mod": { |
| 284 |
IDs: []string{"GOSTDLIB"}, |
| 285 |
Mode: vulncheck.ModeImports, |
| 286 |
}, |
| 287 |
}) |
| 288 |
}) |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
type fetchVulncheckResult struct { |
| 293 |
IDs []string |
| 294 |
Mode vulncheck.AnalysisMode |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
func testFetchVulncheckResult(t *testing.T, env *Env, runPath string, fromRun *vulncheck.Result, want map[string]fetchVulncheckResult) { |
| 313 |
t.Helper() |
| 314 |
|
| 315 |
var result map[protocol.DocumentURI]*vulncheck.Result |
| 316 |
fetchCmd := command.NewFetchVulncheckResultCommand("fetch", command.URIArg{ |
| 317 |
URI: env.Sandbox.Workdir.URI("go.mod"), |
| 318 |
}) |
| 319 |
env.ExecuteCommand(&protocol.ExecuteCommandParams{ |
| 320 |
Command: fetchCmd.Command, |
| 321 |
Arguments: fetchCmd.Arguments, |
| 322 |
}, &result) |
| 323 |
|
| 324 |
for _, v := range want { |
| 325 |
sort.Strings(v.IDs) |
| 326 |
} |
| 327 |
summarize := func(r *vulncheck.Result) fetchVulncheckResult { |
| 328 |
osv := map[string]bool{} |
| 329 |
for _, v := range r.Findings { |
| 330 |
osv[v.OSV] = true |
| 331 |
} |
| 332 |
ids := make([]string, 0, len(osv)) |
| 333 |
for id := range osv { |
| 334 |
ids = append(ids, id) |
| 335 |
} |
| 336 |
sort.Strings(ids) |
| 337 |
return fetchVulncheckResult{ |
| 338 |
IDs: ids, |
| 339 |
Mode: r.Mode, |
| 340 |
} |
| 341 |
} |
| 342 |
got := map[string]fetchVulncheckResult{} |
| 343 |
for k, r := range result { |
| 344 |
modfile := env.Sandbox.Workdir.RelPath(k.Path()) |
| 345 |
got[modfile] = summarize(r) |
| 346 |
} |
| 347 |
if fromRun != nil { |
| 348 |
if diff := cmp.Diff(want, got); diff != "" { |
| 349 |
t.Errorf("fetch vulncheck result = got %v, want %v: diff %v", got, want, diff) |
| 350 |
} |
| 351 |
if diff := cmp.Diff(summarize(fromRun), got[runPath]); diff != "" { |
| 352 |
t.Errorf("fetched vulncheck result differs from returned (-returned, +fetched):\n%s", diff) |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
const workspace1 = ` |
| 358 |
-- go.mod -- |
| 359 |
module golang.org/entry |
| 360 |
|
| 361 |
go 1.18 |
| 362 |
|
| 363 |
require golang.org/cmod v1.1.3 |
| 364 |
|
| 365 |
require ( |
| 366 |
golang.org/amod v1.0.0 // indirect |
| 367 |
golang.org/bmod v0.5.0 // indirect |
| 368 |
) |
| 369 |
-- x/x.go -- |
| 370 |
package x |
| 371 |
|
| 372 |
import ( |
| 373 |
"golang.org/cmod/c" |
| 374 |
"golang.org/entry/y" |
| 375 |
) |
| 376 |
|
| 377 |
func X() { |
| 378 |
c.C1().Vuln1() // vuln use: X -> Vuln1 |
| 379 |
} |
| 380 |
|
| 381 |
func CallY() { |
| 382 |
y.Y() // vuln use: CallY -> y.Y -> bvuln.Vuln |
| 383 |
} |
| 384 |
|
| 385 |
-- y/y.go -- |
| 386 |
package y |
| 387 |
|
| 388 |
import "golang.org/cmod/c" |
| 389 |
|
| 390 |
func Y() { |
| 391 |
c.C2()() // vuln use: Y -> bvuln.Vuln |
| 392 |
} |
| 393 |
` |
| 394 |
|
| 395 |
|
| 396 |
const proxy1 = ` |
| 397 |
-- golang.org/[email protected]/go.mod -- |
| 398 |
module golang.org/cmod |
| 399 |
|
| 400 |
go 1.12 |
| 401 |
-- golang.org/[email protected]/c/c.go -- |
| 402 |
package c |
| 403 |
|
| 404 |
import ( |
| 405 |
"golang.org/amod/avuln" |
| 406 |
"golang.org/bmod/bvuln" |
| 407 |
) |
| 408 |
|
| 409 |
type I interface { |
| 410 |
Vuln1() |
| 411 |
} |
| 412 |
|
| 413 |
func C1() I { |
| 414 |
v := avuln.VulnData{} |
| 415 |
v.Vuln2() // vuln use |
| 416 |
return v |
| 417 |
} |
| 418 |
|
| 419 |
func C2() func() { |
| 420 |
return bvuln.Vuln |
| 421 |
} |
| 422 |
-- golang.org/[email protected]/go.mod -- |
| 423 |
module golang.org/amod |
| 424 |
|
| 425 |
go 1.14 |
| 426 |
-- golang.org/[email protected]/avuln/avuln.go -- |
| 427 |
package avuln |
| 428 |
|
| 429 |
type VulnData struct {} |
| 430 |
func (v VulnData) Vuln1() {} |
| 431 |
func (v VulnData) Vuln2() {} |
| 432 |
-- golang.org/[email protected]/go.mod -- |
| 433 |
module golang.org/amod |
| 434 |
|
| 435 |
go 1.14 |
| 436 |
-- golang.org/[email protected]/avuln/avuln.go -- |
| 437 |
package avuln |
| 438 |
|
| 439 |
type VulnData struct {} |
| 440 |
func (v VulnData) Vuln1() {} |
| 441 |
func (v VulnData) Vuln2() {} |
| 442 |
|
| 443 |
-- golang.org/[email protected]/go.mod -- |
| 444 |
module golang.org/bmod |
| 445 |
|
| 446 |
go 1.14 |
| 447 |
-- golang.org/[email protected]/bvuln/bvuln.go -- |
| 448 |
package bvuln |
| 449 |
|
| 450 |
func Vuln() { |
| 451 |
// something evil |
| 452 |
} |
| 453 |
-- golang.org/[email protected]/unused/unused.go -- |
| 454 |
package unused |
| 455 |
|
| 456 |
func Vuln() { |
| 457 |
// something evil |
| 458 |
} |
| 459 |
-- golang.org/[email protected]/go.mod -- |
| 460 |
module golang.org/amod |
| 461 |
|
| 462 |
go 1.14 |
| 463 |
-- golang.org/[email protected]/avuln/avuln.go -- |
| 464 |
package avuln |
| 465 |
|
| 466 |
type VulnData struct {} |
| 467 |
func (v VulnData) Vuln1() {} |
| 468 |
func (v VulnData) Vuln2() {} |
| 469 |
` |
| 470 |
|
| 471 |
func vulnTestEnv(proxyData string) (*vulntest.DB, []RunOption, error) { |
| 472 |
db, err := vulntest.NewDatabase(context.Background(), []byte(vulnsData)) |
| 473 |
if err != nil { |
| 474 |
return nil, nil, nil |
| 475 |
} |
| 476 |
settings := Settings{ |
| 477 |
"codelenses": map[string]bool{ |
| 478 |
"run_govulncheck": true, |
| 479 |
}, |
| 480 |
} |
| 481 |
ev := EnvVars{ |
| 482 |
|
| 483 |
"GOVULNDB": db.URI(), |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
cache.GoVersionForVulnTest: "go1.18", |
| 488 |
"_GOPLS_TEST_BINARY_RUN_AS_GOPLS": "true", |
| 489 |
"GOSUMDB": "off", |
| 490 |
} |
| 491 |
return db, []RunOption{ProxyFiles(proxyData), ev, settings, WriteGoSum(".")}, nil |
| 492 |
} |
| 493 |
|
| 494 |
func TestRunVulncheckPackageDiagnostics(t *testing.T) { |
| 495 |
db, opts0, err := vulnTestEnv(proxy1) |
| 496 |
if err != nil { |
| 497 |
t.Fatal(err) |
| 498 |
} |
| 499 |
defer db.Clean() |
| 500 |
|
| 501 |
checkVulncheckDiagnostics := func(env *Env, t *testing.T) { |
| 502 |
env.OpenFile("go.mod") |
| 503 |
|
| 504 |
gotDiagnostics := &protocol.PublishDiagnosticsParams{} |
| 505 |
env.AfterChange( |
| 506 |
Diagnostics(env.AtRegexp("go.mod", `golang.org/amod`)), |
| 507 |
ReadDiagnostics("go.mod", gotDiagnostics), |
| 508 |
) |
| 509 |
|
| 510 |
testFetchVulncheckResult(t, env, "", nil, map[string]fetchVulncheckResult{ |
| 511 |
"go.mod": { |
| 512 |
IDs: []string{"GO-2022-01", "GO-2022-02", "GO-2022-03"}, |
| 513 |
Mode: vulncheck.ModeImports, |
| 514 |
}, |
| 515 |
}) |
| 516 |
|
| 517 |
wantVulncheckDiagnostics := map[string]vulnDiagExpectation{ |
| 518 |
"golang.org/amod": { |
| 519 |
diagnostics: []vulnDiag{ |
| 520 |
{ |
| 521 |
msg: "golang.org/amod has known vulnerabilities GO-2022-01, GO-2022-03.", |
| 522 |
severity: protocol.SeverityInformation, |
| 523 |
source: string(cache.Vulncheck), |
| 524 |
codeActions: []string{ |
| 525 |
"Run govulncheck to verify", |
| 526 |
"Upgrade to v1.0.6", |
| 527 |
"Upgrade to latest", |
| 528 |
}, |
| 529 |
}, |
| 530 |
}, |
| 531 |
codeActions: []string{ |
| 532 |
"Run govulncheck to verify", |
| 533 |
"Upgrade to v1.0.6", |
| 534 |
"Upgrade to latest", |
| 535 |
}, |
| 536 |
hover: []string{"GO-2022-01", "Fixed in v1.0.4.", "GO-2022-03"}, |
| 537 |
}, |
| 538 |
"golang.org/bmod": { |
| 539 |
diagnostics: []vulnDiag{ |
| 540 |
{ |
| 541 |
msg: "golang.org/bmod has a vulnerability GO-2022-02.", |
| 542 |
severity: protocol.SeverityInformation, |
| 543 |
source: string(cache.Vulncheck), |
| 544 |
codeActions: []string{ |
| 545 |
"Run govulncheck to verify", |
| 546 |
}, |
| 547 |
}, |
| 548 |
}, |
| 549 |
codeActions: []string{ |
| 550 |
"Run govulncheck to verify", |
| 551 |
}, |
| 552 |
hover: []string{"GO-2022-02", "vuln in bmod (no fix)", "No fix is available."}, |
| 553 |
}, |
| 554 |
} |
| 555 |
|
| 556 |
for pattern, want := range wantVulncheckDiagnostics { |
| 557 |
modPathDiagnostics := testVulnDiagnostics(t, env, pattern, want, gotDiagnostics) |
| 558 |
|
| 559 |
gotActions := env.CodeActionForFile("go.mod", modPathDiagnostics) |
| 560 |
if diff := diffCodeActions(gotActions, want.codeActions); diff != "" { |
| 561 |
t.Errorf("code actions for %q do not match, got %v, want %v\n%v\n", pattern, gotActions, want.codeActions, diff) |
| 562 |
continue |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
wantNoVulncheckDiagnostics := func(env *Env, t *testing.T) { |
| 568 |
env.OpenFile("go.mod") |
| 569 |
|
| 570 |
gotDiagnostics := &protocol.PublishDiagnosticsParams{} |
| 571 |
env.AfterChange( |
| 572 |
ReadDiagnostics("go.mod", gotDiagnostics), |
| 573 |
) |
| 574 |
|
| 575 |
if len(gotDiagnostics.Diagnostics) > 0 { |
| 576 |
t.Errorf("Unexpected diagnostics: %v", stringify(gotDiagnostics)) |
| 577 |
} |
| 578 |
testFetchVulncheckResult(t, env, "", nil, map[string]fetchVulncheckResult{}) |
| 579 |
} |
| 580 |
|
| 581 |
for _, tc := range []struct { |
| 582 |
name string |
| 583 |
setting Settings |
| 584 |
wantDiagnostics bool |
| 585 |
}{ |
| 586 |
{"imports", Settings{"ui.diagnostic.vulncheck": "Imports"}, true}, |
| 587 |
{"default", Settings{}, false}, |
| 588 |
{"invalid", Settings{"ui.diagnostic.vulncheck": "invalid"}, false}, |
| 589 |
} { |
| 590 |
t.Run(tc.name, func(t *testing.T) { |
| 591 |
|
| 592 |
opts := append(opts0, tc.setting) |
| 593 |
WithOptions(opts...).Run(t, workspace1, func(t *testing.T, env *Env) { |
| 594 |
|
| 595 |
|
| 596 |
if tc.wantDiagnostics { |
| 597 |
checkVulncheckDiagnostics(env, t) |
| 598 |
} else { |
| 599 |
wantNoVulncheckDiagnostics(env, t) |
| 600 |
} |
| 601 |
|
| 602 |
if tc.name == "imports" && tc.wantDiagnostics { |
| 603 |
|
| 604 |
var result command.RunVulncheckResult |
| 605 |
env.ExecuteCodeLensCommand("go.mod", command.RunGovulncheck, &result) |
| 606 |
gotDiagnostics := &protocol.PublishDiagnosticsParams{} |
| 607 |
env.OnceMet( |
| 608 |
CompletedProgressToken(result.Token, nil), |
| 609 |
ShownMessage("Found"), |
| 610 |
) |
| 611 |
env.OnceMet( |
| 612 |
Diagnostics(env.AtRegexp("go.mod", "golang.org/bmod")), |
| 613 |
ReadDiagnostics("go.mod", gotDiagnostics), |
| 614 |
) |
| 615 |
|
| 616 |
count := 0 |
| 617 |
for _, diag := range gotDiagnostics.Diagnostics { |
| 618 |
if strings.Contains(diag.Message, "GO-2022-02") { |
| 619 |
count++ |
| 620 |
if got, want := diag.Severity, protocol.SeverityWarning; got != want { |
| 621 |
t.Errorf("Diagnostic for GO-2022-02 = %v, want %v", got, want) |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
if count != 1 { |
| 626 |
t.Errorf("Unexpected number of diagnostics about GO-2022-02 = %v, want 1:\n%+v", count, stringify(gotDiagnostics)) |
| 627 |
} |
| 628 |
} |
| 629 |
}) |
| 630 |
}) |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
func TestRunGovulncheck_Expiry(t *testing.T) { |
| 637 |
|
| 638 |
defer func(prev time.Duration) { |
| 639 |
cache.MaxGovulncheckResultAge = prev |
| 640 |
}(cache.MaxGovulncheckResultAge) |
| 641 |
cache.MaxGovulncheckResultAge = 99 * time.Millisecond |
| 642 |
|
| 643 |
db, opts0, err := vulnTestEnv(proxy1) |
| 644 |
if err != nil { |
| 645 |
t.Fatal(err) |
| 646 |
} |
| 647 |
defer db.Clean() |
| 648 |
|
| 649 |
WithOptions(opts0...).Run(t, workspace1, func(t *testing.T, env *Env) { |
| 650 |
env.OpenFile("go.mod") |
| 651 |
env.OpenFile("x/x.go") |
| 652 |
|
| 653 |
var result command.RunVulncheckResult |
| 654 |
env.ExecuteCodeLensCommand("go.mod", command.RunGovulncheck, &result) |
| 655 |
env.OnceMet( |
| 656 |
CompletedProgressToken(result.Token, nil), |
| 657 |
ShownMessage("Found"), |
| 658 |
) |
| 659 |
|
| 660 |
time.Sleep(100 * time.Millisecond) |
| 661 |
|
| 662 |
env.RegexpReplace("x/x.go", "package x", "package x ") |
| 663 |
env.AfterChange( |
| 664 |
NoDiagnostics(env.AtRegexp("go.mod", "golang.org/bmod")), |
| 665 |
) |
| 666 |
}) |
| 667 |
} |
| 668 |
|
| 669 |
func stringify(a any) string { |
| 670 |
data, _ := json.Marshal(a) |
| 671 |
return string(data) |
| 672 |
} |
| 673 |
|
| 674 |
func TestRunVulncheckWarning(t *testing.T) { |
| 675 |
db, opts, err := vulnTestEnv(proxy1) |
| 676 |
if err != nil { |
| 677 |
t.Fatal(err) |
| 678 |
} |
| 679 |
defer db.Clean() |
| 680 |
WithOptions(opts...).Run(t, workspace1, func(t *testing.T, env *Env) { |
| 681 |
env.OpenFile("go.mod") |
| 682 |
|
| 683 |
var result command.RunVulncheckResult |
| 684 |
env.ExecuteCodeLensCommand("go.mod", command.RunGovulncheck, &result) |
| 685 |
gotDiagnostics := &protocol.PublishDiagnosticsParams{} |
| 686 |
env.OnceMet( |
| 687 |
CompletedProgressToken(result.Token, nil), |
| 688 |
ShownMessage("Found"), |
| 689 |
) |
| 690 |
|
| 691 |
env.OnceMet( |
| 692 |
Diagnostics(env.AtRegexp("go.mod", `golang.org/amod`)), |
| 693 |
ReadDiagnostics("go.mod", gotDiagnostics), |
| 694 |
) |
| 695 |
|
| 696 |
testFetchVulncheckResult(t, env, "go.mod", nil, map[string]fetchVulncheckResult{ |
| 697 |
|
| 698 |
"go.mod": {IDs: []string{"GO-2022-01", "GO-2022-02", "GO-2022-03", "GO-2022-04"}, Mode: vulncheck.ModeGovulncheck}, |
| 699 |
}) |
| 700 |
env.OpenFile("x/x.go") |
| 701 |
env.OpenFile("y/y.go") |
| 702 |
wantDiagnostics := map[string]vulnDiagExpectation{ |
| 703 |
"golang.org/amod": { |
| 704 |
applyAction: "Upgrade to v1.0.6", |
| 705 |
diagnostics: []vulnDiag{ |
| 706 |
{ |
| 707 |
msg: "golang.org/amod has a vulnerability used in the code: GO-2022-01.", |
| 708 |
severity: protocol.SeverityWarning, |
| 709 |
source: string(cache.Govulncheck), |
| 710 |
codeActions: []string{ |
| 711 |
"Upgrade to v1.0.4", |
| 712 |
"Upgrade to latest", |
| 713 |
"Reset govulncheck result", |
| 714 |
}, |
| 715 |
}, |
| 716 |
{ |
| 717 |
msg: "golang.org/amod has a vulnerability GO-2022-03 that is not used in the code.", |
| 718 |
severity: protocol.SeverityInformation, |
| 719 |
source: string(cache.Govulncheck), |
| 720 |
codeActions: []string{ |
| 721 |
"Upgrade to v1.0.6", |
| 722 |
"Upgrade to latest", |
| 723 |
"Reset govulncheck result", |
| 724 |
}, |
| 725 |
}, |
| 726 |
}, |
| 727 |
codeActions: []string{ |
| 728 |
"Upgrade to v1.0.6", |
| 729 |
"Upgrade to latest", |
| 730 |
"Reset govulncheck result", |
| 731 |
}, |
| 732 |
hover: []string{"GO-2022-01", "Fixed in v1.0.4.", "GO-2022-03"}, |
| 733 |
}, |
| 734 |
"golang.org/bmod": { |
| 735 |
diagnostics: []vulnDiag{ |
| 736 |
{ |
| 737 |
msg: "golang.org/bmod has a vulnerability used in the code: GO-2022-02.", |
| 738 |
severity: protocol.SeverityWarning, |
| 739 |
source: string(cache.Govulncheck), |
| 740 |
codeActions: []string{ |
| 741 |
"Reset govulncheck result", |
| 742 |
}, |
| 743 |
}, |
| 744 |
}, |
| 745 |
codeActions: []string{ |
| 746 |
"Reset govulncheck result", |
| 747 |
}, |
| 748 |
hover: []string{"GO-2022-02", "vuln in bmod (no fix)", "No fix is available."}, |
| 749 |
}, |
| 750 |
} |
| 751 |
|
| 752 |
for mod, want := range wantDiagnostics { |
| 753 |
modPathDiagnostics := testVulnDiagnostics(t, env, mod, want, gotDiagnostics) |
| 754 |
|
| 755 |
|
| 756 |
gotActions := env.CodeActionForFile("go.mod", modPathDiagnostics) |
| 757 |
if diff := diffCodeActions(gotActions, want.codeActions); diff != "" { |
| 758 |
t.Errorf("code actions for %q do not match, expected %v, got %v\n%v\n", mod, want.codeActions, gotActions, diff) |
| 759 |
continue |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
if want.applyAction == "" { |
| 764 |
continue |
| 765 |
} |
| 766 |
for _, action := range gotActions { |
| 767 |
if action.Title == want.applyAction { |
| 768 |
env.ApplyCodeAction(action) |
| 769 |
break |
| 770 |
} |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
env.Await(env.DoneWithChangeWatchedFiles()) |
| 775 |
wantGoMod := `module golang.org/entry |
| 776 |
|
| 777 |
go 1.18 |
| 778 |
|
| 779 |
require golang.org/cmod v1.1.3 |
| 780 |
|
| 781 |
require ( |
| 782 |
golang.org/amod v1.0.6 // indirect |
| 783 |
golang.org/bmod v0.5.0 // indirect |
| 784 |
) |
| 785 |
` |
| 786 |
if got := env.BufferText("go.mod"); got != wantGoMod { |
| 787 |
t.Fatalf("go.mod vulncheck fix failed:\n%s", compare.Text(wantGoMod, got)) |
| 788 |
} |
| 789 |
}) |
| 790 |
} |
| 791 |
|
| 792 |
func diffCodeActions(gotActions []protocol.CodeAction, want []string) string { |
| 793 |
var gotTitles []string |
| 794 |
for _, ca := range gotActions { |
| 795 |
gotTitles = append(gotTitles, ca.Title) |
| 796 |
} |
| 797 |
return cmp.Diff(want, gotTitles) |
| 798 |
} |
| 799 |
|
| 800 |
const workspace2 = ` |
| 801 |
-- go.mod -- |
| 802 |
module golang.org/entry |
| 803 |
|
| 804 |
go 1.18 |
| 805 |
|
| 806 |
require golang.org/bmod v0.5.0 |
| 807 |
|
| 808 |
-- x/x.go -- |
| 809 |
package x |
| 810 |
|
| 811 |
import "golang.org/bmod/bvuln" |
| 812 |
|
| 813 |
func F() { |
| 814 |
// Calls a benign func in bvuln. |
| 815 |
bvuln.OK() |
| 816 |
} |
| 817 |
` |
| 818 |
|
| 819 |
const proxy2 = ` |
| 820 |
-- golang.org/[email protected]/bvuln/bvuln.go -- |
| 821 |
package bvuln |
| 822 |
|
| 823 |
func Vuln() {} // vulnerable. |
| 824 |
func OK() {} // ok. |
| 825 |
` |
| 826 |
|
| 827 |
func TestGovulncheckInfo(t *testing.T) { |
| 828 |
db, opts, err := vulnTestEnv(proxy2) |
| 829 |
if err != nil { |
| 830 |
t.Fatal(err) |
| 831 |
} |
| 832 |
defer db.Clean() |
| 833 |
WithOptions(opts...).Run(t, workspace2, func(t *testing.T, env *Env) { |
| 834 |
env.OpenFile("go.mod") |
| 835 |
var result command.RunVulncheckResult |
| 836 |
env.ExecuteCodeLensCommand("go.mod", command.RunGovulncheck, &result) |
| 837 |
gotDiagnostics := &protocol.PublishDiagnosticsParams{} |
| 838 |
env.OnceMet( |
| 839 |
CompletedProgressToken(result.Token, nil), |
| 840 |
ShownMessage("No vulnerabilities found"), |
| 841 |
) |
| 842 |
|
| 843 |
|
| 844 |
env.OnceMet( |
| 845 |
Diagnostics(env.AtRegexp("go.mod", "golang.org/bmod")), |
| 846 |
ReadDiagnostics("go.mod", gotDiagnostics), |
| 847 |
) |
| 848 |
|
| 849 |
testFetchVulncheckResult(t, env, "go.mod", nil, map[string]fetchVulncheckResult{ |
| 850 |
"go.mod": {IDs: []string{"GO-2022-02", "GO-2022-04"}, Mode: vulncheck.ModeGovulncheck}, |
| 851 |
}) |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
wantDiagnostics := map[string]vulnDiagExpectation{ |
| 856 |
"golang.org/bmod": { |
| 857 |
diagnostics: []vulnDiag{ |
| 858 |
{ |
| 859 |
msg: "golang.org/bmod has a vulnerability GO-2022-02 that is not used in the code.", |
| 860 |
severity: protocol.SeverityInformation, |
| 861 |
source: string(cache.Govulncheck), |
| 862 |
codeActions: []string{ |
| 863 |
"Reset govulncheck result", |
| 864 |
}, |
| 865 |
}, |
| 866 |
}, |
| 867 |
codeActions: []string{ |
| 868 |
"Reset govulncheck result", |
| 869 |
}, |
| 870 |
hover: []string{"GO-2022-02", "vuln in bmod (no fix)", "No fix is available."}, |
| 871 |
}, |
| 872 |
} |
| 873 |
|
| 874 |
var allActions []protocol.CodeAction |
| 875 |
for mod, want := range wantDiagnostics { |
| 876 |
modPathDiagnostics := testVulnDiagnostics(t, env, mod, want, gotDiagnostics) |
| 877 |
|
| 878 |
gotActions := env.CodeActionForFile("go.mod", modPathDiagnostics) |
| 879 |
allActions = append(allActions, gotActions...) |
| 880 |
if diff := diffCodeActions(gotActions, want.codeActions); diff != "" { |
| 881 |
t.Errorf("code actions for %q do not match, expected %v, got %v\n%v\n", mod, want.codeActions, gotActions, diff) |
| 882 |
continue |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
var reset protocol.CodeAction |
| 888 |
for _, a := range allActions { |
| 889 |
if a.Title == "Reset govulncheck result" { |
| 890 |
reset = a |
| 891 |
break |
| 892 |
} |
| 893 |
} |
| 894 |
if reset.Title != "Reset govulncheck result" { |
| 895 |
t.Errorf("failed to find a 'Reset govulncheck result' code action, got %v", allActions) |
| 896 |
} |
| 897 |
env.ApplyCodeAction(reset) |
| 898 |
|
| 899 |
env.Await(NoDiagnostics(ForFile("go.mod"))) |
| 900 |
}) |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
func testVulnDiagnostics(t *testing.T, env *Env, pattern string, want vulnDiagExpectation, got *protocol.PublishDiagnosticsParams) []protocol.Diagnostic { |
| 906 |
t.Helper() |
| 907 |
loc := env.RegexpSearch("go.mod", pattern) |
| 908 |
var modPathDiagnostics []protocol.Diagnostic |
| 909 |
for _, w := range want.diagnostics { |
| 910 |
|
| 911 |
var diag *protocol.Diagnostic |
| 912 |
for _, g := range got.Diagnostics { |
| 913 |
if g.Range.Start == loc.Range.Start && w.msg == g.Message { |
| 914 |
modPathDiagnostics = append(modPathDiagnostics, g) |
| 915 |
diag = &g |
| 916 |
break |
| 917 |
} |
| 918 |
} |
| 919 |
if diag == nil { |
| 920 |
t.Errorf("no diagnostic at %q matching %q found\n", pattern, w.msg) |
| 921 |
continue |
| 922 |
} |
| 923 |
if diag.Severity != w.severity || diag.Source != w.source { |
| 924 |
t.Errorf("incorrect (severity, source) for %q, want (%s, %s) got (%s, %s)\n", w.msg, w.severity, w.source, diag.Severity, diag.Source) |
| 925 |
} |
| 926 |
|
| 927 |
gotActions := env.CodeActionForFile("go.mod", []protocol.Diagnostic{*diag}) |
| 928 |
if diff := diffCodeActions(gotActions, w.codeActions); diff != "" { |
| 929 |
t.Errorf("code actions for %q do not match, want %v, got %v\n%v\n", w.msg, w.codeActions, gotActions, diff) |
| 930 |
continue |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
if len(want.hover) > 0 { |
| 935 |
hover, _ := env.Hover(loc) |
| 936 |
for _, part := range want.hover { |
| 937 |
if !strings.Contains(hover.Value, part) { |
| 938 |
t.Errorf("hover contents for %q do not match, want %v, got %v\n", pattern, strings.Join(want.hover, ","), hover.Value) |
| 939 |
break |
| 940 |
} |
| 941 |
} |
| 942 |
} |
| 943 |
return modPathDiagnostics |
| 944 |
} |
| 945 |
|
| 946 |
type vulnRelatedInfo struct { |
| 947 |
Filename string |
| 948 |
Line uint32 |
| 949 |
Message string |
| 950 |
} |
| 951 |
|
| 952 |
type vulnDiag struct { |
| 953 |
msg string |
| 954 |
severity protocol.DiagnosticSeverity |
| 955 |
|
| 956 |
|
| 957 |
codeActions []string |
| 958 |
|
| 959 |
|
| 960 |
relatedInfo []vulnRelatedInfo |
| 961 |
|
| 962 |
source string |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
type vulnDiagExpectation struct { |
| 969 |
|
| 970 |
|
| 971 |
applyAction string |
| 972 |
|
| 973 |
|
| 974 |
diagnostics []vulnDiag |
| 975 |
|
| 976 |
|
| 977 |
codeActions []string |
| 978 |
|
| 979 |
|
| 980 |
hover []string |
| 981 |
} |