Djones-qa/shift-left-quality-engine
GitHub: Djones-qa/shift-left-quality-engine
一款在开发早期阶段自动检测代码质量、复杂度与常见安全模式的静态分析引擎。
Stars: 0 | Forks: 0
# 左移质量引擎
[](https://github.com/Djones-qa/shift-left-quality-engine/actions/workflows/ci.yml)
[](https://www.typescriptlang.org/)
[](https://nodejs.org/)
[](https://www.docker.com/)
[](https://jestjs.io/)
[](./LICENSE)
左移质量引擎 - 自定义规则评估、圈复杂度分析、安全模式检测、pre-commit 集成。在测试运行之前就能捕获 bug。
## 功能
- **自定义规则引擎** - 通过 YAML 定义的 regex 规则,由 Zod 进行验证,支持严重级别和分类
- **圈复杂度分析** - 按函数计算并强制执行复杂度阈值
- **安全模式检测** - 检测硬编码密钥、SQL 注入、eval() 使用、innerHTML XSS
- **Pre-commit 集成** - 阻止违反质量规则的提交
- **可配置阈值** - 设置复杂度限制,启用/禁用安全扫描
- **类别分类** - 规则按 security、complexity、style、anti-pattern 分组
- **REST API** - 提供用于 CI/CD 集成的健康检查和扫描 endpoint
- **Docker 就绪** - 多阶段生产构建
## 架构
```
+-----------------------------------------------------------+
| Shift-Left Quality Engine |
| |
| +-----------+ +--------------+ +----------------+ |
| | Rules | | Analyzers | | Engine | |
| | Engine | | | | | |
| +-----------+ +--------------+ +----------------+ |
| | - Parser | | - Complexity | | - Scanner | |
| | - Eval | | - Security | | - Orchestrator | |
| | - Types | | | | - Results | |
| +-----------+ +--------------+ +----------------+ |
| |
| +-----------------------------------------------------+ |
| | Express REST API (port 3006) | |
| +-----------------------------------------------------+ |
+-----------------------------------------------------------+
```
## 快速开始
### 前置条件
- Node.js >= 20.0.0
- Docker(可选)
### 安装
```
git clone https://github.com/Djones-qa/shift-left-quality-engine.git
cd shift-left-quality-engine
npm install
cp .env.example .env
npm run dev
```
### 运行测试
```
npm test # All tests
npm run test:unit # Unit tests only
npm run test:coverage # With coverage
npm run lint # ESLint
```
## 规则定义 (YAML)
```
rules:
- id: no-console-log
name: No Console Log
severity: warning
pattern: "console\\.log"
message: "Avoid console.log in production code"
category: style
- id: no-var-keyword
name: No Var Keyword
severity: error
pattern: "\\bvar\\b"
message: "Use const or let instead of var"
category: anti-pattern
- id: no-hardcoded-secret
name: No Hardcoded Secrets
severity: error
pattern: "(password|api_key|secret)\\s*=\\s*['\"][^'\"]+['\"]"
message: "Do not hardcode secrets in source code"
category: security
```
## 检测到的安全模式
| 模式 | 严重级别 | 描述 |
|---------|----------|-------------|
| 硬编码密钥 | error | 检测带有值的 password=、api_key=、secret= |
| SQL 注入 | error | 在 query/execute 调用中使用字符串拼接 |
| eval() 使用 | error | 直接使用 eval() 函数 |
| innerHTML | warning | 直接进行 innerHTML 赋值(存在 XSS 风险) |
## 复杂度阈值
圈复杂度通过计算以下内容得出:
| 构造 | 增加 |
|-----------|------|
| if / else | 各 +1 |
| for / while | 各 +1 |
| switch / case | 各 +1 |
| && / II | 各 +1 |
| ? (三元) | +1 |
| 基础 | +1 |
默认阈值:**10**(可通过 MAX_COMPLEXITY 环境变量进行配置)
## 配置
| 变量 | 默认值 | 描述 |
|----------|---------|-------------|
| PORT | 3006 | 服务器端口 |
| NODE_ENV | development | 环境 |
| LOG_LEVEL | info | Winston 日志级别 |
| MAX_COMPLEXITY | 10 | 复杂度阈值 |
| ENABLE_SECURITY | true | 启用安全扫描 |
## API Endpoint
| 方法 | Endpoint | 描述 |
|--------|----------|-------------|
| GET | /api/health | 服务健康检查 |
## CI/CD Pipeline
1. **Lint 与类型检查** - ESLint + TypeScript 编译器
2. **单元测试** - Jest 含覆盖率
3. **Docker 构建** - 多阶段生产镜像
## 作者
**Darrius Jones**
- GitHub: [@Djones-qa](https://github.com/Djones-qa)
- LinkedIn: [darrius-jones-28226b350](https://www.linkedin.com/in/darrius-jones-28226b350)
## 许可证
MIT - 2026 Darrius Jones
详情请查看 [LICENSE](./LICENSE)。
标签:GNU通用公共许可证, MITM代理, Node.js, Shift-left, TypeScript, URL发现, 安全插件, 安全检测, 网络测绘, 自动化攻击, 请求拦截, 错误基检测, 静态代码分析