Skip to content

Commit

Permalink
Add govulncheck-with-excludes.sh wrapper script
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Aug 3, 2023
1 parent 9c567ac commit 3d41717
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
skip-cache: true
-
name: Vulnerability scan
run: make vulncheck
run: make vulncheck-with-excludes
-
name: Coverage
uses: codecov/codecov-action@v2
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ vulncheck:
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...

.PHONY: vulncheck-with-excludes
vulncheck-with-excludes:
go install golang.org/x/vuln/cmd/govulncheck@latest
./bin/govulncheck-with-excludes.sh ./...

.PHONY: test
test:
go test -race -covermode=atomic -coverprofile=coverage.out ./...
Expand Down
67 changes: 67 additions & 0 deletions bin/govulncheck-with-excludes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env bash
set -Eeuo pipefail

# a wrapper / replacement for "govulncheck" which allows for excluding vulnerabilities
# (https://github.com/golang/go/issues/59507)

excludeVulns="$(jq -nc '[
# https://pkg.go.dev/vuln/GO-2023-1987
"GO-2023-1987",
empty # trailing comma hack (makes diffs smaller)
]')"
export excludeVulns

if ! command -v govulncheck > /dev/null; then
govulncheck() {
local user; user="$(id -u):$(id -g)"
local args=(
--rm --interactive --init
--user "$user"
--env HOME=/tmp
--env GOPATH=/tmp/go
--volume govulncheck:/tmp
--env CGO_ENABLED=0
--mount "type=bind,src=$PWD,dst=/wd,ro"
--workdir /wd
"${GOLANG_IMAGE:-golang:latest}"
sh -euc "
go install golang.org/x/vuln/cmd/govulncheck@latest > /dev/null
exec "$GOPATH/bin/govulncheck" "$@"
" --
)
docker run "${args[@]}" "$@"
}
fi

if out="$(govulncheck "$@")"; then
printf '%s\n' "$out"
exit 0
fi

json="$(govulncheck -json "$@")"

vulns="$(jq <<<"$json" -cs 'map(select(has("osv")) | .osv)')"
if [ "$(jq <<<"$vulns" -r 'length')" -le 0 ]; then
printf '%s\n' "$out"
exit 1
fi

filtered="$(jq <<<"$vulns" -c '
(env.excludeVulns | fromjson) as $exclude
| map(select(
.id as $id
| $exclude | index($id) | not
))
')"

text="$(jq <<<"$filtered" -r 'map("- \(.id) (aka \(.aliases | join(", ")))\n\n\t\(.details | gsub("\n"; "\n\t"))") | join("\n\n")')"

if [ -z "$text" ]; then
printf 'No vulnerabilities found.\n'
exit 0
else
printf '%s\n' "$text"
exit 1
fi

0 comments on commit 3d41717

Please sign in to comment.