为什么 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上的类似讨论所示。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 为什么 Cloudflare 规则顺序很重要? (brzozowski.io) 5 分,redfr0g 1小时前 | 隐藏 | 过去 | 收藏 | 1 条评论 帮助 flarite 17分钟前 | 下一个 [–] 有趣,我一直不知道将动作设置为“托管挑战”会等同于跳过所有剩余规则,而我从事多个企业 Cloudflare 账户管理已有数年。 感谢。回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

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