Skip to content

fix(ci): add allow-unsafe-pr-checkout for pull_request_target - #33

Merged
watney1024 merged 1 commit into
mainfrom
zmz_dev
Jul 27, 2026
Merged

fix(ci): add allow-unsafe-pr-checkout for pull_request_target#33
watney1024 merged 1 commit into
mainfrom
zmz_dev

Conversation

@watney1024

Copy link
Copy Markdown
Collaborator

背景

PR #31 将 AI Code Review workflow 的 trigger 从 pull_request 改为 pull_request_target,使 fork PR 能访问 SENSENOVA_API_KEY。但 actions/checkout@v4 新增了安全保护,默认拒绝在 pull_request_target 下 checkout fork 代码。

变更

在 checkout 步骤添加 allow-unsafe-pr-checkout: true,显式放行。

安全说明

此 workflow 对 fork 代码只做只读操作,不存在 "pwn request" 风险:

操作 风险 说明
git diff ✅ 无 纯文本 diff,不执行任何代码
file -b --mime-encoding ✅ 无 读文件头判断类型
curl → SenseNova API ✅ 无 API key 在 HTTP Header,diff 在 body
make / python / ./script.sh ✅ 不存在 workflow 中没有编译或执行步骤
脚本来源 ✅ base 分支 所有 run: 命令 inline 在 workflow yml 中,fork 无法篡改

@github-actions

github-actions Bot commented Jul 26, 2026

Copy link
Copy Markdown

🤖 AI Code Review

共审查 1 个变更文件

📁 .github/workflows/ai-code-review.yml

🔴 Security: jq 命令注入风险 — 第 51 行: jq -r '.[] | select(.filename == "'"$FILE"'") | .patch // empty'$FILE 未经转义嵌入 jq 表达式。若文件名包含双引号,可导致 jq 解析错误或注入。
Suggestion: 替换为 jq -r --arg f "$FILE" '.[] | select(.filename == $f) | .patch // empty',使用 --arg 安全传参。

🟡 缺失 API 错误处理 — 第 36 行 gh api ... > pr_files.json 未检查返回码。若 API 失败(如网络超时、权限不足),后续 jq 会失败且无明显错误信息,整个 review 中断。
Suggestion: 添加 if ! gh api ...; then echo "FATAL: failed to fetch PR files"; exit 1; fi 或类似检查。

🟡 潜在遗漏 review 文件 — 第 57 行: patch 为空时直接跳过,未区分二进制与文本新文件。GitHub API 对于新文件通常返回完整 patch,但若因 API 限制(如文件过大截断)导致 patch 为空,会遗漏 review。
Suggestion: 保留回退逻辑:patch 为空时,通过 file 命令检测是否为二进制,若非二进制且文件行数 < 2000,则构造虚拟 diff 继续 review。

🟡 文件大小限制缺失 — 即使使用 API,patch 内容可能很大(如 10万+字符)。当前未检查 patch 大小,直接发送给 AI 可能导致请求体超时或 token 浪费。
Suggestion: 在发送前检查 DIFF 行数或字符数,超过阈值(如 2000 行或 5000 字符)则跳过并提示。

💭 移除 fetch-depth: 0.github/prompts/ 读取依赖 — 仅用于读取 prompt 文件,但若工作流运行在 pull_request_target 上的 base 分支,默认 checkout 已包含该文件,无问题。但建议添加注释说明此依赖,避免未来误改导致缺失。


In pull_request_target, git checkout gets the base branch, not the PR
head. Use gh api pulls/{number}/files to fetch the real diff, and
cache all files in a single JSON response to avoid per-file API calls.

- Fetch changed file list + per-file patches in one API call
- Skip binary/undiffable files via null patch from API
- Remove git diff fallback and virtual-diff logic for new files
@watney1024
watney1024 merged commit 8123c99 into main Jul 27, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants