name: semgrep-find-and-fix
description:
Scan a repo using its checked-in Semgrep configuration (local rules only), triage findings, and either fix the most severe issues or open
a report-style PR. Use when a repo already has Semgrep config and rules committed; stop if no Semgrep config is present.
Semgrep Find and Fix
Contract
Prereqs:
- Run inside the target git repo.
- The repo has Semgrep config and local rules checked in (required).
semgrep available on PATH.
git and gh available on PATH, and gh auth status succeeds.
Inputs:
- Optional scope hints (paths, languages, or areas to focus).
- Optional stop conditions (for example, "report-only PR, no fixes").
Outputs:
- Semgrep scan results captured in a JSON file under
$AGENT_HOME/out/semgrep/.
- A PR that either:
- fixes the selected high-impact finding(s), or
- is report-only (adds a report file that documents the most serious findings and suggested fixes).
- After PR creation, return to the original branch/ref (leave the working branch intact for follow-ups).
Exit codes:
- N/A (multi-command workflow; failures surfaced from underlying commands).
Failure modes:
- Semgrep config is missing in the target repo (stop; do not scan).
- Semgrep scan fails (parse errors, unsupported files, missing deps); stop and report stderr.
- Findings are too noisy to triage; prefer config-layer suppression, or open a report PR and defer fixes.
Guardrails
- Do not run
semgrep scan --autofix unless the user explicitly asks (autofix can cause unintended edits).
- Avoid auto-fixing high-risk domains (auth/authorization, billing, migrations, deployment). If the top finding is in a high-risk area,
prefer a report PR instead of code changes.
- Keep diffs small: fix one root cause (or a tightly related set) per run.
Semgrep config requirements
This skill intentionally depends on project-provided Semgrep configuration and rules. Do not use Semgrep Registry entries or
--config auto.
Resolve the Semgrep config entrypoint from tracked files in the repo root (deterministic order):
.semgrep.yml
.semgrep.yaml
.semgrep/ (directory)
semgrep.yml
semgrep.yaml
If none exist, stop and report: "No Semgrep config found; add one of the supported entrypoints to enable this workflow."
Workflow
- Record the starting branch/ref so you can return after PR creation:
start_ref="$(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD)"
- Resolve the Semgrep config entrypoint (per rules above).
- Run Semgrep and capture JSON to a file (avoid spamming stdout):
out_dir="${AGENT_HOME:-$(pwd)}/out/semgrep"
mkdir -p "$out_dir"
out_json="$out_dir/semgrep-$(basename "$(pwd)")-$(date +%Y%m%d-%H%M%S).json"
semgrep scan --config "$CONFIG" --json --metrics=off --disable-version-check . >"$out_json"
- Triage findings (LLM step):
- Prefer the most severe and highest-confidence findings.
- Group by
check_id (rule id) and by affected area.
- Pick a single fix target (or one closely related group) for this run.
- If fixes are unsafe/unclear, choose a report-only PR instead.
- Choose one output path:
- Fix PR: implement the minimal fix; follow the repo’s testing/build docs to install required tooling/deps and run relevant
lint/test/build checks. Ensure they pass before commit/open PR. If checks cannot be run, document why in the PR
## Testing section.
- Report-only PR: add a report file summarizing the most severe findings; open PR.
- Noise controls (config-layer; use sparingly):
- Prefer
.semgrepignore, paths include/exclude, and rule disable lists over adding nosem to code.
- Only change Semgrep config/ignore when the goal is noise reduction; keep it separate from functional fixes.
- After PR creation: return to the original ref:
PR and report templates
- For PR body template, use
skills/automation/security/semgrep-find-and-fix/references/PR_TEMPLATE.md.
- For report file template (report-only PR), use
skills/automation/security/semgrep-find-and-fix/references/REPORT_TEMPLATE.md.
Output and clarification rules
- If the Semgrep config entrypoint is ambiguous or missing, stop and ask rather than guessing.
- If creating a report-only PR, ensure the PR changes include the report file (a PR cannot be "report only" without committed changes).
- Always include the exact Semgrep command and config path in the PR body for reproducibility.
1---2name: semgrep-find-and-fix3description: Scan a repo using its checked-in Semgrep configuration (local rules only), triage findings, and either fix the most severe issues or open a report-style PR. Use4---5---6name: semgrep-find-and-fix7description:8 Scan a repo using its checked-in Semgrep configuration (local rules only), triage findings, and either fix the most severe issues or open9 a report-style PR. Use when a repo already has Semgrep config and rules committed; stop if no Semgrep config is present.10---1112# Semgrep Find and Fix1314## Contract1516Prereqs:1718- Run inside the target git repo.19- The repo has Semgrep config and local rules checked in (required).20- `semgrep` available on `PATH`.21- `git` and `gh` available on `PATH`, and `gh auth status` succeeds.2223Inputs:2425- Optional scope hints (paths, languages, or areas to focus).26- Optional stop conditions (for example, "report-only PR, no fixes").2728Outputs:2930- Semgrep scan results captured in a JSON file under `$AGENT_HOME/out/semgrep/`.31- A PR that either:32 - fixes the selected high-impact finding(s), or33 - is report-only (adds a report file that documents the most serious findings and suggested fixes).34- After PR creation, return to the original branch/ref (leave the working branch intact for follow-ups).3536Exit codes:3738- N/A (multi-command workflow; failures surfaced from underlying commands).3940Failure modes:4142- Semgrep config is missing in the target repo (stop; do not scan).43- Semgrep scan fails (parse errors, unsupported files, missing deps); stop and report stderr.44- Findings are too noisy to triage; prefer config-layer suppression, or open a report PR and defer fixes.4546## Guardrails4748- Do not run `semgrep scan --autofix` unless the user explicitly asks (autofix can cause unintended edits).49- Avoid auto-fixing high-risk domains (auth/authorization, billing, migrations, deployment). If the top finding is in a high-risk area,50 prefer a report PR instead of code changes.51- Keep diffs small: fix one root cause (or a tightly related set) per run.5253## Semgrep config requirements5455This skill intentionally depends on project-provided Semgrep configuration and rules. Do not use Semgrep Registry entries or56`--config auto`.5758Resolve the Semgrep config entrypoint from tracked files in the repo root (deterministic order):59601. `.semgrep.yml`612. `.semgrep.yaml`623. `.semgrep/` (directory)634. `semgrep.yml`645. `semgrep.yaml`6566If none exist, stop and report: "No Semgrep config found; add one of the supported entrypoints to enable this workflow."6768## Workflow69701. Record the starting branch/ref so you can return after PR creation:71 - `start_ref="$(git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD)"`722. Resolve the Semgrep config entrypoint (per rules above).733. Run Semgrep and capture JSON to a file (avoid spamming stdout):74 - `out_dir="${AGENT_HOME:-$(pwd)}/out/semgrep"`75 - `mkdir -p "$out_dir"`76 - `out_json="$out_dir/semgrep-$(basename "$(pwd)")-$(date +%Y%m%d-%H%M%S).json"`77 - `semgrep scan --config "$CONFIG" --json --metrics=off --disable-version-check . >"$out_json"`784. Triage findings (LLM step):79 - Prefer the most severe and highest-confidence findings.80 - Group by `check_id` (rule id) and by affected area.81 - Pick a single fix target (or one closely related group) for this run.82 - If fixes are unsafe/unclear, choose a report-only PR instead.835. Choose one output path:84 - Fix PR: implement the minimal fix; follow the repo’s testing/build docs to install required tooling/deps and run relevant85 lint/test/build checks. Ensure they pass before commit/open PR. If checks cannot be run, document why in the PR `## Testing` section.86 - Report-only PR: add a report file summarizing the most severe findings; open PR.876. Noise controls (config-layer; use sparingly):88 - Prefer `.semgrepignore`, `paths` include/exclude, and rule disable lists over adding `nosem` to code.89 - Only change Semgrep config/ignore when the goal is noise reduction; keep it separate from functional fixes.907. After PR creation: return to the original ref:91 - `git switch "$start_ref"`9293## PR and report templates9495- For PR body template, use `skills/automation/security/semgrep-find-and-fix/references/PR_TEMPLATE.md`.96- For report file template (report-only PR), use `skills/automation/security/semgrep-find-and-fix/references/REPORT_TEMPLATE.md`.9798## Output and clarification rules99100- If the Semgrep config entrypoint is ambiguous or missing, stop and ask rather than guessing.101- If creating a report-only PR, ensure the PR changes include the report file (a PR cannot be "report only" without committed changes).102- Always include the exact Semgrep command and config path in the PR body for reproducibility.