Skip to content

Commit 24346ea

Browse files
authored
feat(govulncheck): add support for govulncheck (#43)
govulncheck only works on modules, so only the -mod and -repo-mod hooks were implemented. govulncheck doesn't (currently) display the folder when generating error messages, making the results confusing when running against multiple modules. Learn More: * https://pkg.go.dev/golang.org/x/vuln/cmd/govulncheck chore: Add support for custom printf template to announce module folders when running hooks that don't display what folder/module they are running against. docs: Update README for govulncheck docs: Update samples yaml files for govulncheck
1 parent bf2137d commit 24346ea

File tree

7 files changed

+92
-2
lines changed

7 files changed

+92
-2
lines changed

.pre-commit-hooks.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,41 @@
756756
description: "Run 'go vet [$ARGS] $FILE' for each staged .go file"
757757
pass_filenames: true
758758

759+
# ==============================================================================
760+
# go-vulncheck-mod
761+
# * Folder-Based
762+
# * Recursive
763+
# * Targets first parent folder with a go.mod file
764+
# * Executes if any .go files modified
765+
# * Executes if go.mod modified
766+
# ==============================================================================
767+
- id: go-vulncheck-mod
768+
name: 'go-vulncheck-mod'
769+
entry: go-vulncheck-mod.sh
770+
files: '(\.go$)|(\bgo\.mod$)'
771+
exclude: '(^|/)vendor/'
772+
language: 'script'
773+
description: "Run 'cd $(mod_root $FILE); govulncheck [$ARGS] ./...' for each staged .go file"
774+
pass_filenames: true
775+
require_serial: true
776+
777+
# ==============================================================================
778+
# go-vulncheck-repo-mod
779+
# * Repo-Based
780+
# * Recursive
781+
# * Targets ALL folders with a go.mod file
782+
# * Executes if any .go files modified
783+
# * Executes if go.mod modified
784+
# ==============================================================================
785+
- id: go-vulncheck-repo-mod
786+
name: 'go-vulncheck-repo-mod'
787+
entry: go-vulncheck-repo-mod.sh
788+
files: '(\.go$)|(\bgo\.mod$)'
789+
exclude: '(^|/)vendor/'
790+
language: 'script'
791+
description: "Run 'cd $(mod_root); govulncheck [$ARGS] ./...' for each module in the repo"
792+
pass_filenames: false
793+
759794
# ==============================================================================
760795
# golangci-lint-mod
761796
# * Folder-Based

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
6565
- id: go-vet-repo-mod
6666
- id: go-vet-repo-pkg
6767
#
68+
# Go Vulncheck
69+
# note: Only works with Go modules
70+
#
71+
- id: go-vulncheck-mod
72+
- id: go-vulncheck-repo-mod
73+
#
6874
# Revive
6975
#
7076
- id: go-revive
@@ -402,6 +408,7 @@ This can be useful, for example, for hooks that display warnings, but don't gene
402408
- Correctness Checkers
403409
- [go-test](#go-test)
404410
- [go-vet](#go-vet)
411+
- [go-vulncheck](#go-vulncheck)
405412
- [go-sec](#go-sec)
406413
- [go-staticcheck](#go-staticcheck)
407414
- [go-structslop](#go-structslop)
@@ -491,6 +498,28 @@ bingo install github.com/securego/gosec/v2/cmd/gosec
491498
- https://github.com/securego/gosec#usage
492499
- `gosec (no args)`
493500
501+
----------------
502+
### go-vulncheck
503+
Govulncheck reports known vulnerabilities that affect Go code. It uses static analysis of source code or a binary's symbol table to narrow down reports to only those that could affect the application.
504+
505+
Govulncheck is an official Go tool. It is developed and maintained by the Go security team (which is part of the official Go development team at Google) and backed by the official Go Vulnerability Database.
506+
507+
| Hook ID | Description |
508+
|-------------------------|----------------------------------------------------------------------------------|
509+
| `go-vulncheck-mod` | Run `'cd $(mod_root $FILE); govulncheck [$ARGS] ./...'` for each staged .go file |
510+
| `go-vulncheck-repo-mod` | Run `'cd $(mod_root); govulncheck [$ARGS] ./...'` for each module in the repo |
511+
512+
**NOTE:** Govulncheck only works with Go modules, hence only the `mod` hooks are implemented.
513+
514+
##### Install (via [bingo](https://github.com/TekWizely/bingo))
515+
```
516+
bingo install golang.org/x/vuln/cmd/govulncheck
517+
```
518+
519+
##### Help
520+
- https://pkg.go.dev/golang.org/x/vuln/cmd/govulncheck
521+
- `govulncheck -h`
522+
494523
------------------
495524
### go-staticcheck
496525
A state of the art linter for the Go programming language. Using static analysis, it finds bugs and performance issues, offers simplifications, and enforces style rules.

go-vulncheck-mod.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
cmd=(govulncheck)
3+
printf_module_announce="\nChecking Module: %s\n\n"
4+
. "$(dirname "${0}")/lib/cmd-mod.bash"

go-vulncheck-repo-mod.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
cmd=(govulncheck)
3+
printf_module_announce="\nChecking Module: %s\n\n"
4+
. "$(dirname "${0}")/lib/cmd-repo-mod.bash"

lib/cmd-mod.bash

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# shellcheck shell=bash
22

3+
: "${printf_module_announce:=}" # printf template: '%s'
4+
35
# shellcheck source=./common.bash
46
. "$(dirname "${0}")/lib/common.bash"
57

@@ -15,6 +17,10 @@ error_code=0
1517
# TODO Try to reduce the redundancy by generating the dirname's first
1618
for sub in $(find_module_roots "${FILES[@]}" | sort -u); do
1719
pushd "${sub}" > /dev/null || exit 1
20+
if [ -n "${printf_module_announce}" ]; then
21+
# shellcheck disable=SC2059 # Using variable as printf template
22+
printf -- "${printf_module_announce}" "${sub#./}"
23+
fi
1824
if [ "${error_on_output:-}" -eq 1 ]; then
1925
output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1)
2026
if [ -n "${output}" ]; then

lib/cmd-repo-mod.bash

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# shellcheck shell=bash
22

3+
: "${printf_module_announce:=}" # printf template: '%s'
4+
35
# shellcheck source=./common.bash
46
. "$(dirname "${0}")/lib/common.bash"
57

@@ -17,7 +19,11 @@ for file in $(find . -name go.mod | sort -u); do
1719
if is_path_ignored_by_dir_pattern "${file_dir}" || is_path_ignored_by_file_pattern "${file}" || is_path_ignored_by_pattern "${file}"; then
1820
continue
1921
fi
20-
pushd "${file_dir}" > /dev/null || exit 1
22+
pushd "${file_dir}" >/dev/null || exit 1
23+
if [ -n "${printf_module_announce}" ]; then
24+
# shellcheck disable=SC2059 # Using variable as printf template
25+
printf -- "${printf_module_announce}" "${file_dir#./}"
26+
fi
2127
if [ "${error_on_output:-}" -eq 1 ]; then
2228
output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1)
2329
if [ -n "${output}" ]; then
@@ -27,6 +33,6 @@ for file in $(find . -name go.mod | sort -u); do
2733
elif ! /usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}"; then
2834
error_code=1
2935
fi
30-
popd > /dev/null || exit 1
36+
popd >/dev/null || exit 1
3137
done
3238
exit $error_code

sample-config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ repos:
114114
- id: go-vet-repo-mod
115115
- id: go-vet-repo-pkg
116116
#
117+
# Go Vulncheck
118+
# note: Only works with Go modules
119+
#
120+
- id: go-vulncheck-mod
121+
- id: go-vulncheck-repo-mod
122+
#
117123
# Revive
118124
#
119125
- id: go-revive

0 commit comments

Comments
 (0)