Skip to content

Commit 2a3445e

Browse files
authored
ci: prevent command injection in prevent-deletion workflow (#22928)
1 parent e36a4d3 commit 2a3445e

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

.github/workflows/prevent-deletion.yaml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,43 @@ jobs:
1616
steps:
1717
- name: Checkout base
1818
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
1921
- name: Fetch head
22+
env:
23+
HEAD_CLONE_URL: ${{ github.event.pull_request.head.repo.clone_url }}
24+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
2025
run: |
21-
git remote add head ${{ github.event.pull_request.head.repo.clone_url }}
22-
git fetch --depth=1 head ${{ github.event.pull_request.head.ref }}
26+
git remote add head "$HEAD_CLONE_URL"
27+
git fetch head -- "$HEAD_REF"
2328
- name: Find changes
29+
env:
30+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
2431
run: |
25-
git rev-parse '${{ github.event.pull_request.head.sha }}'
26-
if git diff --merge-base --name-only --diff-filter 'D' HEAD '${{ github.event.pull_request.head.sha }}' | grep -E '^media/.*\.(jpg|png|jpeg|gif)$' >/tmp/changed_files; then
32+
git rev-parse "$HEAD_SHA"
33+
if git diff --merge-base --name-only --diff-filter 'D' HEAD "$HEAD_SHA" | grep -E '^media/.*\.(jpg|png|jpeg|gif)$' >/tmp/changed_files; then
2734
cat /tmp/changed_files
28-
echo '{"name":"Image Deletion Check","head_sha":"${{ github.event.pull_request.head.sha }}","status":"completed","conclusion":"failure"}' > /tmp/body.json
35+
jq -n --arg sha "$HEAD_SHA" \
36+
'{name:"Image Deletion Check",head_sha:$sha,status:"completed",conclusion:"failure"}' > /tmp/body.json
2937
jq \
3038
--arg count "$(wc -l /tmp/changed_files | awk '{print $1}')" \
3139
--arg summary "$(cat /tmp/changed_files | sed 's/^/- /')" \
3240
'.output.title = "Found " + $count + " deleted images" | .output.summary = $summary' \
3341
/tmp/body.json > /tmp/body2.json
3442
else
35-
echo '{"name":"Image Deletion Check","head_sha":"${{ github.event.pull_request.head.sha }}","status":"completed","conclusion":"success","output":{"title":"OK","summary":"No deleted images"}}' > /tmp/body2.json
43+
jq -n --arg sha "$HEAD_SHA" \
44+
'{name:"Image Deletion Check",head_sha:$sha,status:"completed",conclusion:"success",output:{title:"OK",summary:"No deleted images"}}' > /tmp/body2.json
3645
fi
3746
- name: Publish result
47+
env:
48+
GH_TOKEN: ${{ github.token }}
49+
REPO: ${{ github.repository }}
3850
run: |
3951
cat /tmp/body2.json
4052
curl \
4153
-sSL \
4254
-X POST \
4355
-H "Accept: application/vnd.github+json" \
44-
-H "Authorization: token ${{ github.token }}" \
56+
-H "Authorization: token $GH_TOKEN" \
4557
-T '/tmp/body2.json' \
46-
'https://api.github.com/repos/${{ github.repository }}/check-runs'
58+
"https://api.github.com/repos/$REPO/check-runs"

0 commit comments

Comments
 (0)