跳到正文
DEV Community· rhussa02·· 43 分鐘前AI 評分69

交給 AI 編碼智能體執行的 10 分鐘合併前安全檢查

A 10-minute pre-merge security check you can hand to your AI coding agent

AI 導讀

作者整理了一套約需 10 分鐘的合併前安全檢查,涵蓋變更行、憑證、注入風險、新路由授權、CI 和容器設定,以及未測試的變更。文中提供可保存為 SKILL.md 的執行流程,要求核實 grep 命中、不要輸出完整密鑰,並按 BLOCK、FIX 或 SHIP 給出結論。

正文

Most leaked keys and injection bugs that reach main were not hard to find. Nobody looked. The reviewer read the logic, the tests passed, and the .env value pasted into a config file went out with the rest.

Here is a check to run on every diff before it merges. It takes about ten minutes by hand. Further down, I show how to make an AI coding agent run it for you.

1. Look only at what changed

Reviewing the whole repo is how checks get skipped. Start from the diff:

git diff -U0 origin/main...HEAD

-U0 drops context lines, so every + line is new code. That is the code you are responsible for.

2. Secrets

Search the added lines for anything that looks like a credential:

git diff -U0 origin/main...HEAD | grep -E '^\+' | \
  grep -Ei "(api[_-]?key|secret|token|passw(or)?d)\s*[:=]\s*['\"][A-Za-z0-9_/+=-]{12,}['\"]|AKIA[0-9A-Z]{16}|BEGIN (RSA |EC |OPENSSH )?PRIVATE KEY"

If a real secret shows up, deleting it in the next commit is not enough. It is in the history. Rotate it.

3. Injection

Three patterns cover most injection bugs in Python web code:

Pattern Example Fix
SQL built from strings f"SELECT * FROM users WHERE id = {uid}" Parameterized query: cur.execute("... WHERE id = %s", (uid,))
Shell with user input subprocess.run(cmd, shell=True) Pass a list, drop shell=True
Unsafe deserialization pickle.loads(body), yaml.load(f) json.loads, yaml.safe_load

A grep finds candidates. It does not find bugs. For each hit, trace where the input comes from. A query built from a constant is fine. A query built from request.args is not.

4. Auth on new routes

Grep cannot do this one. For every new route or handler in the diff, answer two questions:

  1. Does it check who the caller is?
  2. Does it check that this caller may touch this record?

The second one is the one that gets missed. GET /invoices/<id> that checks login but not ownership lets any user read any invoice.

5. CI and container changes

If the diff touches a Dockerfile or .github/workflows/, check these:

  • Base images pinned to a version or digest, not :latest
  • A non-root USER before CMD
  • No curl ... | sh
  • Actions pinned to a commit SHA, not a tag
  • A permissions: block that starts at contents: read
  • No ${{ github.event.* }} pasted straight into a run: step. Pass it through an env var instead:
- run: echo "$TITLE"
  env:
    TITLE: ${{ github.event.issue.title }}

6. Untested changes

List the functions the diff adds or changes, then check whether any test mentions them. A function that parses input, handles money or dates, or touches files and has no test is a FIX, not a SHIP.

Make the agent do it

Claude Code, Codex CLI, Cursor and most other coding agents now read SKILL.md files: a folder with instructions and optional scripts that the agent loads when the task matches. A pre-merge skill looks like this:

---
name: premerge-check
description: Run before merging a branch. Checks the diff for secrets,
  injection, missing auth, CI and Docker misconfig, and untested changes,
  then returns BLOCK, FIX, or SHIP with a ranked fix list.
---

1. Run `git diff -U0 origin/main...HEAD`.
2. Run the secret and injection greps on added lines. Treat hits as leads.
3. Read each flagged file and confirm or discard the lead.
4. For each new route, confirm identity and ownership checks.
5. If Dockerfiles or workflows changed, check the CI list.
6. List changed functions with no test.
7. Verdict: BLOCK on any committed secret or exploitable injection,
   FIX on high findings or untested input handling, otherwise SHIP.
8. Never print a full secret. Do not edit files unless asked.

Save it as ~/.claude/skills/premerge-check/SKILL.md (Codex reads .agents/skills/). Then ask the agent to "check this branch before I merge" and it follows the steps.

The parts that make this work are rule 3 and rule 8. Without "confirm or discard the lead," agents report every grep hit as a vulnerability. Without "never print a full secret," they paste your key into the chat log.

What do you check before merging that is not on this list?

This post was written with help from an AI assistant. The commands were tested on a sample repo before posting.

來源:DEV Community · dev.to