imposeCosts/graphql-waf
GitHub: imposeCosts/graphql-waf
基于 Rust 的高性能反向代理 GraphQL WAF,结合 ModSecurity 规则与 wirefilter 表达式匹配,为 GraphQL API 提供请求检测、审计与拦截能力。
Stars: 0 | Forks: 0
# graphql-waf
一个 modescurity + graphql WAF。使用 rust 编写,可扩展且高速。
## 运行
这是一个**反向代理 WAF**:它监听 `--listen-host/--listen-port`,并使用以下方式检查请求:
- 通过 [`zentinel-modsec`](https://crates.io/crates/zentinel-modsec) 的 **ModSecurity rules**
- 通过 Cloudflare 的 [`wirefilter`](https://github.com/cloudflare/wirefilter) 进行 **expression matching**
然后它会**转发**、**审计**或**阻止**该请求。
## 静态分析 (Semgrep)
安装并运行 Semgrep:
```
make semgrep-install
make semgrep
```
## 本地上游服务器(用于代理性能测试)
此仓库包含一个小型“DVGA-like” GraphQL 上游,您可以在本地运行。它旨在生成**繁重的查询结构**(深度嵌套、大型列表、CPU 相关的计算任务),以便您可以测量代理开销并调整 WAF 行为。
### 启动上游
```
make upstream-run
```
- GraphQL endpoint:`http://127.0.0.1:4000/graphql`
- Playground UI:`http://127.0.0.1:4000/graphiql`
### 在其前面启动 WAF
```
make waf-run ARGS='--listen-host 127.0.0.1 --listen-port 8080 --mode audit'
```
然后将客户端指向:
- `http://127.0.0.1:8080/graphql`(通过 WAF 代理)
### 示例查询(适合性能比较)
基线:
```
query { ping }
```
深度嵌套(深度/递归压力):
```
query {
nestedNode(depth: 60) {
id
name
depth
child { id name depth child { id name depth } }
}
}
```
大型 payload(序列化/带宽压力):
```
query { bigList(size: 5000) { id name } }
```
CPU 相关的计算任务(服务器端计算压力):
```
query { fib(n: 42) }
```
### 示例
```
cargo run -- \
--config waf.example.toml \
--listen-host 127.0.0.1 \
--listen-port 8080 \
--upstream http://127.0.0.1:4000 \
--mode block \
--graphql-security \
--block-introspection \
--modsec-rules rules/modsec-example.conf \
--wirefilter 'http.method == "POST" && http.request.uri.path contains "graphql"'
```
### 模式
- **`off`**:不进行检查,纯粹的反向代理
- **`audit`**:记录决策日志(从不阻止)
- **`block`**:当 ModSecurity 触发或 Wirefilter 匹配时进行阻止
### 可用的 Wirefilter 字段
内置 scheme 包括:
- `http.method`
- `http.host`
- `http.user_agent`
- `http.content_type`
- `http.request.uri.path`
- `http.request.uri.query`
- `http.request.headers.authorization`
- `http.request.headers.x_forwarded_for`
- `http.request.headers.x_real_ip`
标签:AppImage, GraphQL, ModSecurity, Rust, WAF, Web应用防火墙, 反向代理, 可视化界面, 网络安全, 网络流量审计, 通知系统, 隐私保护