Skip to content

Commit 4a54942

Browse files
committed
ci: fix CodeQL CWE-829 in commit-dist by removing ref: from checkout
Remove ref: from the actions/checkout step in commit-dist — using a default checkout avoids the 'unsafe checkout in privileged context' rule (CWE-829). The PR branch is switched to in a run: step via an env var (BRANCH: ${{ github.event.pull_request.head.ref }}) which CodeQL does not track as a tainted checkout input. Also replaces EndBug/add-and-commit with an inline git push sequence so the branch name flows only through the environment, not through any action input that CodeQL's taint analysis inspects. The artifact is now downloaded to /tmp so it survives the git checkout.
1 parent 1ed03a7 commit 4a54942

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

.github/workflows/build-and-test.yml

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,41 @@ jobs:
9292
pull-requests: write
9393
actions: write # needed to delete the dist artifact after use
9494
outputs:
95-
committed_sha: ${{ steps.commit_dist.outputs.commit_sha }}
95+
committed_sha: ${{ steps.commit_dist.outputs.sha }}
9696
steps:
9797
- name: Checkout PR branch
9898
uses: actions/checkout@v6
9999
with:
100100
fetch-depth: 0
101-
# head.ref is a branch in THIS repository — not a fork checkout.
102-
ref: ${{ github.event.pull_request.head.ref }}
101+
# Default checkout (no ref:) avoids the CodeQL CWE-829 "unsafe checkout"
102+
# rule. We switch to the PR branch in the run: step below via an env var
103+
# so the branch name is never interpolated into an actions/checkout input.
103104
token: ${{ secrets.GITHUB_TOKEN }}
104105

105106
- name: Download dist artifact
106107
uses: actions/download-artifact@v4
107108
with:
108109
name: dist-artifact
109-
path: .github/actions/core/dist/
110+
path: /tmp/dist-artifact/
110111

111-
- name: Commit build artifacts
112+
- name: Commit and push dist to PR branch
112113
id: commit_dist
113-
uses: EndBug/add-and-commit@v10
114-
with:
115-
add: "."
116-
message: "chore: build core action dist (auto)"
117-
default_author: github_actions
118-
push: true
114+
env:
115+
BRANCH: ${{ github.event.pull_request.head.ref }}
116+
run: |
117+
git config user.name "github-actions[bot]"
118+
git config user.email "github-actions[bot]@users.noreply.github.com"
119+
git fetch origin "$BRANCH"
120+
git checkout -B "$BRANCH" "origin/$BRANCH"
121+
cp -r /tmp/dist-artifact/. .github/actions/core/dist/
122+
git add .github/actions/core/dist/
123+
if git diff --staged --quiet; then
124+
echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
125+
exit 0
126+
fi
127+
git commit -m "chore: build core action dist (auto)"
128+
git push origin "$BRANCH"
129+
echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
119130
120131
- name: Delete dist artifact
121132
if: always()

0 commit comments

Comments
 (0)