blob: 90220ead0c8bf596c57bfadf9c82db93926faff4 [file] [log] [blame]
// 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.
// This program runs govulncheck on a module or a binary and then
// writes the result as JSON. It is intended to be run in a sandbox.
// Unless it panics, this program always terminates with exit code 0.
// If there is an error, it writes a JSON object with field "Error".
// Otherwise, it writes a internal/govulncheck.SandboxResponse as JSON.
package main
import (
// main function for govulncheck sandbox that accepts four inputs
// in the following order:
// - path to govulncheck
// - govulncheck mode
// - input module or binary to analyze
// - full path to the vulnerability database
func main() {
run(os.Stdout, flag.Args())
func run(w io.Writer, args []string) {
fail := func(err error) {
fmt.Fprintf(w, `{"Error": %q}`, err)
if len(args) != 4 {
fail(errors.New("need four args: govulncheck path, mode, input module dir or binary, full path to vuln db"))
mode := args[1]
if !worker.IsValidGovulncheckMode(mode) {
fail(fmt.Errorf("%q is not a valid mode", mode))
resp, err := runGovulncheck(args[0], mode, args[2], args[3])
if err != nil {
b, err := json.MarshalIndent(resp, "", "\t")
if err != nil {
fail(fmt.Errorf("json.MarshalIndent: %v", err))
func runGovulncheck(govulncheckPath, mode, filePath, vulnDBDir string) (*govulncheck.SandboxResponse, error) {
response := govulncheck.SandboxResponse{
Stats: govulncheck.ScanStats{},
findings, err := govulncheck.RunGovulncheckCmd(govulncheckPath, mode, filePath, vulnDBDir, &response.Stats)
if err != nil {
return nil, err
response.Findings = findings
return &response, nil