Skip to content

Commit 3d9ac9d

Browse files
authored
fix: prevent script injection in PR workflow (#340)
Move github.event references to env vars and expand validation to check all github.event.* usage in run steps By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 20ff829 commit 3d9ac9d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

.github/workflows/pr-build.yml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ permissions:
1515
id-token: write
1616
contents: read
1717

18+
env:
19+
USER: ${{ github.event.pull_request.user.login }}
20+
LABELS: ${{ toJSON(github.event.pull_request.labels.*.name) }}
21+
1822
jobs:
1923
static-code-checks:
2024
runs-on: ubuntu-latest
@@ -27,18 +31,18 @@ jobs:
2731
if: always()
2832
run: |
2933
# Check if PR is from workflows bot or dependabot
30-
if [[ "${{ github.event.pull_request.user.login }}" == "aws-application-signals-bot" ]]; then
34+
if [[ "${{ env.USER }}" == "aws-application-signals-bot" ]]; then
3135
echo "Skipping check: PR from aws-application-signals-bot"
3236
exit 0
3337
fi
3438
35-
if [[ "${{ github.event.pull_request.user.login }}" == "dependabot[bot]" ]]; then
39+
if [[ "${{ env.USER }}" == "dependabot[bot]" ]]; then
3640
echo "Skipping check: PR from dependabot"
3741
exit 0
3842
fi
3943
4044
# Check for skip changelog label
41-
if echo '${{ toJSON(github.event.pull_request.labels.*.name) }}' | jq -r '.[]' | grep -q "skip changelog"; then
45+
if echo '${{ env.LABELS }}' | jq -r '.[]' | grep -q "skip changelog"; then
4246
echo "Skipping check: skip changelog label found"
4347
exit 0
4448
fi
@@ -71,7 +75,7 @@ jobs:
7175
7276
echo "No versioned actions found in changed files"
7377
74-
- name: Check for github.event.inputs in run steps
78+
- name: Check for github.event in run steps
7579
if: always()
7680
run: |
7781
# Get changed GitHub workflow/action files
@@ -81,20 +85,20 @@ jobs:
8185
VIOLATIONS=""
8286
for file in $CHANGED_FILES; do
8387
# Extract all 'run' step values excluding this validation step
84-
RUN_STEPS=$(yq eval '.. | select(has("run") and has("name") and .name != "Check for github.event.inputs in run steps") | .run' "$file" 2>/dev/null || echo "")
85-
if echo "$RUN_STEPS" | grep -q "github\.event\.inputs\."; then
86-
VIOLATIONS="$VIOLATIONS$file: Contains github.event.inputs.* in run step\n"
88+
RUN_STEPS=$(yq eval '.. | select(has("run") and has("name") and .name != "Check for github.event in run steps") | .run' "$file" 2>/dev/null || echo "")
89+
if echo "$RUN_STEPS" | grep -q "github\.event\."; then
90+
VIOLATIONS="$VIOLATIONS$file: Contains github.event.* in run step\n"
8791
fi
8892
done
8993
9094
if [ -n "$VIOLATIONS" ]; then
91-
echo -e "Found github.event.inputs.* usage in run steps. This can lead to script injection vulnerabilities:"
95+
echo -e "Found github.event.* usage in run steps. This can lead to script injection vulnerabilities:"
9296
echo -e "$VIOLATIONS"
9397
exit 1
9498
fi
9599
fi
96100
97-
echo "No github.event.inputs usage found in run steps"
101+
echo "No github.event usage found in run steps"
98102
99103
build:
100104
runs-on: ubuntu-latest

0 commit comments

Comments
 (0)