Go Vulnerability Management

repository·master·Indexed 19 days ago

https://github.com/golang/vuln

Tooling to analyze Go codebases and binaries for known vulnerabilities in dependencies using the Go vulnerability database. Includes govulncheck, a CLI tool that identifies vulnerabilities in functions actually invoked by the code, and a client for accessing vulnerability data via JSON endpoints at https://vuln.go.dev.

Tokens
1.8K
Snippets
10
Records
11
Agent score
66%

What's inside golang/vuln

  1. Install govulncheck

    master

    You can install the latest version of govulncheck using the go install command. This tool allows you to analyze your codebase and binaries to identify known vulnerabilities in your dependencies, specifically focusing on vulnerabilities in functions that your code actually calls to reduce noise.

    go install golang.org/x/vuln/cmd/govulncheck@latest
  2. Access the Go Vulnerability Database via JSON endpoints

    master

    The Go vulnerability database is hosted at https://vuln.go.dev and provides data in JSON format.

    Warning: Do not rely on the YAML files in the x/vulndb repository, as they use an internal format that may change without notice. Instead, use the supported JSON endpoints provided below.

    Endpoint Variables:

    • $base: The base URL https://vuln.go.dev.
    • $module: A specific module path (e.g., golang.org/x/crypto).
    • $vuln: A Go vulnerability ID (e.g., GO-2021-1234).
  3. Run govulncheck on your module

    master

    Once installed, navigate to your Go module's directory and run govulncheck followed by the package pattern you wish to analyze. Using ./... will scan all packages in the current module.

    govulncheck ./...
  4. Reference: Go Vulnerability Database JSON endpoints

    master

    The following JSON endpoints are available for accessing vulnerability data. Note that these paths and formats are provisional and subject to change.

    PathDescription
    $base/index.jsonList of module paths in the database mapped to their last modified timestamp.
    $base/$module.jsonList of vulnerability entries for a specific module.
    $base/ID/index.jsonList of all vulnerability entries in the database.
    $base/ID/$vuln.jsonAn individual Go vulnerability report.
    | Path | Description |
    | :--- | :--- |
    | `$base/index.json` | List of module paths in the database mapped to its last modified timestamp ([link](https://vuln.go.dev/index.json)). |
    | `$base/$module.json` | List of vulnerability entries for that module ([example](https://vuln.go.dev/golang.org/x/crypto.json)). |
    | `$base/ID/index.json` | List of all the vulnerability entries in the database |
    | `$base/ID/$vuln.json` | An individual Go vulnerability report |
  5. Initialize a Client with NewInMemoryClient

    master

    Use NewInMemoryClient to create a client from a slice of *osv.Entry objects. This is useful for testing or working with transient vulnerability data in memory.

    import (
    	"golang.org/x/vuln/internal/client"
    	"golang.org/x/vuln/internal/osv"
    )
    
    client, err := client.NewInMemoryClient([]*osv.Entry{
    	// your OSV entries here
    })
  6. Query vulnerabilities by modules with ByModules

    master

    The ByModules method retrieves vulnerability information for a list of module requests. It returns a slice of *ModuleResponse objects, preserving the original order of the requests.

    Even if no vulnerabilities are found for a specific request, a response is still returned with a nil Entries field.

    import (
    	"context"
    	"golang.org/x/vuln/internal/client"
    )
    
    reqs := []*client.ModuleRequest{
    	{
    		Path:    "example.com/module",
    		Version: "v1.2.3", // Optional: filter by version
    	},
    	{
    		Path: "another.com/pkg",
    		// Version omitted to get all vulnerabilities for this module
    	},
    }
    
    responses, err := c.ByModules(ctx, reqs)
    for _, resp := range responses {
    	fmt.Printf("Module: %s, Version: %s, Vulns: %d\n", resp.Path, resp.Version, len(resp.Entries))
    }
  7. Get the last modified time with LastModifiedTime

    master

    The LastModifiedTime method returns the timestamp indicating when the vulnerability database was last updated. This is useful for determining if a local cache needs to be refreshed.

    time, err := c.LastModifiedTime(ctx)
    if err != nil {
    	// handle error
    }
    fmt.Printf("Database last updated: %v\n", time)
  8. Initialize a Client with NewClient

    master

    Use NewClient to create a client for reading vulnerability databases. The source argument must be a URL prefixed with http, https, or file.

    • HTTP/HTTPS: Connects to a remote vulnerability database (e.g., https://vuln.go.dev).
    • File: Connects to a local directory containing the vulnerability database.

    You can optionally provide Options to configure the underlying http.Client.

    import "golang.org/x/vuln/internal/client"
    
    // For a remote database
    client, err := client.NewClient("https://vuln.go.dev", &client.Options{})
    
    // For a local database
    client, err := client.NewClient("file:///path/to/vulndb", nil)
  9. ModuleRequest configuration

    master

    The ModuleRequest struct defines the criteria for searching vulnerabilities for a specific module.

    FieldTypeDescription
    PathstringThe module path to filter on. Required.
    Versionstring(Optional) If set, only return vulnerabilities affecting this specific version. Must be a valid semver.
    type ModuleRequest struct {
    	Path    string
    	Version string
    }
  10. Use the govulncheck CLI

    master

    The govulncheck tool is the command-line entrypoint for scanning Go projects for known vulnerabilities. It uses the golang.org/x/vuln/scan package to execute scanning logic. The tool reports errors to stderr and exits with a non-zero status code if vulnerabilities are found or if an error occurs during execution.

    # Example usage (actual flags and arguments are defined in the scan package)
    govulncheck ./...