ci: label merged Dependabot security updates for backport (#32806)
## Problem Dependabot security fixes merged to `main` are never backported. None of the 388 Dependabot PRs since #24484 got the `backport` label. Three defects cause this: 1. `fetch-metadata` reads only the oldest 100 of the repo's 596 alerts, so `alert-state` is always empty. 2. It compares versions in a format that doesn't match the alert data (`= v0.4.0` for Go). 3. `dependabot.yaml` enables auto-merge with `GITHUB_TOKEN`, and a merge made with that token starts no workflows. Even a labeled PR wouldn't reach `backport.yaml`. The label step also runs in a Dependabot `pull_request` run, which can't use the `main` environment or a KMS App token (the signing pool requires a protected ref). `pull_request_target` is not an option because it is being removed before 2026-12-02. ## Fix - `backport.yaml`, `detect` job: also run for merged Dependabot PRs that aren't labeled yet. Get the backporter App token (environment `main`, same KMS key as the `backport` job) and run `.github/scripts/dependabot-alert-match.sh`. If the PR fixed an alert, add `backport` with the App token. That `labeled` event runs the normal backport. The script runs from the base branch and never checks out PR code. - `dependabot-alert-match.sh`: matches the PR's updated dependencies against open or fixed alerts by package, by manifest among the PR's changed files, and by the update crossing the alert's first patched version (old < patched <= new). Paginates, and normalizes a leading `v`. - `dependabot.yaml`: remove `fetch-metadata`, the label step that never ran, and auto-merge. The job now only sends the Slack notice. **A person now merges Dependabot PRs**, as they already approve them since #32303. ## Verification - `bash .github/scripts/dependabot-alert-match.test.sh` passes. - Replaying the script against the last 120 merged Dependabot PRs matches 12. That includes dompurify #31213, next #31217, storybook #31215 and #31216, axios #30239, and moby/sys/user #30534. It doesn't match grpc #31954 or cel-go #31959, whose alerts were fixed earlier. ## Before merging The App needs the "Dependabot alerts: read" permission first (ENT-263, coder/github-org-terraform manifest PR). Until then the token step fails for unlabeled Dependabot PRs. Runs for manually labeled PRs are unaffected. Known limits: a prerelease version compares by `sort -V`, and a grouped PR's alerts are matched per dependency. Tracked in ENT-245. Not in this PR: the audit of past fixes (ENT-270) and a fallback for lockfile conflicts (ENT-271). --------- Co-authored-by: Lisa Fiander <lisa@coder.com>
This commit is contained in:
1 parent
9a33a25f14
commit
4a60d186f8
4 files changed
+485
-56
No files matched your search
Executable
+135
@@ -0,0 +1,135 @@
|
||||
#!/usr/bin/env bash
|
||||
# Reports whether a merged Dependabot PR fixed a Dependabot alert, so
|
||||
# backport.yaml can label it for backport.
|
||||
#
|
||||
# A PR fixes an alert when, for one of its updated dependencies:
|
||||
# - the alert is for the same package,
|
||||
# - the alert's manifest is one of the files the PR changed, and
|
||||
# - the update crossed the alert's first patched version: the old version is
|
||||
# below it and the new version is at or above it.
|
||||
#
|
||||
# Open alerts count too, because GitHub may not have closed the alert yet when
|
||||
# the PR merges. Comparing against the first patched version avoids parsing
|
||||
# vulnerable version ranges. Requiring the old version to be below it skips
|
||||
# alerts that were already fixed before the PR.
|
||||
#
|
||||
# Inputs (environment):
|
||||
# PR_NUMBER the merged pull request
|
||||
# REPO owner/name, defaults to GITHUB_REPOSITORY
|
||||
# GH_TOKEN token that can read pull requests and Dependabot alerts
|
||||
#
|
||||
# Writes matched=true|false and alerts=<comma-separated alert numbers> to
|
||||
# GITHUB_OUTPUT when it is set. Outside Actions it also prints the matched
|
||||
# alerts. Tests: dependabot-alert-match.test.sh.
|
||||
#
|
||||
# To see which alerts a PR matched, run it locally. The Actions log is public
|
||||
# and intentionally doesn't say. You need a gh token that can read Dependabot
|
||||
# alerts:
|
||||
# PR_NUMBER=<n> REPO=coder/coder bash .github/scripts/dependabot-alert-match.sh
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
: "${PR_NUMBER:?PR_NUMBER is required}"
|
||||
REPO="${REPO:-${GITHUB_REPOSITORY:?REPO or GITHUB_REPOSITORY is required}}"
|
||||
|
||||
# log_local_only MESSAGE: prints only outside Actions. Actions logs are public,
|
||||
# and these lines say which bump fixed which alert before release branches are
|
||||
# patched, so in Actions the result goes to GITHUB_OUTPUT alone.
|
||||
log_local_only() {
|
||||
if [[ -z "${GITHUB_ACTIONS:-}" ]]; then
|
||||
echo "$1"
|
||||
fi
|
||||
}
|
||||
|
||||
# version_le A B: succeeds when version A <= version B. A leading "v" is
|
||||
# ignored, because Go alerts and Dependabot commit messages disagree on it.
|
||||
version_le() {
|
||||
local a="${1#v}" b="${2#v}"
|
||||
printf '%s\n%s\n' "${a}" "${b}" | sort -V -C
|
||||
}
|
||||
|
||||
# version_lt A B: succeeds when version A < B.
|
||||
version_lt() {
|
||||
[[ "${1#v}" != "${2#v}" ]] && version_le "$1" "$2"
|
||||
}
|
||||
|
||||
# Dependabot's commit lists every updated dependency in a YAML trailer with
|
||||
# the new version:
|
||||
# - dependency-name: dompurify
|
||||
# dependency-version: 3.4.17
|
||||
# The old version is only in the body, as "Bumps [name](url) from A to B." or,
|
||||
# in grouped PRs, "Updates `name` from A to B".
|
||||
message=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}/commits" --jq '.[0].commit.message')
|
||||
deps=$(awk '
|
||||
{ gsub(/["\047]/, "") }
|
||||
/^- dependency-name: / { name = $3 }
|
||||
/^ dependency-version: / && name != "" { print name "\t" $2; name = "" }
|
||||
' <<<"${message}")
|
||||
|
||||
olds=$(awk '
|
||||
/^Bumps \[/ {
|
||||
name = $0; sub(/^Bumps \[/, "", name); sub(/\].*/, "", name)
|
||||
if (match($0, /\) from [^ ]+ to /)) { split(substr($0, RSTART + 7), v, " "); print name "\t" v[1] }
|
||||
}
|
||||
/^Updates `/ {
|
||||
name = $0; sub(/^Updates `/, "", name); sub(/`.*/, "", name)
|
||||
if (match($0, /` from [^ ]+ to /)) { split(substr($0, RSTART + 7), v, " "); print name "\t" v[1] }
|
||||
}
|
||||
' <<<"${message}")
|
||||
|
||||
# An empty result below means the input was incomplete, not that nothing
|
||||
# matched. Fail so the job is visibly red instead of reporting matched=false
|
||||
# and skipping a backport.
|
||||
if [[ -z "${deps}" ]]; then
|
||||
echo "::error::No updated-dependencies metadata in the first commit of PR #${PR_NUMBER}."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
files=$(gh api --paginate "repos/${REPO}/pulls/${PR_NUMBER}/files" --jq '.[].filename')
|
||||
if [[ -z "${files}" ]]; then
|
||||
echo "::error::PR #${PR_NUMBER} lists no changed files."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
matched=()
|
||||
while IFS=$'\t' read -r name version; do
|
||||
[[ -z "${name}" ]] && continue
|
||||
old=$(awk -F'\t' -v n="${name}" '$1 == n { print $2; exit }' <<<"${olds}")
|
||||
if [[ -z "${old}" ]]; then
|
||||
echo "::warning::No old version for ${name} in PR #${PR_NUMBER}; matching on the new version only."
|
||||
fi
|
||||
# Assigned first, not read through < <(...): set -e ignores a failure in a
|
||||
# process substitution, and a 403 or rate limit would look like no alerts.
|
||||
alert_rows=$(
|
||||
gh api --paginate -X GET "repos/${REPO}/dependabot/alerts" \
|
||||
-f state=open,fixed -f package="${name}" -f per_page=100 \
|
||||
--jq '.[] | [.number, .dependency.manifest_path, (.security_vulnerability.first_patched_version.identifier // "")] | @tsv'
|
||||
)
|
||||
while IFS=$'\t' read -r number manifest patched; do
|
||||
[[ -z "${number}" || -z "${patched}" ]] && continue
|
||||
grep -qxF -- "${manifest}" <<<"${files}" || continue
|
||||
version_le "${patched}" "${version}" || continue
|
||||
if [[ -n "${old}" ]] && ! version_lt "${old}" "${patched}"; then
|
||||
continue
|
||||
fi
|
||||
log_local_only "Alert #${number}: ${name} ${old:-?} to ${version} fixes ${manifest} (patched in ${patched})."
|
||||
matched+=("${number}")
|
||||
done <<<"${alert_rows}"
|
||||
done <<<"${deps}"
|
||||
|
||||
result=false
|
||||
if ((${#matched[@]} > 0)); then
|
||||
result=true
|
||||
fi
|
||||
alerts=$(
|
||||
IFS=,
|
||||
echo "${matched[*]:-}"
|
||||
)
|
||||
log_local_only "matched=${result} alerts=${alerts}"
|
||||
|
||||
if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
|
||||
{
|
||||
echo "matched=${result}"
|
||||
echo "alerts=${alerts}"
|
||||
} >>"${GITHUB_OUTPUT}"
|
||||
fi
|
||||
+270
@@ -0,0 +1,270 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for dependabot-alert-match.sh. No CI job runs this file.
|
||||
#
|
||||
# Run: bash .github/scripts/dependabot-alert-match.test.sh
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/dependabot-alert-match.sh"
|
||||
WORK=$(mktemp -d)
|
||||
trap 'rm -rf "${WORK}"' EXIT
|
||||
|
||||
# A gh stub that serves fixtures from ${WORK}. The script passes --jq, so the
|
||||
# fixtures are the already-filtered output:
|
||||
# commit-message the first commit's message
|
||||
# files the PR's changed files, one per line
|
||||
# alerts.<package> number<TAB>manifest<TAB>first patched version
|
||||
# alerts-fail when present, the alerts call fails like a 403
|
||||
mkdir -p "${WORK}/bin"
|
||||
cat >"${WORK}/bin/gh" <<'STUB'
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
args="$*"
|
||||
case "${args}" in
|
||||
*/commits*) cat "${FIXTURES}/commit-message" ;;
|
||||
*/files*) cat "${FIXTURES}/files" ;;
|
||||
*/dependabot/alerts*)
|
||||
[[ -f "${FIXTURES}/alerts-fail" ]] && {
|
||||
echo "gh: HTTP 403" >&2
|
||||
exit 1
|
||||
}
|
||||
pkg=""
|
||||
while (($#)); do
|
||||
if [[ "$1" == "-f" && "$2" == package=* ]]; then
|
||||
pkg="${2#package=}"
|
||||
fi
|
||||
shift
|
||||
done
|
||||
f="${FIXTURES}/alerts.${pkg//\//_}"
|
||||
[[ -f "${f}" ]] && cat "${f}"
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
echo "unexpected gh call: ${args}" >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
STUB
|
||||
chmod +x "${WORK}/bin/gh"
|
||||
|
||||
failures=0
|
||||
case_name=""
|
||||
OUT=""
|
||||
|
||||
# begin <name>: starts a case with empty fixtures.
|
||||
begin() {
|
||||
case_name="$1"
|
||||
rm -rf "${WORK}/fixtures"
|
||||
mkdir -p "${WORK}/fixtures"
|
||||
: >"${WORK}/fixtures/files"
|
||||
}
|
||||
|
||||
# commit <name> <old> <new>...: sets a Dependabot commit message with one
|
||||
# dependency per name/old/new triple.
|
||||
commit() {
|
||||
local args=("$@")
|
||||
{
|
||||
echo "chore: bump things"
|
||||
echo
|
||||
if ((${#args[@]} == 3)); then
|
||||
echo "Bumps [${args[0]}](https://example.com) from ${args[1]} to ${args[2]}."
|
||||
else
|
||||
echo "Bumps the group with ${#args[@]} updates."
|
||||
for ((i = 0; i < ${#args[@]}; i += 3)); do
|
||||
echo "Updates \`${args[i]}\` from ${args[i + 1]} to ${args[i + 2]}"
|
||||
done
|
||||
fi
|
||||
echo
|
||||
echo "---"
|
||||
echo "updated-dependencies:"
|
||||
for ((i = 0; i < ${#args[@]}; i += 3)); do
|
||||
echo "- dependency-name: ${args[i]}"
|
||||
echo " dependency-version: ${args[i + 2]}"
|
||||
echo " dependency-type: direct:production"
|
||||
done
|
||||
echo "..."
|
||||
} >"${WORK}/fixtures/commit-message"
|
||||
}
|
||||
|
||||
# run: runs the script and captures GITHUB_OUTPUT in OUT.
|
||||
run() {
|
||||
: >"${WORK}/output"
|
||||
FIXTURES="${WORK}/fixtures" PATH="${WORK}/bin:${PATH}" \
|
||||
PR_NUMBER=1 REPO=coder/coder GITHUB_OUTPUT="${WORK}/output" GITHUB_ACTIONS="${IN_ACTIONS:-}" \
|
||||
bash "${SCRIPT}" >"${WORK}/stdout" 2>"${WORK}/stderr" || {
|
||||
echo "FAIL: ${case_name}: script exited non-zero"
|
||||
sed 's/^/ stderr: /' "${WORK}/stderr"
|
||||
failures=$((failures + 1))
|
||||
return
|
||||
}
|
||||
OUT=$(<"${WORK}/output")
|
||||
}
|
||||
|
||||
# run_fails: runs the script and expects a non-zero exit and no output file
|
||||
# content, so the job fails visibly instead of reporting matched=false.
|
||||
run_fails() {
|
||||
: >"${WORK}/output"
|
||||
if FIXTURES="${WORK}/fixtures" PATH="${WORK}/bin:${PATH}" \
|
||||
PR_NUMBER=1 REPO=coder/coder GITHUB_OUTPUT="${WORK}/output" GITHUB_ACTIONS="${IN_ACTIONS:-}" \
|
||||
bash "${SCRIPT}" >"${WORK}/stdout" 2>"${WORK}/stderr"; then
|
||||
failures=$((failures + 1))
|
||||
echo "FAIL: ${case_name}: script exited zero"
|
||||
elif [[ -s "${WORK}/output" ]]; then
|
||||
failures=$((failures + 1))
|
||||
echo "FAIL: ${case_name}: wrote output despite the failure: $(<"${WORK}/output")"
|
||||
fi
|
||||
}
|
||||
|
||||
# expect <matched> <alerts>
|
||||
expect() {
|
||||
local want
|
||||
want=$(printf 'matched=%s\nalerts=%s' "$1" "$2")
|
||||
if [[ "${OUT}" != "${want}" ]]; then
|
||||
failures=$((failures + 1))
|
||||
echo "FAIL: ${case_name}"
|
||||
echo " want: ${want//$'\n'/ }"
|
||||
echo " got: ${OUT//$'\n'/ }"
|
||||
fi
|
||||
}
|
||||
|
||||
begin "npm: alert patched in the new version"
|
||||
commit dompurify 3.4.14 3.4.17
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '705\tsite/package.json\t3.4.17\n' >"${WORK}/fixtures/alerts.dompurify"
|
||||
run
|
||||
expect true 705
|
||||
|
||||
begin "go: v prefix on the patched version"
|
||||
commit github.com/moby/sys/user 0.4.0 0.4.1
|
||||
printf 'go.mod\ngo.sum\n' >"${WORK}/fixtures/files"
|
||||
printf '683\tgo.mod\tv0.4.1\n' >"${WORK}/fixtures/alerts.github.com_moby_sys_user"
|
||||
run
|
||||
expect true 683
|
||||
|
||||
begin "new version is newer than the patched version"
|
||||
commit next 15.0.0 15.10.0
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '9\tsite/package.json\t15.9.2\n' >"${WORK}/fixtures/alerts.next"
|
||||
run
|
||||
expect true 9
|
||||
|
||||
begin "new version is older than the patched version"
|
||||
commit axios 1.7.0 1.8.0
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '10\tsite/package.json\t1.8.4\n' >"${WORK}/fixtures/alerts.axios"
|
||||
run
|
||||
expect false ""
|
||||
|
||||
begin "version compare is numeric, not lexical"
|
||||
commit axios 1.8.0 1.10.0
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '10\tsite/package.json\t1.9.0\n' >"${WORK}/fixtures/alerts.axios"
|
||||
run
|
||||
expect true 10
|
||||
|
||||
begin "alert is for a manifest the PR did not change"
|
||||
commit dompurify 3.4.14 3.4.17
|
||||
echo "offlinedocs/package.json" >"${WORK}/fixtures/files"
|
||||
printf '705\tsite/package.json\t3.4.17\n' >"${WORK}/fixtures/alerts.dompurify"
|
||||
run
|
||||
expect false ""
|
||||
|
||||
begin "alert was already patched before the PR"
|
||||
commit google.golang.org/grpc 1.84.0 1.85.0
|
||||
echo "go.mod" >"${WORK}/fixtures/files"
|
||||
printf '554\tgo.mod\t1.82.1\n' >"${WORK}/fixtures/alerts.google.golang.org_grpc"
|
||||
run
|
||||
expect false ""
|
||||
|
||||
begin "old version equals the patched version"
|
||||
commit dompurify 3.4.17 3.4.18
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '705\tsite/package.json\t3.4.17\n' >"${WORK}/fixtures/alerts.dompurify"
|
||||
run
|
||||
expect false ""
|
||||
|
||||
begin "old version unknown: match on the new version"
|
||||
printf -- '---\nupdated-dependencies:\n- dependency-name: dompurify\n dependency-version: 3.4.17\n...\n' >"${WORK}/fixtures/commit-message"
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '705\tsite/package.json\t3.4.17\n' >"${WORK}/fixtures/alerts.dompurify"
|
||||
run
|
||||
expect true 705
|
||||
|
||||
begin "no alert for the package"
|
||||
commit left-pad 1.3.0 1.3.1
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
run
|
||||
expect false ""
|
||||
|
||||
begin "alert has no patched version"
|
||||
commit foo 1.0.0 2.0.0
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '11\tsite/package.json\t\n' >"${WORK}/fixtures/alerts.foo"
|
||||
run
|
||||
expect false ""
|
||||
|
||||
begin "grouped PR: one of two dependencies fixes an alert"
|
||||
commit left-pad 1.3.0 1.3.1 dompurify 3.4.14 3.4.17
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '705\tsite/package.json\t3.4.17\n' >"${WORK}/fixtures/alerts.dompurify"
|
||||
run
|
||||
expect true 705
|
||||
|
||||
begin "several alerts for one package"
|
||||
commit dompurify 3.4.14 3.4.17
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '705\tsite/package.json\t3.4.17\n706\tsite/package.json\t3.4.16\n' >"${WORK}/fixtures/alerts.dompurify"
|
||||
run
|
||||
expect true 705,706
|
||||
|
||||
begin "in Actions: result only in GITHUB_OUTPUT, nothing about alerts in the log"
|
||||
commit dompurify 3.4.14 3.4.17
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '705\tsite/package.json\t3.4.17\n' >"${WORK}/fixtures/alerts.dompurify"
|
||||
IN_ACTIONS=true run
|
||||
unset IN_ACTIONS
|
||||
expect true 705
|
||||
if [[ -s "${WORK}/stdout" ]] || grep -qiE 'alert|matched|dompurify|3\.4\.17' "${WORK}/stderr"; then
|
||||
failures=$((failures + 1))
|
||||
echo "FAIL: ${case_name}: the log mentions the alert"
|
||||
sed 's/^/ stdout: /' "${WORK}/stdout"
|
||||
sed 's/^/ stderr: /' "${WORK}/stderr"
|
||||
fi
|
||||
|
||||
begin "locally: matched alerts are printed"
|
||||
commit dompurify 3.4.14 3.4.17
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '705\tsite/package.json\t3.4.17\n' >"${WORK}/fixtures/alerts.dompurify"
|
||||
run
|
||||
grep -q 'Alert #705' "${WORK}/stdout" || {
|
||||
failures=$((failures + 1))
|
||||
echo "FAIL: ${case_name}: no alert line on stdout"
|
||||
}
|
||||
|
||||
begin "alerts API call fails"
|
||||
commit dompurify 3.4.14 3.4.17
|
||||
echo "site/package.json" >"${WORK}/fixtures/files"
|
||||
printf '705\tsite/package.json\t3.4.17\n' >"${WORK}/fixtures/alerts.dompurify"
|
||||
touch "${WORK}/fixtures/alerts-fail"
|
||||
run_fails
|
||||
|
||||
begin "commit without Dependabot metadata"
|
||||
echo "chore: manual change" >"${WORK}/fixtures/commit-message"
|
||||
echo "go.mod" >"${WORK}/fixtures/files"
|
||||
run_fails
|
||||
|
||||
begin "empty commit message"
|
||||
: >"${WORK}/fixtures/commit-message"
|
||||
echo "go.mod" >"${WORK}/fixtures/files"
|
||||
run_fails
|
||||
|
||||
begin "no changed files"
|
||||
commit dompurify 3.4.14 3.4.17
|
||||
printf '705\tsite/package.json\t3.4.17\n' >"${WORK}/fixtures/alerts.dompurify"
|
||||
run_fails
|
||||
|
||||
if ((failures > 0)); then
|
||||
echo "${failures} failure(s)"
|
||||
exit 1
|
||||
fi
|
||||
echo "ok"
|
||||
Reference in new issue
Block a user