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 f8fe7bf
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
run: make vulncheck
-
name: Coverage
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
flags: unittests
Expand Down Expand Up @@ -189,6 +189,7 @@ jobs:
uses: ludeeus/action-shellcheck@master
with:
ignore_paths: 'bin/tests/libs'
ignore_names: govulncheck-with-excludes.sh
-
name: Setup bats
uses: mig4/setup-bats@v1
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ lint: install-linter
.PHONY: vulncheck
vulncheck:
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...
./bin/govulncheck-with-excludes.sh ./...

.PHONY: test
test:
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 f8fe7bf

Please sign in to comment.