[vuln] cmd/govulncheck: HTML output

80 views
Skip to first unread message

Jonathan Amsterdam (Gerrit)

unread,
Mar 24, 2022, 12:19:20 PM3/24/22
to Julie Qiu, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Julie Qiu.

Jonathan Amsterdam would like Julie Qiu to review this change.

View Change

cmd/govulncheck: HTML output

If the -html flag is provided, print HTML to standard out.

The HTML is similar to the default text output, except
full call stacks are available by opening detail elements.

Change-Id: I7ea9bfeb6f9cd43b66e8a659c2b61a6b2e5a95a3
---
A cmd/govulncheck/html.go
M cmd/govulncheck/main.go
2 files changed, 144 insertions(+), 15 deletions(-)

diff --git a/cmd/govulncheck/html.go b/cmd/govulncheck/html.go
new file mode 100644
index 0000000..70a4105
--- /dev/null
+++ b/cmd/govulncheck/html.go
@@ -0,0 +1,112 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build go1.18
+// +build go1.18
+
+package main
+
+import (
+ "fmt"
+ "html/template"
+ "io"
+
+ "golang.org/x/vuln/vulncheck"
+)
+
+func html(w io.Writer, r *vulncheck.Result, callStacks map[*vulncheck.Vuln][]vulncheck.CallStack, moduleVersions map[string]string, topPackages map[string]bool, vulnGroups [][]*vulncheck.Vuln) error {
+ tmpl, err := template.New("").Funcs(template.FuncMap{
+ "funcName": funcName,
+ }).Parse(templateSource)
+ if err != nil {
+ return err
+ }
+
+ type vuln struct {
+ PkgPath string
+ CurrentVersion string
+ FixedVersion string
+ Reference string
+ Details string
+ }
+
+ type callstack struct {
+ Summary string
+ Stack vulncheck.CallStack
+ }
+
+ type callstacks struct {
+ ID string // osv.Entry ID
+ Stacks []callstack
+ }
+
+ data := struct {
+ Vulns []vuln
+ CallStacks []callstacks
+ }{}
+
+ for _, vg := range vulnGroups {
+ v0 := vg[0]
+ data.Vulns = append(data.Vulns, vuln{
+ PkgPath: v0.PkgPath,
+ CurrentVersion: moduleVersions[v0.ModPath],
+ FixedVersion: "v" + latestFixed(v0.OSV.Affected),
+ Reference: fmt.Sprintf("https://pkg.go.dev/vuln/%s", v0.OSV.ID),
+ Details: v0.OSV.Details,
+ })
+ // Keep first call stack for each vuln.
+ stacks := callstacks{ID: v0.OSV.ID}
+ for _, v := range vg {
+ if css := callStacks[v]; len(css) > 0 {
+ stacks.Stacks = append(stacks.Stacks, callstack{
+ Summary: summarizeCallStack(css[0], topPackages, v.PkgPath),
+ Stack: css[0],
+ })
+ }
+ }
+ data.CallStacks = append(data.CallStacks, stacks)
+ }
+ return tmpl.Execute(w, data)
+}
+
+var templateSource = `
+<!DOCTYPE html>
+<html lang="en">
+<meta charset="utf-8">
+<title>govulncheck Results</title>
+
+<body>
+ {{with .Vulns}}
+ <h2>Vulnerabilities</h2>
+ <table>
+ <tr><th>Package</th><th>Your Version</th><th>Fixed Version</th><th>Reference</th><th>Details</th><tr>
+ {{range .Vulns}}
+ <tr>
+ <td>{[.PkgPath}}</td>
+ <td>{{.CurrentVersion}}</td>
+ <td>{{.FixedVersion}}</td>
+ <td>{{.Reference}}</td>
+ <td>{{.Details}}</td>
+ </tr>
+ {{end}}
+ </table>
+
+ <h2>Call Stacks</h2>
+ {{range .CallStacks}}
+ <h3>.ID</h3>
+ {{range .Stacks}}
+ <details>
+ <summary>{{.Summary}}</summary>
+ {{range .Stack}}
+ <p>{{.Function | funcName}}</p>
+ {{end}}
+ </details>
+ {{end}}
+ {{end}}
+ {{else}}
+ No vulnerabilities found.
+ {{end}}
+</body>
+</html>
+`
diff --git a/cmd/govulncheck/main.go b/cmd/govulncheck/main.go
index 63bc149..896392d 100644
--- a/cmd/govulncheck/main.go
+++ b/cmd/govulncheck/main.go
@@ -40,6 +40,7 @@
jsonFlag = flag.Bool("json", false, "")
verboseFlag = flag.Bool("v", false, "")
testsFlag = flag.Bool("tests", false, "")
+ htmlFlag = flag.Bool("html", false, "")
)

const usage = `govulncheck: identify known vulnerabilities by call graph traversal.
@@ -54,6 +55,8 @@

-json Print vulnerability findings in JSON format.

+ -html Generate HTML with the vulnerability findings.
+
-tags Comma-separated list of build tags.

-tests Boolean flag indicating if test files should be analyzed too.
@@ -135,7 +138,20 @@
if *jsonFlag {
writeJSON(r)
} else {
- writeText(r, pkgs, moduleVersions)
+ callStacks := vulncheck.CallStacks(r)
+ // Create set of top-level packages, used to find representative symbols
+ topPackages := map[string]bool{}
+ for _, p := range pkgs {
+ topPackages[p.PkgPath] = true
+ }
+ vulnGroups := groupByIDAndPackage(r.Vulns)
+ if *htmlFlag {
+ if err := html(os.Stdout, r, callStacks, moduleVersions, topPackages, vulnGroups); err != nil {
+ die("writing HTML: %v", err)
+ }
+ } else {
+ writeText(r, callStacks, moduleVersions, topPackages, vulnGroups)
+ }
}
exitCode := 0
// Following go vet, fail with 3 if there are findings (in this case, vulns).
@@ -154,24 +170,11 @@
fmt.Println()
}

-func writeText(r *vulncheck.Result, pkgs []*packages.Package, moduleVersions map[string]string) {
- if len(r.Vulns) == 0 {
- return
- }
- callStacks := vulncheck.CallStacks(r)
-
+func writeText(r *vulncheck.Result, callStacks map[*vulncheck.Vuln][]vulncheck.CallStack, moduleVersions map[string]string, topPackages map[string]bool, vulnGroups [][]*vulncheck.Vuln) {
const labelWidth = 16
line := func(label, text string) {
fmt.Printf("%-*s%s\n", labelWidth, label, text)
}
-
- // Create set of top-level packages, used to find
- // representative symbols
- topPackages := map[string]bool{}
- for _, p := range pkgs {
- topPackages[p.PkgPath] = true
- }
- vulnGroups := groupByIDAndPackage(r.Vulns)
for _, vg := range vulnGroups {
// All the vulns in vg have the same PkgPath, ModPath and OSV.
// All have a non-zero CallSink.

To view, visit change 395554. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: vuln
Gerrit-Branch: master
Gerrit-Change-Id: I7ea9bfeb6f9cd43b66e8a659c2b61a6b2e5a95a3
Gerrit-Change-Number: 395554
Gerrit-PatchSet: 1
Gerrit-Owner: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Julie Qiu <ju...@golang.org>
Gerrit-Attention: Julie Qiu <ju...@golang.org>
Gerrit-MessageType: newchange

Julie Qiu (Gerrit)

unread,
Mar 25, 2022, 8:16:20 AM3/25/22
to Jonathan Amsterdam, goph...@pubsubhelper.golang.org, Gopher Robot, golang-co...@googlegroups.com

Attention is currently required from: Jonathan Amsterdam.

Patch set 1:Code-Review +2

View Change

1 comment:

To view, visit change 395554. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: vuln
Gerrit-Branch: master
Gerrit-Change-Id: I7ea9bfeb6f9cd43b66e8a659c2b61a6b2e5a95a3
Gerrit-Change-Number: 395554
Gerrit-PatchSet: 1
Gerrit-Owner: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Julie Qiu <ju...@golang.org>
Gerrit-Attention: Jonathan Amsterdam <j...@google.com>
Gerrit-Comment-Date: Fri, 25 Mar 2022 12:16:16 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Jonathan Amsterdam (Gerrit)

unread,
Mar 25, 2022, 10:04:29 AM3/25/22
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Julie Qiu.

Jonathan Amsterdam uploaded patch set #2 to this change.

View Change

cmd/govulncheck: HTML output

If the -html flag is provided, print HTML to standard out.

The HTML is similar to the default text output, except
full call stacks are available by opening detail elements.

Change-Id: I7ea9bfeb6f9cd43b66e8a659c2b61a6b2e5a95a3
---
A cmd/govulncheck/html.go
M cmd/govulncheck/main.go
2 files changed, 147 insertions(+), 18 deletions(-)

To view, visit change 395554. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: vuln
Gerrit-Branch: master
Gerrit-Change-Id: I7ea9bfeb6f9cd43b66e8a659c2b61a6b2e5a95a3
Gerrit-Change-Number: 395554
Gerrit-PatchSet: 2
Gerrit-Owner: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Julie Qiu <ju...@golang.org>
Gerrit-Attention: Julie Qiu <ju...@golang.org>
Gerrit-MessageType: newpatchset

Jonathan Amsterdam (Gerrit)

unread,
Mar 25, 2022, 10:04:30 AM3/25/22
to goph...@pubsubhelper.golang.org, Julie Qiu, Gopher Robot, golang-co...@googlegroups.com

Attention is currently required from: Julie Qiu.

View Change

1 comment:

  • File cmd/govulncheck/main.go:

    • Done

To view, visit change 395554. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: vuln
Gerrit-Branch: master
Gerrit-Change-Id: I7ea9bfeb6f9cd43b66e8a659c2b61a6b2e5a95a3
Gerrit-Change-Number: 395554
Gerrit-PatchSet: 2
Gerrit-Owner: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Julie Qiu <ju...@golang.org>
Gerrit-Attention: Julie Qiu <ju...@golang.org>
Gerrit-Comment-Date: Fri, 25 Mar 2022 14:04:26 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Julie Qiu <ju...@golang.org>
Gerrit-MessageType: comment

Jonathan Amsterdam (Gerrit)

unread,
Mar 25, 2022, 10:05:51 AM3/25/22
to goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Julie Qiu, Gopher Robot, golang-co...@googlegroups.com

Jonathan Amsterdam submitted this change.

View Change



1 is the latest approved patch-set.
The change was submitted with unreviewed changes in the following files:

```
The name of the file: cmd/govulncheck/main.go
Insertions: 4, Deletions: 4.

@@ -53,13 +53,13 @@

Flags:

- -json Print vulnerability findings in JSON format.
+ -json Print vulnerability findings in JSON format.

- -html Generate HTML with the vulnerability findings.
+ -html Generate HTML with the vulnerability findings.

- -tags Comma-separated list of build tags.

+ -tags Comma-separated list of build tags.

-	-tests     Boolean flag indicating if test files should be analyzed too.
+ -tests Boolean flag indicating if test files should be analyzed too.

govulncheck can be used with either one or more package patterns (i.e. golang.org/x/crypto/...
or ./...) or with a single path to a Go binary. In the latter case module and symbol
```

Approvals: Julie Qiu: Looks good to me, approved Jonathan Amsterdam: Trusted; Run TryBots
cmd/govulncheck: HTML output

If the -html flag is provided, print HTML to standard out.

The HTML is similar to the default text output, except
full call stacks are available by opening detail elements.

Change-Id: I7ea9bfeb6f9cd43b66e8a659c2b61a6b2e5a95a3
Reviewed-on: https://go-review.googlesource.com/c/vuln/+/395554
Trust: Jonathan Amsterdam <j...@google.com>
Run-TryBot: Jonathan Amsterdam <j...@google.com>
Reviewed-by: Julie Qiu <ju...@golang.org>

---
A cmd/govulncheck/html.go
M cmd/govulncheck/main.go
2 files changed, 151 insertions(+), 18 deletions(-)

index 63bc149..05327d8 100644

--- a/cmd/govulncheck/main.go
+++ b/cmd/govulncheck/main.go
@@ -40,6 +40,7 @@
jsonFlag = flag.Bool("json", false, "")
verboseFlag = flag.Bool("v", false, "")
testsFlag = flag.Bool("tests", false, "")
+ htmlFlag = flag.Bool("html", false, "")
)

const usage = `govulncheck: identify known vulnerabilities by call graph traversal.
@@ -52,11 +53,13 @@

Flags:

- -json Print vulnerability findings in JSON format.
+ -json Print vulnerability findings in JSON format.

- -tags Comma-separated list of build tags.

+ -html Generate HTML with the vulnerability findings.

-	-tests     Boolean flag indicating if test files should be analyzed too.

+ -tags Comma-separated list of build tags.
+
+ -tests Boolean flag indicating if test files should be analyzed too.

govulncheck can be used with either one or more package patterns (i.e. golang.org/x/crypto/...
or ./...) or with a single path to a Go binary. In the latter case module and symbol

To view, visit change 395554. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: vuln
Gerrit-Branch: master
Gerrit-Change-Id: I7ea9bfeb6f9cd43b66e8a659c2b61a6b2e5a95a3
Gerrit-Change-Number: 395554
Gerrit-PatchSet: 3
Gerrit-Owner: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: Julie Qiu <ju...@golang.org>
Gerrit-MessageType: merged
Reply all
Reply to author
Forward
0 new messages