Skip to content

Commit f147189

Browse files
committed
Harden CRD size workflow against untrusted checkout
Check out only the trusted pull request base commit in the privileged workflow and fetch the PR CRD through GitHub's raw Contents API instead of placing the fork tree on the runner. The raw representation avoids the JSON and base64 response limitation for files larger than 1 MiB. Grant the workflow read-only contents access and enable strict shell error handling while preserving the existing size calculation and comment behavior.
1 parent e7a4aaa commit f147189

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

‎.github/workflows/crd-size-badge.yaml‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ on:
1111
- '[0-9]+.[0-9]+-fr[0-9]+'
1212

1313
permissions:
14+
contents: read
1415
pull-requests: write
1516

1617
jobs:
@@ -20,17 +21,29 @@ jobs:
2021
- name: Checkout
2122
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
2223
with:
23-
ref: ${{ github.event.pull_request.head.sha || github.sha }}
24-
allow-unsafe-pr-checkout: true
24+
# Never check out the untrusted PR head in this privileged workflow.
25+
ref: ${{ github.event.pull_request.base.sha || github.sha }}
2526

2627
- name: Install yq
2728
uses: mikefarah/yq@1b9b4ac5187171d2e5e3129be0cfa827c7f9d53d # v4
2829

2930
- name: Compute CRD JSON size
3031
id: size
32+
env:
33+
PR_HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
34+
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
3135
run: |
36+
set -euo pipefail
3237
CRD_FILE="config/crd/bases/core.openstack.org_openstackcontrolplanes.yaml"
33-
SIZE=$(yq eval -o=json "$CRD_FILE" | jq -c . | wc -c)
38+
if [ "$GITHUB_EVENT_NAME" = "pull_request_target" ]; then
39+
PR_CRD_JSON=$(gh api \
40+
-H "Accept: application/vnd.github.raw+json" \
41+
"repos/$PR_HEAD_REPO/contents/$CRD_FILE?ref=$PR_HEAD_SHA" \
42+
| yq eval -o=json - | jq -c .)
43+
SIZE=$(printf '%s\n' "$PR_CRD_JSON" | wc -c)
44+
else
45+
SIZE=$(yq eval -o=json "$CRD_FILE" | jq -c . | wc -c)
46+
fi
3447
echo "bytes=$SIZE" >> "$GITHUB_OUTPUT"
3548
3649
if [ "$SIZE" -lt 307200 ]; then

0 commit comments

Comments
 (0)