Skip to content

feat: add Cloudflare WAF service#155

Draft
K6gg wants to merge 1 commit into
chaitin:mainfrom
K6gg:feat/add-cloudflare-waf-service
Draft

feat: add Cloudflare WAF service#155
K6gg wants to merge 1 commit into
chaitin:mainfrom
K6gg:feat/add-cloudflare-waf-service

Conversation

@K6gg

@K6gg K6gg commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

接入设备

Cloudflare WAF — Cloudflare 边缘防火墙 / Web 应用防护(SaaS)。服务包 services/cloudflare__waf,proto 包 Cloudflare_WAF

设备版本

Cloudflare API v4https://api.cloudflare.com/client/v4),SaaS 无版本号、稳定 API。

认证方式

  • API Token(推荐)secret.apiToken,以 Authorization: Bearer <token> 发送;最小权限 Zone.Firewall Services + Zone.Zone Settings
  • 兼容 Legacy Global API Keysecret.authEmail + secret.authKeyX-Auth-Email / X-Auth-Key),仅在无 apiToken 时使用。

实现方法

方式:RPC(gRPC,5 个方法)

方法 类型 Cloudflare 端点 说明
BlockIP POST /…/firewall/access_rules/rules 创建 block 规则;按 target+mode 幂等去重,created_count 报告实际新建数
UnblockIP DELETE /…/firewall/access_rules/rules/{id} 删除匹配规则;无匹配则 deleted_count=0(幂等)
ListAccessRules GET /…/firewall/access_rules/rules 按 value/mode 过滤 + 分页
GetSecurityLevel GET /zones/{zone}/settings/security_level 安全等级查询
SetSecurityLevel PATCH /zones/{zone}/settings/security_level 安全等级设置(同值幂等)
  • 作用域:请求 account_id 优先,否则 zone_id(请求或配置)。
  • 错误映射:参数错误→INVALID_ARGUMENT,401/403 或 CF 错误码 9109/10000→PERMISSION_DENIED,4xx 或 success:falseFAILED_PRECONDITION,5xx/网络→UNAVAILABLE,非 JSON→UNKNOWN
  • 写操作声明默认参数(mode 默认 block)、幂等语义、回滚方式(Block⇄Unblock)和审计字段(x-request-id / x-engine-instance)。

测试命令

cd services
npm run validate -- --service-dir cloudflare__waf   # service package naming checks passed
npm test -- --service-dir cloudflare__waf            # 33/33 pass(行 100% / 分支 90%)
npm run pack:check                                   # ok

已知限制

  1. IP 封禁基于 IP Access Rules,未使用 Custom Rules / IP Lists。
  2. 仅 zone / account 维度,未覆盖 Cloudflare 全部 firewall 能力。
  3. SetSecurityLevel: under_attack 为高影响操作,建议加审批。
  4. 未做上游分页自动翻页(由调用方传 page/per_page 控制)。

真实设备验证

待补:在测试 Zone 用 scoped token 执行以下验证并附截图。

# 1) 封禁一个 TEST-NET 地址(写入测试对象)
curl -X POST "https://api.cloudflare.com/client/v4/zones/<zone_id>/firewall/access_rules/rules" \
  -H "Authorization: Bearer <token>" -H "Content-Type: application/json" \
  -d '{"mode":"block","configuration":{"target":"ip","value":"203.0.113.7"},"notes":"octobus test"}'
# -> {"success":true,"result":{"id":"<rule_id>", ...}}

# 2) 查询确认
curl "https://api.cloudflare.com/client/v4/zones/<zone_id>/firewall/access_rules/rules?configuration.value=203.0.113.7" \
  -H "Authorization: Bearer <token>"

# 3) 解封(清理测试对象)
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/<zone_id>/firewall/access_rules/rules/<rule_id>" \
  -H "Authorization: Bearer <token>"
方法 结果
BlockIP ✅ 规则创建
ListAccessRules ✅ 命中
UnblockIP ✅ 规则删除(清理完成)

(截图:Cloudflare Dashboard → Security → WAF → Tools 中可见对应 IP Access Rule 的创建与删除)

@innomentats

Copy link
Copy Markdown
Member

Review 阻塞:这个 service package PR 有测试文件,但我没有在 PR 描述、评论或改动文件中找到真实的测试截图证据(图片链接或提交的图片文件)。请补充能证明该 service package 跑通的截图,例如 npm test -- --service-dir <service> 通过,以及 OctoBus 实例调用/联调成功的截图。

@innomentats
innomentats self-requested a review June 26, 2026 09:48
@innomentats
innomentats marked this pull request as draft June 26, 2026 11:43
@K6gg
K6gg force-pushed the feat/add-cloudflare-waf-service branch from 8ecc90d to ce6fa8f Compare June 26, 2026 12:03
@monkeyscan

monkeyscan Bot commented Jun 26, 2026

Copy link
Copy Markdown

PR Title: feat: add Cloudflare WAF service

Commit: ce6fa8f

本次 PR 新增了多个 OctoBus Service Package(Cloudflare WAF、TopSec WAF、DefectDojo、Elastic Kibana、CloudAtlas、QiAnXin Hunter、Slack Group Robot、ThreatBook NGTIP 等),并扩展了 internal/packageimport 以支持从远程压缩包(.tgz/.zip)导入。

关键发现:

  1. TopSec WAF 存在高危安全缺陷login 函数通过修改全局环境变量 NODE_TLS_REJECT_UNAUTHORIZED 来禁用 TLS 校验,影响整个进程;且 AES-CBC 加密时错误地将密钥作为 IV 使用,破坏加密安全性。
  2. 远程包下载存在 DoS 风险internal/packageimport/importer.go 中的 downloadRemoteArchive 使用默认 http.DefaultClient(无超时),且未限制响应体大小,恶意服务端可能导致磁盘耗尽或进程挂死。
  3. 多处 Service 向 Node.js fetch 传入了非标准选项timeoutMsinsecureSkipVerify/tlsInsecureSkipVerify 等参数在 Node.js 原生 fetch 中无效,导致超时配置和 TLS 跳过校验策略不生效,已在 Cloudflare WAF、Elastic Kibana、CloudAtlas、Hunter、ThreatBook NGTIP、Slack Group Robot 等服务的实现中出现。

if (authEmail && authKey) {
return { 'x-auth-email': authEmail, 'x-auth-key': authKey };
}
throw errorWithCode('INVALID_ARGUMENT', 'apiToken (or authEmail + authKey) is required');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多处 Service 向 Node.js fetch 传入无效选项导致超时和 TLS 配置失效

callCloudflaretimeoutMs 以及 insecureSkipVerify/tlsInsecureSkipVerify 等选项直接展开到 fetch 的参数中。Node.js 原生 fetch(基于 undici)不支持这些选项,它们会被静默忽略。这导致配置的超时时间对请求毫无约束,网络异常时请求可能无限挂起;同时 TLS 跳过校验配置也不生效,遇到自签名证书时会连接失败。

Problem code:

Changed code at services/cloudflare__waf/src/cloudflare-waf.js:190

Recommendation:
使用 AbortSignal.timeout(timeoutMs) 实现请求超时;对于 TLS 跳过校验,应使用支持 rejectUnauthorized: false 的自定义 https.Agent 并通过 fetchdispatcher 选项(或全局 undici Agent)传入,而非传递无效选项。此问题在 elastic-kibana-7-17-26.jscloudatlas.jshunter.jsthreatbook-ngtip-v5.jsslack-group-robot.js 等文件中同样存在,建议一并修复。

@innomentats

Copy link
Copy Markdown
Member

Reviewer note: this PR currently has merge conflicts with the target branch, so it cannot be merged or reviewed safely in its current state.

Please rebase or merge the latest base branch, resolve the conflicts, and make sure GitHub Actions pass again. I am marking/keeping this PR as draft until the conflicts are resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants