Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .github/scripts/cla-scope.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env bash
# Tests the "Decide who must sign" step of .github/workflows/cla.yml against
# recorded commit lists. Run: bash .github/scripts/cla-scope.test.sh
# Needs bash, jq and python3. The step's script is extracted from the workflow,
# so the test always exercises the committed logic.
set -euo pipefail
root=$(cd "$(dirname "$0")/../.." && pwd)
wf="$root/.github/workflows/cla.yml"
tmp=$(mktemp -d)
trap 'find "$tmp" -delete' EXIT

python3 -I - "$wf" "$tmp/scope.sh" <<'PY'
import re, sys
s = open(sys.argv[1]).read()
step = s[s.index("id: scope"):]
m = re.search(r"\n run: \|\n((?: .*\n|\n)+)", step)
open(sys.argv[2], "w").write("".join(l[10:] + "\n" for l in m.group(1).splitlines()))
PY
allowlist=$(sed -n 's/^ CLA_ALLOWLIST: "\(.*\)"$/\1/p' "$wf")
[ -n "$allowlist" ] || { echo "CLA_ALLOWLIST not found in $wf"; exit 1; }

mkdir -p "$tmp/bin"
printf '#!/usr/bin/env bash\ncat "$FIXTURE"\n' > "$tmp/bin/gh"
chmod +x "$tmp/bin/gh"

commit() { # sha author committer verified
local a=null c=null
[ "$2" = - ] || a="{\"login\":\"$2\"}"
[ "$3" = - ] || c="{\"login\":\"$3\"}"
printf '{"sha":"%s","author":%s,"committer":%s,"commit":{"verification":{"verified":%s}}}' "$1" "$a" "$c" "$4"
}

fail=0
# case <name> <opener> <expected: run=true|run=false|error> <commits...>
case_() {
local name=$1 opener=$2 want=$3; shift 3
local list; list=$(IFS=,; echo "[$*]")
echo "$list" > "$tmp/fixture.json"
: > "$tmp/out"
local got
if FIXTURE="$tmp/fixture.json" OPENER="$opener" PR=1 REPO=o/r RUNNER_TEMP="$tmp" \
GITHUB_OUTPUT="$tmp/out" CLA_ALLOWLIST="$allowlist" PATH="$tmp/bin:$PATH" \
bash "$tmp/scope.sh" > "$tmp/log" 2>&1; then
got=$(cat "$tmp/out")
else
got=error
fi
if [ "$got" = "$want" ]; then echo "ok $name"; else echo "FAIL $name: want $want, got $got"; cat "$tmp/log"; fail=1; fi
}

case_ "maintainer PR, own unlinked commits" 0xmanhnv run=false \
"$(commit a1 0xmanhnv 0xmanhnv false)" "$(commit a2 - - false)"
case_ "maintainer PR with an outside account's commit" 0xmanhnv run=true \
"$(commit b1 someone someone false)"
case_ "bot PR" 'dependabot[bot]' run=false \
"$(commit c1 'dependabot[bot]' web-flow true)"
case_ "outside PR, own commits" someone run=true \
"$(commit d1 someone someone false)"
case_ "outside PR, unlinked commits" someone run=true \
"$(commit d2 - - false)"
case_ "outside PR, maintainer as author, unsigned" someone error \
"$(commit e1 0xmanhnv someone false)"
case_ "outside PR, maintainer as author, signed by the contributor" someone error \
"$(commit e2 0xmanhnv someone true)"
case_ "outside PR, maintainer as committer only, author unlinked" someone error \
"$(commit e3 - 0xmanhnv true)"
case_ "outside PR, maintainer as author and committer, unsigned" someone error \
"$(commit e4 0xmanhnv 0xmanhnv false)"
case_ "outside PR, maintainer commit authored, committed and signed by the maintainer" someone run=true \
"$(commit f1 0xmanhnv 0xmanhnv true)" "$(commit f2 someone someone false)"

exit $fail
31 changes: 31 additions & 0 deletions .github/workflows/cla-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: CLA scope test

# Tests the "Decide who must sign" step of cla.yml whenever the workflow or its
# test changes. Runs the pull request's own copy with a read-only token.

on:
pull_request:
paths:
- .github/workflows/cla.yml
- .github/workflows/cla-test.yml
- .github/scripts/cla-scope.test.sh
push:
branches: [main, develop]
paths:
- .github/workflows/cla.yml
- .github/workflows/cla-test.yml
- .github/scripts/cla-scope.test.sh

permissions:
contents: read

jobs:
test:
name: cla-scope-test
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- run: bash .github/scripts/cla-scope.test.sh
48 changes: 33 additions & 15 deletions .github/workflows/cla.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ name: CLA
# Allowlist (CLA_ALLOWLIST): the maintainer and the bots that open pull
# requests here. A pull request opened by one of them is checked only when it
# carries commits linked to another GitHub account. In a pull request opened
# by anyone else, a commit that names an allowlisted account must have a
# verified signature, because commit author e-mails can be forged.
# by anyone else, a commit that names an allowlisted account counts only when
# its author and its committer are both allowlisted accounts and its signature
# is verified: author and committer e-mails can be forged, and a signature
# proves only the committer. The logic is tested by
# .github/scripts/cla-scope.test.sh.
#
# Keep `cla` a required check on pull requests. In the merge queue it always
# passes (the pull request was already checked), so requiring it does not
# block the queue.

on:
pull_request_target: # security-review: allow GHA-PR-TARGET no pull request code is checked out or run; see the safety notes above
Expand Down Expand Up @@ -61,7 +68,8 @@ jobs:
steps:
- name: Decide who must sign
id: scope
if: github.event_name == 'pull_request_target'
# Pull request events only (not comments, not the merge queue).
if: github.event.pull_request
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
Expand All @@ -70,34 +78,44 @@ jobs:
run: |
set -euo pipefail
opener_allowed=$(jq -rn --arg al "$CLA_ALLOWLIST" --arg u "$OPENER" '$al | split(",") | index($u) != null')
# One line per commit: sha, linked account (author, else committer), verified.
# One line per commit: sha, the account the CLA action will judge it by
# (author, else committer), and a kind:
# allowlisted author AND committer allowlisted AND signature verified
# forged judged as an allowlisted account but not the above
# unlinked no GitHub account
# other another account (must sign)
# The signature proves only the committer, so both identities count.
gh api --paginate "repos/$REPO/pulls/$PR/commits" \
| jq -r --arg al "$CLA_ALLOWLIST" '.[] |
((.author.login // .committer.login // "") ) as $l |
[.sha, $l, (if $l != "" and ($al | split(",") | index($l)) != null then "allowlisted" elif $l == "" then "unlinked" else "other" end),
(.commit.verification.verified | tostring)] | @tsv' > "$RUNNER_TEMP/commits.tsv"
| jq -r --arg al "$CLA_ALLOWLIST" '
($al | split(",")) as $list |
def ok($u): $u != "" and ($list | index($u)) != null;
.[] |
(.author.login // "") as $a | (.committer.login // "") as $c |
(if $a != "" then $a else $c end) as $id |
[.sha, $id,
(if $id == "" then "unlinked"
elif ok($id) | not then "other"
elif ok($a) and ok($c) and (.commit.verification.verified == true) then "allowlisted"
else "forged" end)] | @tsv' > "$RUNNER_TEMP/commits.tsv"
run=false
forged=""
while IFS=$'\t' read -r sha login kind verified; do
while IFS=$'\t' read -r sha login kind; do
case "$kind" in
other) run=true ;;
unlinked) [ "$opener_allowed" = true ] || run=true ;;
allowlisted)
if [ "$opener_allowed" != true ] && [ "$verified" != true ]; then
forged="$forged $sha($login)"
fi ;;
forged) [ "$opener_allowed" = true ] || forged="$forged $sha($login)" ;;
esac
done < "$RUNNER_TEMP/commits.tsv"
[ "$opener_allowed" = true ] || run=true
if [ -n "$forged" ]; then
echo "::error::unsigned commits name an allowlisted account:$forged. Sign them, or author them under your own account."
echo "::error::commits name an allowlisted account but are not authored, committed and signed by allowlisted accounts:$forged. Author them under your own account."
exit 1
fi
echo "run=$run" >> "$GITHUB_OUTPUT"

- name: CLA
if: >-
(github.event_name == 'pull_request_target' && steps.scope.outputs.run == 'true') ||
(github.event.pull_request && steps.scope.outputs.run == 'true') ||
(github.event_name == 'issue_comment' &&
(github.event.comment.body == 'recheck' ||
startsWith(github.event.comment.body, 'I have read the CLA Document and I hereby sign the CLA')))
Expand Down
Loading