为什么 Cloudflare 规则的顺序很重要?
Why Cloudflare rule order matters?

原始链接: https://www.brzozowski.io/web-applications/2025/03/11/why-cloudflare-rule-order-matters.html

本文详细介绍Cloudflare规则引擎中一个关键且经常被误解的方面:**规则评估顺序至关重要,因为存在“终止动作”。** 作者发现,一个旨在保护`/metrics`端点的`Block`规则,在放置在`JS Challenge`规则*之后*时会被绕过。这是因为完成挑战会设置一个`cf_clearance` cookie,从而自动终止进一步的规则评估——有效地绕过了`Block`规则。 Cloudflare将某些动作(如`Block`、`JS Challenge`等)定义为“终止动作”。如果触发了终止动作,则后续的规则将*不会*被处理。 作者建议按动作类型组织规则,将限制性动作(如`Block`)优先于挑战动作,以确保安全性。虽然未确认大规模的可利用性,但该问题凸显了Cloudflare仪表板中关于规则执行顺序的误导性信息所带来的潜在漏洞。这个问题已被用户注意到多年,正如Serverfault上的类似讨论所示。

一个 Hacker News 的讨论强调了 Cloudflare 规则引擎一个关键且经常被误解的方面:**规则顺序至关重要。** 用户发现,某些“终止操作”,例如“Managed Challenge”,会立即停止规则评估,即使仪表盘暗示后续规则仍会被检查。 许多评论者对 Cloudflare 的文档和支持表示沮丧,指出信息不一致,并且普遍缺乏对这种行为的清晰解释。UI 没有清楚地表明规则何时会停止进一步评估,导致调试困难——尤其是在与其他防火墙系统(如 UFW)结合使用时。 对话也转向了对 Cloudflare 整体“自行解决”文档风格的抱怨,以及关于一位以私人飞机出行问题而闻名的苛刻人士的讨论。最终,核心要点是理解 Cloudflare 的规则处理顺序对于有效配置至关重要。
相关文章

原文

Cloudflare logo.

Before jumping into this article please take a look at the following Cloudflare ruleset and think for a while what is wrong with it?

Seemingly safe Cloudflare rule order.

I set up above rules and thought they would work like the following:

Cloudflare rule schema (logo).

From the first glance it seems perfectly fine. Website administrator wants to challenge users opening the website to prevent bot traffic. Additionally he specifies a rule that blocks access to /metrics endpoint to prevent unauthorized access to Prometheus metrics. I was surprised when I enabled both rules and the this happened:

Result of enabled Cloudflare rules.

What is going on here?! The Block rule was never executed and anyone could access my precious /metrics endpoint! Let me explain what is going on. Cloudflare rules language consists of several Actions. Action is a result of a matching rule. As for now, for custom security rules the following actions are allowed:

  • Interactive Challenge
  • JS Challenge
  • Managed Challenge
  • Block
  • Skip
  • Log
  • Execute
  • Rewrite
  • Redirect
  • Route
  • Set Configuration
  • Compress Response
  • Set Cache Settings
  • Serve Error
  • Log custom field

Some actions however are Terminating Actions. Terminating action will stop the evaluation of the remaining rules. This means that any rules, that are ordered AFTER a terminating action, will not be evaluated if the Terminating Action is executed. Cloudflare recognizes the following Terminating Actions:

  • Interactive Challenge
  • JS Challenge
  • Managed Challenge
  • Block
  • Redirect
  • Serve Error
  • Log custom field

This behavior is especially important when configuring Block rules that have real security impact. If a rule that restricts access to a resource is placed AFTER i.e. JS Challenge action, it can be easily bypassed when a client completes the challenge and gets cf_clearance cookie assigned. With this cookie, Cloudflare automatically clears request of any challenge rules meaning rule evaluation will be terminated.

To sum up, to securely configure Cloudflare custom security rules, they should be grouped by resulting action and placed in the following order:

  1. Skip - Whitelist trusted IPs/services,
  2. Block - Most restrictive rules, cannot be bypassed,
  3. Log - Capture traffic before modifications,
  4. Redirect - URL redirections,
  5. Serve Error - Custom error pages,
  6. Execute - Transformation rules,
  7. Rewrite - Transformation rules,
  8. Route - Transformation rules,
  9. Set Configuration - Configuration rules,
  10. Compress Response - Performance rules,
  11. Set Cache Settings - Caching rules,
  12. Log custom field - Custom logging,
  13. Interactive Challenge - Challenge suspicious traffic,
  14. JS Challenge - Challenge suspicious traffic,
  15. Managed Challenge - Challenge suspicious traffic

And what about exploitability? Unfortunatelly, I was not able to confirm that this issue is exploitable at scale which can mean two things. Either my testing methodology (fuzzing with cf_clearance cookie) was badly adjusted for this research, or this type of misconfigurations are very uncommon, and hard to detect in blackbox scenario.

In other way this seems to be intended behavior even though Cloudflare dashboard is straight lying to you by saying that Block rule will execute after Challenge action:

Cloudflare UI not at it's prime. Above statement is not true, as the rule will never be evaluated after “Force bot detection” rule.

It was also funny to discover this exact issue discussed on serverfault.com forum some years ago. Looks like people had already problems understanding Cloudflare security rules a while ago.

References:

https://developers.cloudflare.com/ruleset-engine/rules-language/actions/ https://serverfault.com/questions/1059124/how-does-cloudflare-firwall-rules-order-work

联系我们 contact @ memedata.com