|
8 | 8 | content: Configure WAF custom rules with Terraform |
9 | 9 | --- |
10 | 10 |
|
11 | | -import { Render } from "~/components"; |
| 11 | +import { Render, GlossaryTooltip } from "~/components"; |
12 | 12 |
|
13 | 13 | This page provides examples of creating WAF custom rules in a zone or account using Terraform. The examples cover the following scenarios: |
14 | 14 |
|
15 | 15 | - Zone-level configurations: |
16 | | - |
17 | 16 | - [Add a custom rule to a zone](#add-a-custom-rule-to-a-zone) |
18 | | - |
| 17 | + - [Add a custom rule challenging requests with leaked credentials](#add-a-custom-rule-challenging-requests-with-leaked-credentials) |
| 18 | + - [Add a custom rule blocking malicious uploads](#add-a-custom-rule-blocking-malicious-uploads) |
19 | 19 | - Account-level configurations: |
20 | | - |
21 | 20 | - [Create and deploy a custom ruleset](#create-and-deploy-a-custom-ruleset) |
22 | 21 | - [Add a custom rule checking for exposed credentials](#add-a-custom-rule-checking-for-exposed-credentials) |
23 | 22 |
|
@@ -60,14 +59,67 @@ resource "cloudflare_ruleset" "zone_custom_firewall" { |
60 | 59 |
|
61 | 60 | <Render file="add-new-rule" params={{ one: "custom rule" }} /> <br /> |
62 | 61 |
|
| 62 | +### Add a custom rule challenging requests with leaked credentials |
| 63 | + |
| 64 | +:::note |
| 65 | +For more information on enabling leaked credentials detection using Terraform, refer to the [leaked credentials detection](/waf/detections/leaked-credentials/get-started/#1-turn-on-leaked-credentials-detection) documentation. |
| 66 | +::: |
| 67 | + |
| 68 | +This example adds a custom rule that challenges requests with leaked credentials by using one of the [leaked credentials fields](/waf/detections/leaked-credentials/#leaked-credentials-fields) in the rule expression. |
| 69 | + |
| 70 | +```tf |
| 71 | +resource "cloudflare_ruleset" "zone_custom_firewall_leaked_creds" { |
| 72 | + zone_id = "<ZONE_ID>" |
| 73 | + name = "Phase entry point ruleset for custom rules in my zone" |
| 74 | + description = "" |
| 75 | + kind = "zone" |
| 76 | + phase = "http_request_firewall_custom" |
| 77 | +
|
| 78 | + rules { |
| 79 | + ref = "challenge_leaked_username_password" |
| 80 | + description = "Challenge requests with a leaked username and password" |
| 81 | + expression = "(cf.waf.credential_check.username_and_password_leaked)" |
| 82 | + action = "managed_challenge" |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +For more information on configuring custom detection locations, refer to the [Terraform example](/waf/detections/leaked-credentials/get-started/#4-optional-configure-a-custom-detection-location) in the WAF documentation. |
| 88 | + |
| 89 | +### Add a custom rule blocking malicious uploads |
| 90 | + |
| 91 | +:::note |
| 92 | +For more information on enabling malicious uploads detection using Terraform, refer to the [malicious uploads detection](/waf/detections/malicious-uploads/get-started/#1-turn-on-the-detection) documentation. |
| 93 | +::: |
| 94 | + |
| 95 | +This example adds a custom rule that blocks requests with one or more <GlossaryTooltip term="content object">content objects</GlossaryTooltip> considered malicious by using one of the [content scanning fields](/waf/detections/malicious-uploads/#content-scanning-fields) in the rule expression. |
| 96 | + |
| 97 | +```tf |
| 98 | +resource "cloudflare_ruleset" "zone_custom_firewall_malicious_uploads" { |
| 99 | + zone_id = "<ZONE_ID>" |
| 100 | + name = "Phase entry point ruleset for custom rules in my zone" |
| 101 | + description = "" |
| 102 | + kind = "zone" |
| 103 | + phase = "http_request_firewall_custom" |
| 104 | +
|
| 105 | + rules { |
| 106 | + ref = "block_malicious_uploads" |
| 107 | + description = "Block requests uploading malicious content objects" |
| 108 | + expression = "(cf.waf.content_scan.has_malicious_obj and http.request.uri.path eq \"/upload.php\")" |
| 109 | + action = "block" |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +For more information on configuring custom scan expressions, refer to the [Terraform example](/waf/detections/malicious-uploads/get-started/#4-optional-configure-a-custom-scan-expression) in the WAF documentation. |
| 115 | + |
63 | 116 | ## Account-level configurations |
64 | 117 |
|
65 | 118 | ### Create and deploy a custom ruleset |
66 | 119 |
|
67 | 120 | The following example creates a [custom ruleset](/ruleset-engine/custom-rulesets/) in the account with ID `<ACCOUNT_ID>` containing a single custom rule. This custom ruleset is then deployed using a separate `cloudflare_ruleset` Terraform resource. If you do not deploy a custom ruleset, it will not execute. |
68 | 121 |
|
69 | 122 | :::caution |
70 | | - |
71 | 123 | You can only create and deploy custom rulesets at the account level. |
72 | 124 | ::: |
73 | 125 |
|
@@ -123,12 +175,11 @@ For more information on configuring and deploying custom rulesets, refer to [Wor |
123 | 175 |
|
124 | 176 | ### Add a custom rule checking for exposed credentials |
125 | 177 |
|
126 | | -The following configuration creates a custom ruleset with a single rule that [checks for exposed credentials](/waf/managed-rules/check-for-exposed-credentials/configure-api/#create-a-custom-rule-checking-for-exposed-credentials). |
| 178 | +<Render file="leaked-credentials-recommend-detection" product="waf" /> |
127 | 179 |
|
128 | | -:::caution |
| 180 | +The following configuration creates a custom ruleset with a single rule that [checks for exposed credentials](/waf/managed-rules/check-for-exposed-credentials/configure-api/#create-a-custom-rule-checking-for-exposed-credentials). |
129 | 181 |
|
130 | 182 | You can only add exposed credential checks to rules in a custom ruleset (that is, a ruleset with `kind = "custom"`). |
131 | | -::: |
132 | 183 |
|
133 | 184 | ```tf |
134 | 185 | resource "cloudflare_ruleset" "account_firewall_custom_ruleset_exposed_creds" { |
|
0 commit comments