Skip to content

Commit dd4d075

Browse files
pedrosousaOxyjun
authored andcommitted
[WAF] Improve linking between API and Terraform (#21741)
* Add links between API and Terraform sections * Move Terraform examples of using traffic detections to WAF docs * Move custom detection location examples to concept section * Apply suggestions from PCX review --------- Co-authored-by: Jun Lee <[email protected]>
1 parent 3a4cac9 commit dd4d075

24 files changed

+327
-226
lines changed

src/content/docs/terraform/additional-configurations/waf-custom-rules.mdx

Lines changed: 11 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ import { Render, GlossaryTooltip } from "~/components";
1313

1414
This page provides examples of creating [WAF custom rules](/waf/custom-rules/) in a zone or account using Terraform. The examples cover the following scenarios:
1515

16-
- Zone-level configurations:
17-
- [Add a custom rule to a zone](#add-a-custom-rule-to-a-zone)
18-
- [Add a custom rule challenging requests with leaked credentials](#add-a-custom-rule-challenging-requests-with-leaked-credentials)
19-
- [Add a custom rule blocking malicious uploads](#add-a-custom-rule-blocking-malicious-uploads)
20-
- Account-level configurations:
21-
- [Create and deploy a custom ruleset](#create-and-deploy-a-custom-ruleset)
22-
- [Add a custom rule checking for exposed credentials](#add-a-custom-rule-checking-for-exposed-credentials)
16+
- [Add a custom rule to a zone](#add-a-custom-rule-to-a-zone) (zone-level configuration)
17+
- [Create and deploy a custom ruleset](#create-and-deploy-a-custom-ruleset) (account-level configuration)
18+
19+
The WAF documentation includes additional Terraform examples — refer to [More resources](#more-resources).
2320

2421
## Before you start
2522

@@ -33,9 +30,7 @@ This page provides examples of creating [WAF custom rules](/waf/custom-rules/) i
3330

3431
---
3532

36-
## Zone-level configurations
37-
38-
### Add a custom rule to a zone
33+
## Add a custom rule to a zone
3934

4035
The following example configures a custom rule in the zone entry point ruleset for the `http_request_firewall_custom` phase for zone with ID `<ZONE_ID>`. The rule will block all traffic on non-standard HTTP(S) ports:
4136

@@ -60,75 +55,17 @@ resource "cloudflare_ruleset" "zone_custom_firewall" {
6055

6156
<Render file="add-new-rule" params={{ ruleType: "custom rule" }} /> <br />
6257

63-
### Add a custom rule challenging requests with leaked credentials
64-
65-
:::note
66-
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-the-detection) documentation.
67-
:::
68-
69-
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.
70-
71-
<Render file="v4-code-snippets" />
72-
73-
```tf
74-
resource "cloudflare_ruleset" "zone_custom_firewall_leaked_creds" {
75-
zone_id = "<ZONE_ID>"
76-
name = "Phase entry point ruleset for custom rules in my zone"
77-
description = ""
78-
kind = "zone"
79-
phase = "http_request_firewall_custom"
80-
81-
rules {
82-
ref = "challenge_leaked_username_password"
83-
description = "Challenge requests with a leaked username and password"
84-
expression = "(cf.waf.credential_check.username_and_password_leaked)"
85-
action = "managed_challenge"
86-
}
87-
}
88-
```
89-
90-
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.
91-
92-
### Add a custom rule blocking malicious uploads
58+
## Create and deploy a custom ruleset (account-level configuration) {/* create-and-deploy-a-custom-ruleset */}
9359

9460
:::note
95-
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.
61+
Account-level WAF configuration requires an Enterprise plan with a paid add-on.
9662
:::
9763

98-
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.
99-
100-
<Render file="v4-code-snippets" />
101-
102-
```tf
103-
resource "cloudflare_ruleset" "zone_custom_firewall_malicious_uploads" {
104-
zone_id = "<ZONE_ID>"
105-
name = "Phase entry point ruleset for custom rules in my zone"
106-
description = ""
107-
kind = "zone"
108-
phase = "http_request_firewall_custom"
109-
110-
rules {
111-
ref = "block_malicious_uploads"
112-
description = "Block requests uploading malicious content objects"
113-
expression = "(cf.waf.content_scan.has_malicious_obj and http.request.uri.path eq \"/upload.php\")"
114-
action = "block"
115-
}
116-
}
117-
```
118-
119-
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.
120-
121-
## Account-level configurations
122-
123-
### Create and deploy a custom ruleset
124-
12564
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.
12665

127-
:::caution
12866
You can only create and deploy custom rulesets at the account level.
129-
:::
13067

131-
The following configuration creates the custom ruleset with a single rule:
68+
The following configuration creates a custom ruleset with a single rule:
13269

13370
<Render file="v4-code-snippets" />
13471

@@ -183,68 +120,7 @@ resource "cloudflare_ruleset" "account_firewall_custom_entrypoint" {
183120

184121
For more information on configuring and deploying custom rulesets, refer to [Work with custom rulesets](/ruleset-engine/custom-rulesets/) in the Ruleset Engine documentation.
185122

186-
### Add a custom rule checking for exposed credentials
123+
## More resources
187124

188-
<Render file="leaked-credentials-recommend-detection" product="waf" />
189-
190-
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).
191-
192-
You can only add exposed credential checks to rules in a custom ruleset (that is, a ruleset with `kind = "custom"`).
193-
194-
<Render file="v4-code-snippets" />
195-
196-
```tf
197-
resource "cloudflare_ruleset" "account_firewall_custom_ruleset_exposed_creds" {
198-
account_id = "<ACCOUNT_ID>"
199-
name = "Custom ruleset checking for exposed credentials"
200-
description = ""
201-
kind = "custom"
202-
phase = "http_request_firewall_custom"
203-
204-
rules {
205-
ref = "check_for_exposed_creds_add_header"
206-
description = "Add header when there is a rule match and exposed credentials are detected"
207-
expression = "http.request.method == \"POST\" && http.request.uri == \"/login.php\""
208-
action = "rewrite"
209-
action_parameters {
210-
headers {
211-
name = "Exposed-Credential-Check"
212-
operation = "set"
213-
value = "1"
214-
}
215-
}
216-
exposed_credential_check {
217-
username_expression = "url_decode(http.request.body.form[\"username\"][0])"
218-
password_expression = "url_decode(http.request.body.form[\"password\"][0])"
219-
}
220-
}
221-
}
222-
```
223-
224-
<Render file="add-new-rule" params={{ ruleType: "rule" }} /> <br />
225-
226-
The following configuration deploys the custom ruleset. It defines a dependency on the `account_firewall_custom_ruleset_exposed_creds` resource and obtains the ID of the created custom ruleset:
227-
228-
<Render file="v4-code-snippets" />
229-
230-
```tf
231-
resource "cloudflare_ruleset" "account_firewall_custom_entrypoint" {
232-
account_id = "<ACCOUNT_ID>"
233-
name = "Account-level entry point ruleset for the http_request_firewall_custom phase deploying a custom ruleset checking for exposed credentials"
234-
description = ""
235-
kind = "root"
236-
phase = "http_request_firewall_custom"
237-
238-
depends_on = [cloudflare_ruleset.account_firewall_custom_ruleset_exposed_creds]
239-
240-
rules {
241-
ref = "deploy_custom_ruleset_example_com"
242-
description = "Deploy custom ruleset for example.com"
243-
expression = "(cf.zone.name eq \"example.com\")"
244-
action = "execute"
245-
action_parameters {
246-
id = cloudflare_ruleset.account_firewall_custom_ruleset_exposed_creds.id
247-
}
248-
}
249-
}
250-
```
125+
- [Malicious uploads detection: Add a custom rule to block malicious uploads](/waf/detections/malicious-uploads/terraform-examples/#add-a-custom-rule-to-block-malicious-uploads)
126+
- [Leaked credentials detection: Add a custom rule to challenge requests with leaked credentials](/waf/detections/leaked-credentials/terraform-examples/#add-a-custom-rule-to-challenge-requests-with-leaked-credentials)

src/content/docs/waf/analytics/security-analytics.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ To apply the filters for an insight to the data displayed in the Security Analyt
9292

9393
### Score-based analyses
9494

95-
The **Attack analysis**, **Bot analysis**, **Malicious uploads**, and **Account abuse detection** sections display statistics related to WAF attack scores, bot scores, WAF content scanning scores, and leaked credentials scanning of incoming requests for the selected time frame. All plans include access to the **Leaked credential check** under **Account abuse detection**. This feature detects login attempts using credentials that have been exposed online. For more information on what to do if you have credentials that have been leaked, refer to the [mitigation examples page](/waf/detections/leaked-credentials/examples/).
95+
The **Attack analysis**, **Bot analysis**, **Malicious uploads**, and **Account abuse detection** sections display statistics related to WAF attack scores, bot scores, WAF content scanning scores, and leaked credentials scanning of incoming requests for the selected time frame. All plans include access to the **Leaked credential check** under **Account abuse detection**. This feature detects login attempts using credentials that have been exposed online. For more information on what to do if you have credentials that have been leaked, refer to the [example mitigation rules page](/waf/detections/leaked-credentials/examples/).
9696

9797
You can examine different traffic segments according to the current metric (attack score, bot score, or content scanning). To apply score filters for different segments, select the buttons below the traffic chart. For example, select **Likely attack** under **Attack analysis** to filter requests that are likely an attack (requests with WAF attack score values between 21 and 50).
9898

src/content/docs/waf/detections/attack-score.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,4 @@ If you are an Enterprise customer and you created a rule with _Log_ action, chan
102102

103103
## Additional remarks
104104

105-
The WAF Attack Score is different from Bot Score. WAF Attack Score identifies variation of attacks that WAF Managed Rules do not catch, while Bot Score identifies bots.
105+
The WAF attack score is different from [bot score](/bots/concepts/bot-score/). WAF attack score identifies variations of attacks that WAF Managed Rules do not catch, while bot score identifies bots.

src/content/docs/waf/detections/leaked-credentials/api-calls.mdx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ head:
1010

1111
import { APIRequest } from "~/components";
1212

13+
The following examples address common scenarios of using the Cloudflare API to manage and configure leaked credentials detection.
14+
15+
If you are using Terraform, refer to [Terraform configuration examples](/waf/detections/leaked-credentials/terraform-examples/).
16+
1317
## General operations
1418

1519
The following API examples cover basic operations such as enabling and disabling the leaked credentials detection.
@@ -53,11 +57,11 @@ To obtain the current status of the leaked credentials detection, use a `GET` re
5357

5458
## Custom detection location operations
5559

56-
The following API examples cover operations on custom detection locations for leaked credentials detection.
60+
The following API examples cover operations on [custom detection locations](/waf/detections/leaked-credentials/#custom-detection-locations) for leaked credentials detection.
5761

5862
### Add a custom detection location
5963

60-
Use a `POST` request similar to the following:
64+
To add a custom detection location, use a `POST` request similar to the following:
6165

6266
<APIRequest
6367
path="/zones/{zone_id}/leaked-credential-checks/detections"
@@ -95,7 +99,7 @@ To get a list of existing custom detection locations, use a `GET` request simila
9599

96100
### Delete a custom detection location
97101

98-
Use a `DELETE` request similar to the following:
102+
To delete a custom detection location, use a `DELETE` request similar to the following:
99103

100104
<APIRequest
101105
path="/zones/{zone_id}/leaked-credential-checks/detections/{detection_id}"

src/content/docs/waf/detections/leaked-credentials/examples.mdx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
---
2-
title: Mitigation examples
2+
title: Example mitigation rules
33
pcx_content_type: configuration
44
sidebar:
5-
order: 4
6-
label: Mitigation examples
5+
order: 5
76
head:
87
- tag: title
9-
content: Leaked credentials mitigation examples
8+
content: Leaked credentials example mitigation rules
109
description: Examples of rules for mitigating requests containing leaked credentials.
1110
---
1211

@@ -59,7 +58,3 @@ Create a [custom rule](/waf/custom-rules/) that challenges requests containing a
5958
- **Action**: _Managed Challenge_
6059

6160
---
62-
63-
## More resources
64-
65-
- [Terraform example: Add a custom rule challenging requests with leaked credentials](/terraform/additional-configurations/waf-custom-rules/#add-a-custom-rule-challenging-requests-with-leaked-credentials)

src/content/docs/waf/detections/leaked-credentials/get-started.mdx

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ You can combine the previous expression with other [fields](/ruleset-engine/rule
7272

7373
</Details>
7474

75-
For additional examples, refer to [Mitigation examples](/waf/detections/leaked-credentials/examples/).
75+
For additional examples, refer to [Example mitigation rules](/waf/detections/leaked-credentials/examples/).
7676

7777
### Handle detected leaked credentials at the origin server
7878

@@ -84,22 +84,27 @@ Additionally, you may want to handle leaked credentials detected by Cloudflare a
8484

8585
## 4. (Optional) Configure a custom detection location
8686

87-
To check for leaked credentials in a way that is not covered by the default configuration, add a custom detection location.
87+
To check for leaked credentials in a way that is not covered by the default configuration, add a [custom detection location](/waf/detections/leaked-credentials/#custom-detection-locations).
8888

8989
<Tabs syncKey="dashPlusAPI"> <TabItem label="Dashboard">
9090

9191
1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com/), and select your account and domain.
9292
2. Go to **Security** > **Settings**.
9393
3. Under **Incoming traffic detections**, select **Leaked credentials** and then select **Add custom username and password location**.
94-
4. In **Username location** and **Password location** (optional), enter expressions for obtaining the username and the password from the HTTP request. Refer to the following example expressions:
94+
4. In **Username location** and **Password location** (optional), enter expressions for obtaining the username and the password from the HTTP request. For example, you could use the following expressions:
9595

96-
| Request type | Username location / Password location |
97-
| ---------------- | --------------------------------------------------------------------------------------------------------------- |
98-
| JSON body | `lookup_json_string(http.request.body.raw, "user")`<br/>`lookup_json_string(http.request.body.raw, "secret")` |
99-
| URL-encoded form | `url_decode(http.request.body.form["user"][0])`<br/>`url_decode(http.request.body.form["secret"][0])` |
100-
| Multipart form | `url_decode(http.request.body.multipart["user"][0])`<br/>`url_decode(http.request.body.multipart["secret"][0])` |
96+
- Username location:<br/>
97+
`lookup_json_string(http.request.body.raw, "user")`
98+
- Password location:<br/>
99+
`lookup_json_string(http.request.body.raw, "secret")`
101100

102-
Refer to the [`lookup_json_string()`](/ruleset-engine/rules-language/functions/#lookup_json_string) and [`url_decode()`](/ruleset-engine/rules-language/functions/#url_decode) documentation for more information on these functions.
101+
This configuration will scan incoming HTTP requests containing a JSON body with a structure similar to the following:
102+
103+
```js
104+
{"user": "<USERNAME>", "secret": "<PASSWORD>"}
105+
```
106+
107+
Refer to the [`lookup_json_string()`](/ruleset-engine/rules-language/functions/#lookup_json_string) documentation for more information on this function.
103108

104109
5. Select **Save**.
105110

@@ -126,22 +131,16 @@ Refer to the [`lookup_json_string()`](/ruleset-engine/rules-language/functions/#
126131

127132
</TabItem> <TabItem label="Terraform">
128133

129-
Use the `cloudflare_leaked_credential_check_rule` resource to add a custom detection location. For example:
130-
131-
```terraform
132-
resource "cloudflare_leaked_credential_check_rule" "custom_location_example" {
133-
zone_id = "<ZONE_ID>"
134-
username = "lookup_json_string(http.request.body.raw, \"user\")"
135-
password = "lookup_json_string(http.request.body.raw, \"secret\")"
136-
}
137-
```
134+
<Render file="detections/leaked-credentials-configure-location-terraform" />
138135

139-
For more information, refer to the [Terraform Cloudflare provider documentation](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs).
136+
Refer to the [`lookup_json_string()`](/ruleset-engine/rules-language/functions/#lookup_json_string) documentation for more information on this function.
140137

141138
</TabItem> </Tabs>
142139

143140
You only need to provide an expression for the username in custom detection locations.
144141

142+
For more examples of custom detection locations for different request types, refer to [Custom detection locations](/waf/detections/leaked-credentials/#custom-detection-locations).
143+
145144
---
146145

147146
## Test your configuration

src/content/docs/waf/detections/leaked-credentials/index.mdx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Leaked credentials detection includes rules for identifying credentials in HTTP
4747

4848
Additionally, the scan includes generic rules for other common web authentication patterns.
4949

50-
You can also configure custom detection locations to address the specific authentication mechanism used in your web applications. A custom detection location tells the Cloudflare WAF where to find usernames and passwords in HTTP requests of your web application.
50+
You can also configure [custom detection locations](#custom-detection-locations) to address the specific authentication mechanism used in your web applications. A custom detection location tells the Cloudflare WAF where to find usernames and passwords in HTTP requests of your web application.
5151

5252
## Custom detection locations
5353

@@ -72,6 +72,14 @@ You could configure a custom detection location with the following settings:
7272

7373
When specifying a custom detection location, only the location of the username field is required.
7474

75+
The following table includes example detection locations for different request types:
76+
77+
| Request type | Username location / Password location |
78+
| ---------------- | --------------------------------------------------------------------------------------------------------------- |
79+
| JSON body | `lookup_json_string(http.request.body.raw, "user")`<br/>`lookup_json_string(http.request.body.raw, "secret")` |
80+
| URL-encoded form | `url_decode(http.request.body.form["user"][0])`<br/>`url_decode(http.request.body.form["secret"][0])` |
81+
| Multipart form | `url_decode(http.request.body.multipart["user"][0])`<br/>`url_decode(http.request.body.multipart["secret"][0])` |
82+
7583
Expressions used to specify custom detection locations can include the following fields and functions:
7684

7785
- Fields:

0 commit comments

Comments
 (0)