vahapogut/Aegis-Security
GitHub: vahapogut/Aegis-Security
一款专为AI生成代码设计的安全扫描工具,能精准识别大模型「偷工减料」产生的逻辑漏洞和安全风险。
Stars: 1 | Forks: 0
## 快速开始
```
cargo install aegis
aegis audit .
```
就是这样。无需配置。无需云端。无需 API 密钥。100% 本地运行。
## 支持的语言
| 语言 | 规则数 | 状态 |
|----------|-------|--------|
| TypeScript / TSX | 10 | 稳定 |
| Python | 5 | 稳定 |
| JavaScript | — | 计划中 |
| Go | — | 计划中 |
| Rust | — | 计划中 |
即将支持更多语言。Aegis 使用 Tree-sitter,它支持 40 多种语言 —— 引擎已准备就绪,我们只需要社区驱动的规则。
## 演示
```
$ aegis audit .
[HIGH] Possible Auth Bypass. Sensitive route lacks authorization (role) checks. in auth.ts:35
Explanation: AI often implements JWT verification but forgets to check
if the user actually has the privileges to access the route.
[HIGH] Floating promise detected. AI often forgets to await async functions. in db.ts:12
Explanation: Unawaited database calls may continue executing after
the request lifecycle ends, causing race conditions.
[HIGH] Hardcoded secret detected. in config.ts:3
Explanation: AI coding assistants frequently insert mock API keys
which developers forget to move into environment variables.
[MEDIUM] Fake validation detected. in users.ts:7
Explanation: AI generates if-checks that log errors but never return,
allowing execution to continue past failed validation.
Scan Summary
────────────
HIGH: 8
MEDIUM: 1
LOW: 0
AI Risk Score: 82/100
[!] Found 9 violations
```
## 为什么会有 Aegis
AI 编程助手(Copilot、Cursor、Claude、DeepSeek)极大地提高了开发速度。但它们引入了一种**全新的 bug 类别**,这是传统 linter 完全无法发现的 —— 因为语法完全正确,出问题的是**逻辑**。
Aegis 不会问*“这段代码有漏洞吗?”*
Aegis 会问**“这是 AI 写的吗,它偷工减料了吗?”**
我们使用 **Tree-sitter** 进行 AST 解析,并结合自定义的 **YAML 规则引擎**,以极高的精准度捕获这些行为安全缺陷。
## 真实的 AI 生成安全漏洞
这些是 LLM 每天生成的实际模式。Aegis 可以捕获所有这些模式。
### TypeScript
**遗漏 `await`(竞态条件)**
```
app.post('/api/users', (req, res) => {
db.save(req.body); // ← No await. Data may never persist.
res.send("Saved");
});
```
**虚假验证(身份验证绕过)**
```
if (!req.body.email) {
console.log("Email missing"); // ← No return. Code continues below.
}
createUser(req.body); // ← Runs even without email
```
**静默吞没错误**
```
try {
await chargeCustomer(order);
} catch (e) {
console.log(e); // ← Payment fails silently.
}
```
### Python
**通过 f-string 导致的 SQL 注入**
```
# AI 的 #1 Python 错误 — SQL 中的 f-strings
cursor.execute(f"SELECT * FROM users WHERE id = {user_id}")
```
**不安全的 Pickle 反序列化(RCE)**
```
# AI 万事皆推荐 pickle — 导致 remote code execution
model = pickle.load(open("model.pkl", "rb"))
```
**生产环境中遗留的 DEBUG=True**
```
# AI 生成 Django/Flask boilerplate 却从不翻转这个
DEBUG = True
SECRET_KEY = "super-secret-key-12345"
```
## 规则展示
Aegis 内置了针对 TypeScript 和 Python 的 **15 条精准优先的规则**。
### TypeScript 规则 (10)
| 规则 ID | 严重性 | 检测到的问题 |
|---------|----------|------------------|
| `ai-auth-bypass` | HIGH | 验证了 JWT 但未进行角色/权限检查 |
| `ai-floating-promise` | HIGH | 未等待的异步 DB/网络调用 |
| `ai-silent-fail` | HIGH | `catch(e) { console.log(e) }` |
| `ai-hardcoded-secret` | HIGH | 代码中遗留的占位符 API 密钥 |
| `ai-regex-injection` | HIGH | `new RegExp(req.query.q)` |
| `ai-open-redirect` | HIGH | `res.redirect(req.query.next)` |
| `ai-insecure-fetch` | HIGH | `rejectUnauthorized: false` |
| `ai-missing-rate-limit` | HIGH | 缺少暴力破解保护的 Login/OTP 路由 |
| `ai-unsafe-innerhtml` | HIGH | 未经过滤直接使用 `innerHTML = userInput` |
| `ai-fake-validation` | MEDIUM | 永远不会 return/throw 的验证块 |
### Python 规则 (5)
| 规则 ID | 严重性 | 检测到的问题 |
|---------|----------|------------------|
| `ai-bare-except` | HIGH | 静默吞没所有错误的 `except:` |
| `ai-hardcoded-secret-py` | HIGH | 源文件中硬编码的凭据 |
| `ai-sql-format-string` | HIGH | 通过 f-string 或 `.format()` 进行的 SQL 注入 |
| `ai-debug-true` | HIGH | Django/Flask 设置中遗留的 `DEBUG = True` |
| `ai-pickle-load` | HIGH | 允许远程代码执行 (RCE) 的 `pickle.load()` |
## 性能
Rust 和 Tree-sitter 使 Aegis 极其快速。
```
Scanned 12,000 LOC in 0.08s
```
Aegis 不会给你的 CI/CD 流水线增加任何明显的开销。
## 为什么不用 Semgrep?
“这不就是 Semgrep 吗?” —— 不。我们在架构上借鉴了相同的思想(AST + YAML 规则),但我们解决的是一个根本不同的问题。
| | Semgrep | Aegis |
|---|---------|-------|
| **重点** | 通用 SAST | AI 行为安全 |
| **受众** | 安全工程师 | 使用 AI 助手的开发者 |
| **检测内容** | 通用漏洞 (SQLi, XSS) | 特定于 LLM 的故障模式 |
| **策略** | 广泛检测 | 精准优先(目标为零误报) |
| **输出** | 标准警告 | 解释引擎(解释 AI *为什么* 会犯这个错误) |
## GitHub Action
将 Aegis 引入你的 CI/CD 流水线。它将自动审计每一个 Pull Request。
```
name: Aegis Security Audit
on: [push, pull_request]
jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: vahapogut/aegis-action@v1
with:
format: 'sarif'
```
## 输出格式
Aegis 支持 JSON 和 SARIF 输出,以便轻松集成到 GitHub Security 标签页、Datadog 或自定义仪表板中。
```
aegis audit . --format json
aegis audit . --format text # default, colored terminal output
```
## 编写自定义规则
Aegis 规则是包含 Tree-sitter 查询的 YAML 文件。请参阅 [CONTRIBUTING.md](CONTRIBUTING.md) 获取完整指南。
```
rule:
id: "ai-your-pattern"
language: "typescript"
confidence: "HIGH"
message: "Short description of the bug."
explanation: "Why the AI does this and how to fix it."
query: >
(catch_clause
body: (statement_block) @block
(#not-match? @block "throw|return")
)
```
## 许可证
Apache-2.0。由 [IPEC Labs](mailto:info@ipeclabs.com) 为社区构建。
标签:AI安全, AI幻觉检测, AI辅助编程, Chat Copilot, CISA项目, DevSecOps, LLM生成代码检测, Rust, Tree-sitter, TypeScript安全, 上游代理, 代码安全扫描, 可视化界面, 安全合规, 开源安全工具, 本地化安全检测, 网络代理, 网络流量审计, 身份验证绕过, 逆向工程平台, 通知系统, 静态应用安全测试(SAST)