Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Semgrep-AI

基于 LangGraph + LLM(DeepSeek)的 Semgrep 告警自动分析与修复工作流。

环境依赖

Python 版本

  • Python >= 3.10(推荐 3.12)

Python 依赖库

依赖 版本 用途
langgraph >= 1.1 LangGraph 工作流编排
openai >= 2.36 DeepSeek LLM 客户端
python-dotenv >= 1.2 .env 环境变量加载
langchain-core >= 1.4 LangGraph 底层依赖

安装方式:

pip install langgraph openai python-dotenv

外部工具

  • Semgrep(必需):代码扫描引擎,需提前安装,确保 semgrep 命令可用
  • DeepSeek API Key(必需):需在 .env 中配置 DEEPSEEK_API_KEY

主要功能

  1. 自动扫描 — 调用 Semgrep 对目标代码仓库进行安全扫描,提取 ERROR 级别告警
  2. 智能分析 — 基于 LLM 分析告警产生原因,定位代码漏洞根源,输出修复方案
  3. 自动修复 — 根据分析结果自动修改源代码,消除告警
  4. 迭代验证 — 修复后重新扫描,确认告警消失;如有新增告警则继续修复,直至全部清零

使用方法

1. 配置环境变量

复制 .env 文件并填入 DeepSeek API Key:

DEEPSEEK_API_KEY = "sk-your-key-here"
DEEPSEEK_BASE_URL = "https://api.deepseek.com/v1"
DEEPSEEK_MODEL = "deepseek-chat"

2. 运行

python main.py <repo_path> [--rule <rule_path>]

参数说明

参数 必填 说明
repo_path 要扫描的代码仓库路径
--rule / -r Semgrep 规则路径,不指定则 auto

示例

# 扫描 testCases/t1 目录(自动规则)
python main.py ./testCases/t1

# 使用自定义规则扫描
python main.py ./testCases/t1 --rule ./my-rules.yaml

整体框架

Semgrep-AI/
├── main.py                         # 入口:CLI 参数解析 + 启动工作流
├── workflow.py                     # LangGraph 工作流定义(状态、节点、路由、图构建)
├── .env                            # 全局环境变量配置
├── agents/
│   ├── baseAgent/                  # Agent 基类(Mixin 模式)
│   │   ├── base_agent.py           #   入口,组合所有 Mixin
│   │   ├── agent_core.py           #   执行引擎(run / run_step)
│   │   ├── agent_tools.py          #   工具注册与参数解析
│   │   ├── agent_skills.py         #   技能加载与匹配
│   │   ├── agent_plan.py           #   规划模式(任务拆解)
│   │   ├── agent_prompts.py        #   所有 LLM Prompt 模板
│   │   └── llm_client.py           #   DeepSeek API 客户端
│   ├── tools/                      # 工具集合
│   │   ├── base_tool.py            #   工具基类
│   │   ├── file_tools.py           #   ReadFile / WriteFile / EditFile
│   │   └── bash_tools.py           #   Glob / Grep / Bash
│   ├── skills/                     # 技能定义(SKILL.md)
│   │   ├── semgrep-alert-analysis/ #   告警分析技能
│   │   └── code-repair/            #   代码修复技能
│   └── rules/                      # 全局约束规则
├── testCases/                      # 测试用例目录
└── logs/                           # 执行日志

工作流流程

init_node → scan_node ──┬── (done) → END
                         └── (有告警) → analysis_node → repair_node → scan_node(迭代)
  1. init_node: 初始化状态
  2. scan_node: 执行 semgrep scan,解析 JSON 结果,提取 ERROR 告警
  3. analysis_node: 调用 BaseAgent(激活 semgrep-alert-analysis 技能)分析告警
  4. repair_node: 调用 BaseAgent(激活 code-repair 技能)执行代码修复
  5. 返回 scan_node 重新扫描,循环直至无告警

About

Establish an integrated engineering platform for automated code defect scanning, root cause analysis, and intelligent repair.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages