Skip to content

Commit f78bef1

Browse files
sourcecodereviewerRbb666
authored andcommitted
fix(ci): prevent expression injection in pr_format_bot.yml
Move user-controlled GitHub Actions context expressions (github.event.pull_request.head.ref, head.repo.full_name, pull_request.number, event.action) from direct interpolation in run: blocks to env: variables. Direct interpolation of these values in shell scripts allows attackers to inject arbitrary commands via crafted branch names under pull_request_target, which runs in the base repo context. Using env: variables ensures values are treated as literal strings by the shell, preventing command injection. Ref: https://securitylab.github.com/research/github-actions-untrusted-input/ Reported-by: Wilson Cyber Research (@sourcecodereviewer) Security: expression-injection
1 parent 5d1747a commit f78bef1

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

.github/workflows/pr_format_bot.yml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,38 @@ jobs:
2020
- name: Check if first commit and add comment
2121
env:
2222
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23+
PR_HEAD_REF: ${{ github.event.pull_request.head.ref }}
24+
PR_HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
25+
PR_NUMBER: ${{ github.event.pull_request.number }}
26+
PR_ACTION: ${{ github.event.action }}
27+
REPO_FULL_NAME: ${{ github.repository }}
2328
run: |
24-
echo "Event action: ${{ github.event.action }}"
29+
echo "Event action: $PR_ACTION"
2530
2631
# 获取 PR 的提交信息
2732
commits=$(curl -s \
2833
-H "Accept: application/vnd.github.v3+json" \
2934
-H "Authorization: Bearer $GITHUB_TOKEN" \
30-
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/commits")
35+
"https://api.github.com/repos/${REPO_FULL_NAME}/pulls/${PR_NUMBER}/commits")
3136
3237
# 检查 API 响应是否为数组
3338
if echo "$commits" | jq -e 'type == "array"' > /dev/null; then
3439
commit_count=$(echo "$commits" | jq '. | length')
3540
echo "PR commit count: $commit_count"
3641
3742
should_comment=false
38-
if [ "${{ github.event.action }}" = "opened" ]; then
43+
if [ "$PR_ACTION" = "opened" ]; then
3944
should_comment=true
40-
elif [ "${{ github.event.action }}" = "synchronize" ] && [ "$commit_count" -eq 1 ]; then
45+
elif [ "$PR_ACTION" = "synchronize" ] && [ "$commit_count" -eq 1 ]; then
4146
should_comment=true
4247
fi
4348
4449
if [ "$should_comment" = true ]; then
4550
echo "Adding format notification comment..."
4651
4752
# 构建工作流链接
48-
branch="${{ github.event.pull_request.head.ref }}"
49-
fork_repo="${{ github.event.pull_request.head.repo.full_name }}"
53+
branch="$PR_HEAD_REF"
54+
fork_repo="$PR_HEAD_REPO"
5055
workflow_url="https://github.com/${fork_repo}/actions/workflows/pr_clang_format.yml"
5156
direct_link="${workflow_url}?branch=${branch}"
5257
@@ -69,7 +74,7 @@ jobs:
6974
"- 设置需排除的文件/目录(目录请以\"/\"结尾)"
7075
"Set files/directories to exclude (directories should end with \"/\")"
7176
"- 将目标分支设置为 \ Set the target branch to:**\`${branch}\`**"
72-
"- 设置PR number为 \ Set the PR number to:**\`${{ github.event.pull_request.number }}\`**"
77+
"- 设置PR number为 \ Set the PR number to:**\`${PR_NUMBER}\`**"
7378
""
7479
"3. **等待工作流完成 | Wait for the workflow to complete**"
7580
"格式化后的代码将自动推送至你的分支。"
@@ -92,7 +97,7 @@ jobs:
9297
existing_comment=$(curl -s \
9398
-H "Accept: application/vnd.github.v3+json" \
9499
-H "Authorization: Bearer $GITHUB_TOKEN" \
95-
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" | \
100+
"https://api.github.com/repos/${REPO_FULL_NAME}/issues/${PR_NUMBER}/comments" | \
96101
jq -r '.[] | select(.user.login == "github-actions[bot]" and (.body | contains("<!-- PR Format Notification Comment -->"))) | {id: .id, body: .body} | @base64')
97102
98103
# 使用 jq 安全地构建 JSON 负载
@@ -107,7 +112,7 @@ jobs:
107112
-H "Accept: application/vnd.github.v3+json" \
108113
-H "Authorization: Bearer $GITHUB_TOKEN" \
109114
-d "$json_payload" \
110-
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id")
115+
"https://api.github.com/repos/${REPO_FULL_NAME}/issues/comments/$comment_id")
111116
else
112117
# 创建新评论
113118
echo "Creating new comment"
@@ -116,7 +121,7 @@ jobs:
116121
-H "Accept: application/vnd.github.v3+json" \
117122
-H "Authorization: Bearer $GITHUB_TOKEN" \
118123
-d "$json_payload" \
119-
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments")
124+
"https://api.github.com/repos/${REPO_FULL_NAME}/issues/${PR_NUMBER}/comments")
120125
fi
121126
122127
# 提取 HTTP 状态码和响应体

0 commit comments

Comments
 (0)