Skip to content

Commit 825527a

Browse files
committed
cmd/vulnreport: add command vulnreport withdraw
Adds a command that makes it easier to withdraw an existing report. Usage: $ vulnreport -reason="..." withdraw NNN Change-Id: Iabe6c1a4b0d0ce15692bb6be743876a790dec437 Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/595996 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent ce4433b commit 825527a

File tree

5 files changed

+81
-7
lines changed

5 files changed

+81
-7
lines changed

cmd/vulnreport/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ var commands = map[string]command{
6060
"symbols": &symbolsCmd{},
6161
"osv": &osvCmd{},
6262
"unexclude": &unexclude{},
63+
"withdraw": &withdraw{},
6364
"xref": &xref{},
6465
}
6566

cmd/vulnreport/withdraw.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"context"
9+
"flag"
10+
"fmt"
11+
"time"
12+
13+
"golang.org/x/vulndb/internal/osv"
14+
"golang.org/x/vulndb/internal/report"
15+
)
16+
17+
var reason = flag.String("reason", "", "the reason this report is being withdrawn")
18+
19+
type withdraw struct {
20+
*fixer
21+
*filenameParser
22+
}
23+
24+
func (withdraw) name() string { return "withdraw" }
25+
26+
func (withdraw) usage() (string, string) {
27+
const desc = "withdraws a report"
28+
return filenameArgs, desc
29+
}
30+
31+
func (w *withdraw) setup(ctx context.Context, env environment) error {
32+
if *reason == "" {
33+
return fmt.Errorf("flag -reason must be provided")
34+
}
35+
w.fixer = new(fixer)
36+
w.filenameParser = new(filenameParser)
37+
return setupAll(ctx, env, w.fixer, w.filenameParser)
38+
}
39+
40+
func (w *withdraw) close() error {
41+
return nil
42+
}
43+
44+
func (w *withdraw) skip(input any) string {
45+
r := input.(*yamlReport)
46+
47+
if r.IsExcluded() {
48+
return "excluded; can't be withdrawn"
49+
}
50+
51+
if r.Withdrawn != nil {
52+
return "already withdrawn"
53+
}
54+
55+
if r.CVEMetadata != nil {
56+
return "withdrawing Go-published report not yet supported"
57+
}
58+
59+
return ""
60+
}
61+
62+
func (w *withdraw) run(ctx context.Context, input any) (err error) {
63+
r := input.(*yamlReport)
64+
r.Withdrawn = &osv.Time{Time: time.Now()}
65+
r.Summary = "WITHDRAWN: " + r.Summary
66+
r.Description = report.Description(
67+
fmt.Sprintf("(This report has been withdrawn with reason: %q). %s",
68+
*reason, r.Description))
69+
return w.fixAndWriteAll(ctx, r, false)
70+
}

internal/osv/time.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,11 @@ func (t *Time) UnmarshalJSON(data []byte) error {
3737
t.Time = time.UTC()
3838
return nil
3939
}
40+
41+
func (t Time) MarshalYAML() (interface{}, error) {
42+
if !t.IsZero() {
43+
return t.UTC().Format(time.RFC3339), nil
44+
}
45+
46+
return t, nil
47+
}

internal/report/osv.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,11 @@ func (r *Report) ToOSV(lastModified time.Time) (osv.Entry, error) {
131131
})
132132
}
133133

134-
var withdrawn *osv.Time
135-
if r.Withdrawn != nil {
136-
withdrawn = &osv.Time{Time: *r.Withdrawn}
137-
}
138-
139134
entry := osv.Entry{
140135
ID: r.ID,
141136
Published: osv.Time{Time: r.Published},
142137
Modified: osv.Time{Time: lastModified},
143-
Withdrawn: withdrawn,
138+
Withdrawn: r.Withdrawn,
144139
Related: r.Related,
145140
Summary: toParagraphs(r.Summary.String()),
146141
Credits: credits,

internal/report/report.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ type Report struct {
308308
// assigning a CVE ID ourselves, use CVEMetadata.Description instead.
309309
Description Description `yaml:",omitempty"`
310310
Published time.Time `yaml:",omitempty"`
311-
Withdrawn *time.Time `yaml:",omitempty"`
311+
Withdrawn *osv.Time `yaml:",omitempty"`
312312

313313
// CVE are CVE IDs for existing CVEs.
314314
// If we are assigning a CVE ID ourselves, use CVEMetadata.ID instead.

0 commit comments

Comments
 (0)