jonasbb/cloudflare-rule-linter

GitHub: jonasbb/cloudflare-rule-linter

Cloudflare 规则语言 Linter,检测规则语法与逻辑问题并给出优化建议。

Stars: 2 | Forks: 0

# Cloudflare 规则语言 Linter Cloudflare 允许在其[规则语言](https://developers.cloudflare.com/ruleset-engine/rules-language/)中编写过滤规则和防火墙规则。 它们受到 Wireshark 过滤器的启发。 这些过滤器可能变得非常复杂,逻辑错误可能导致阻止所有网站流量的严重后果。 该仓库实现了规则集引擎的 Linter。 它基于[Cloudflare wirefilter](https://github.com/cloudflare/wirefilter),这是他们用于解析和执行规则的工程。 ## 示例 ``` warning[deprecated_field]: Found deprecated field ip.geoip.country ╭▸ ./test/example_readme.tfvars:4:27 │ 3 │ rule_deprecated_field { 4 │ expression = "ip.geoip.country eq \"T1\"" │ ━━━━━━━━━━━━━━━━━━━━━━━━━━ The value `ip.geoip.country` should be replaced with `ip.src.country`. ╰╴ warning[duplicate_list_entries]: Found duplicate entry in list ╭▸ ./test/example_readme.tfvars:7:27 │ 6 │ rule_duplicate_list_entries { 7 │ expression = "ip.src in {1.0.0.0/8 1.1.0.0/16}" ╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ The values `1.0.0.0/8` and `1.1.0.0/16` overlap. warning[reserved_ip_space]: Found usage of reserved IP range ╭▸ ./test/example_readme.tfvars:10:27 │ 9 │ rule_reserved_ip_space { 10 │ expression = "ip.src eq 192.168.0.1" ╰╴ ━━━━━━━━━━━━━━━━━━━━━ The value `192.168.0.1` is within reserved address space. warning[negated_comparison]: Found negated comparison ╭▸ ./test/example_readme.tfvars:13:27 │ 12 │ rule_negated_comparison { 13 │ expression = "not http.host eq \"example.com\"" ╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Consider simplifying from `not http.host eq "example.com"` to `http.host ne "example.com"` warning[regex_raw_strings]: Found regex match with non-raw string ╭▸ ./test/example_readme.tfvars:16:27 │ 15 │ rule_regex_raw_string { 16 │ expression = "http.host matches \"example\\.com\"" ╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Regex matches must use raw string literals (e.g., r"..." or r#"..."#) when using the `matches` operator. warning[illogical_condition]: Found illogical condition with AND ╭▸ ./test/example_readme.tfvars:19:28 │ 18 │ rule_illogical_condition { 19 │ expression = "(http.host in { \"example.com\" }) and (http.host eq \"example.org\")" ╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ The value `http.host` is compared for equality multiple times in an AND expression. ``` ## 方案 此代码依赖于手动同步 Cloudflare 文档中的规则方案。 [字段](https://developers.cloudflare.com/ruleset-engine/rules-language/fields/)和[函数](https://developers.cloudflare.com/ruleset-engine/rules-language/functions/)已按类型记录。 如果您注意到此处实现的方案与 Cloudflare 实现之间的任何差异,请打开 Issue 或 PR。 ## Lint 检查项 | 名称 | 类别 | 描述 | | --- | --- | --- | | [deprecated_field](./docs/deprecated_field.md) | deprecated | 检查是否使用了旧字段名并建议正确的值。 | | [duplicate_list_entries](./docs/duplicate_list_entries.md) | correctness | 检测列表中的重复值。 | | [empty_list](./docs/empty_list.md) | correctness | 检查与空列表的比较,这总是为假。 | | [header_case](./docs/header_case.md) | correctness | 检查标题名是否并非全部为小写。 | | [illogical_condition](./docs/illogical_condition.md) | style | 检测不合逻辑的条件,例如在 AND 表达式中对同一字段进行多次相等比较,或在 OR 表达式中对同一字段进行多次不等比较。 | | [invalid_list_name](./docs/invalid_list_name.md) | correctness | 检查托管列表名是否无效,以及可选的定制列表名是否无效。 | | [negated_comparison](./docs/negated_comparison.md) | style | 检测被否定的比较并建议改用相反的操作符。 | | [operator_style](./docs/operator_style.md) | style | 强制使用一致的操作符表示法(英文风格或 C 风格)。 | | [overly_permissive_pattern](./docs/overly_permissive_pattern.md) | correctness | 检查过于宽松的匹配正则表达式与通配符模式。 | | [regex_raw_strings](./docs/regex_raw_strings.md) | style | 确保匹配正则表达式使用原始字符串字面量(r"...")而非普通引号字符串。 | | [reserved_ip_space](./docs/reserved_ip_space.md) | correctness | 检查是否使用了保留的 IP 范围,这些范围在规则中可能没有实际用途。 | | [timestamp_comparisons](./docs/timestamp_comparisons.md) | correctness | 检测针对 http.request.timestamp.sec 的比较是否使用了超出合理范围的值。 | | [unnecessary_patterns](./docs/unnecessary_patterns.md) | style | 检测可以简化为 `eq` 或 `contains` 表达式的正则表达式与通配符模式。 | | [value_domain](./docs/value_domain.md) | correctness | 检查某些字段的值是否超出有效范围,例如无效的 HTTP 方法或无效的大洲。 | ## 链接 * 关于 Cloudflare 规则语言、可用表达式、操作符、值、字段和函数的 Cloudflare 文档。 * 包含用于解析 Cloudflare 规则语言的 Rust 代码的 Cloudflare 仓库。 这提供了基础框架,但可用字段和函数的具体细节未包含在内。 * Cloudflare wirefilter 的分支,预定义了更多函数。 * 一个简单的 WebUI,用于模拟 Cloudflare 规则语言、解析它并报告错误。 这基于 wirefilter crate。 * Wirefilter 字段和类型。 *
标签:CLI, Cloudflare, Go, Linter, MITRE ATT&CK, pptx, Ruby工具, WiFi技术, 二进制发布, 云安全监控, 云计算, 可视化界面, 安全合规, 开源工具, 网络代理, 网络过滤, 规则引擎, 规则语言, 语法检查, 过滤器, 通知系统, 防火墙规则, 静态分析