Skip to content

Commit 279e9b5

Browse files
[WAF] Add Terraform info for leaked credentials & malicious uploads (#18675)
--------- Co-authored-by: Rebecca Tamachiro <[email protected]>
1 parent 0b28be9 commit 279e9b5

File tree

6 files changed

+128
-15
lines changed

6 files changed

+128
-15
lines changed

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

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ head:
88
content: Configure WAF custom rules with Terraform
99
---
1010

11-
import { Render } from "~/components";
11+
import { Render, GlossaryTooltip } from "~/components";
1212

1313
This page provides examples of creating WAF custom rules in a zone or account using Terraform. The examples cover the following scenarios:
1414

1515
- Zone-level configurations:
16-
1716
- [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)
1919
- Account-level configurations:
20-
2120
- [Create and deploy a custom ruleset](#create-and-deploy-a-custom-ruleset)
2221
- [Add a custom rule checking for exposed credentials](#add-a-custom-rule-checking-for-exposed-credentials)
2322

@@ -60,14 +59,67 @@ resource "cloudflare_ruleset" "zone_custom_firewall" {
6059

6160
<Render file="add-new-rule" params={{ one: "custom rule" }} /> <br />
6261

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+
63116
## Account-level configurations
64117

65118
### Create and deploy a custom ruleset
66119

67120
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.
68121

69122
:::caution
70-
71123
You can only create and deploy custom rulesets at the account level.
72124
:::
73125

@@ -123,12 +175,11 @@ For more information on configuring and deploying custom rulesets, refer to [Wor
123175

124176
### Add a custom rule checking for exposed credentials
125177

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" />
127179

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).
129181

130182
You can only add exposed credential checks to rules in a custom ruleset (that is, a ruleset with `kind = "custom"`).
131-
:::
132183

133184
```tf
134185
resource "cloudflare_ruleset" "account_firewall_custom_ruleset_exposed_creds" {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@ Create a [custom rule](/waf/custom-rules/) that challenges requests containing a
5757
```
5858

5959
- **Action**: _Managed Challenge_
60+
61+
---
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: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ head:
1010

1111
import { Render, TabItem, Tabs, Details } from "~/components";
1212

13-
## 1. Turn on leaked credentials detection
13+
## 1. Turn on the detection
1414

1515
<Render file="leaked-credentials-detection-enable" />
1616

@@ -126,6 +126,20 @@ This pair of lookup expressions (for username and password) will scan incoming H
126126

127127
Refer to the [`lookup_json_string()`](/ruleset-engine/rules-language/functions/#lookup_json_string) documentation for more information on this function.
128128

129+
</TabItem> <TabItem label="Terraform">
130+
131+
Use the `cloudflare_leaked_credential_check_rule` resource to add a custom detection location. For example:
132+
133+
```terraform
134+
resource "cloudflare_leaked_credential_check_rule" "custom_location_example" {
135+
zone_id = "<ZONE_ID>"
136+
username = "lookup_json_string(http.request.body.raw, \"user\")"
137+
password = "lookup_json_string(http.request.body.raw, \"secret\")"
138+
}
139+
```
140+
141+
For more information, refer to the [Terraform Cloudflare provider documentation](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs).
142+
129143
</TabItem> </Tabs>
130144

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

src/content/docs/waf/detections/malicious-uploads/example-rules.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ This custom rule example blocks requests with uploaded content objects over 15 M
4444
- Action: _Block_
4545

4646
In this example, you must also test for equality because currently any file over 15 MB will be handled internally as if it had a size of 15 MB. This means that using the `>` (greater than) [comparison operator](/ruleset-engine/rules-language/operators/#comparison-operators) would not work for this particular rule — you should use `>=` (greater than or equal) instead.
47+
48+
---
49+
50+
## More resources
51+
52+
- [Terraform example: Add a custom rule blocking malicious uploads](/terraform/additional-configurations/waf-custom-rules/#add-a-custom-rule-blocking-malicious-uploads)

src/content/docs/waf/detections/malicious-uploads/get-started.mdx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ sidebar:
55
order: 2
66
head:
77
- tag: title
8-
content: Get started with content scanning
8+
content: Get started with malicious uploads detection
99
---
1010

1111
import { Details, TabItem, Tabs } from "~/components";
1212

1313
:::note
14-
1514
WAF content scanning is available to customers on an Enterprise plan with a paid add-on.
1615
:::
1716

18-
## 1. Enable WAF content scanning
17+
## 1. Turn on the detection
1918

2019
<Tabs syncKey="dashPlusAPI"> <TabItem label="Dashboard">
2120

@@ -34,6 +33,19 @@ curl --request POST \
3433
--header "X-Auth-Key: <API_KEY>"
3534
```
3635

36+
</TabItem> <TabItem label="Terraform">
37+
38+
Use the `cloudflare_content_scanning` resource to enable malicious uploads detection for a zone. For example:
39+
40+
```terraform
41+
resource "cloudflare_content_scanning" "zone_malicious_uploads_example" {
42+
zone_id = "<ZONE_ID>"
43+
enabled = true
44+
}
45+
```
46+
47+
For more information, refer to the [Terraform Cloudflare provider documentation](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs).
48+
3749
</TabItem> </Tabs>
3850

3951
## 2. Validate the content scanning behavior
@@ -140,6 +152,17 @@ The above request will add the following expression to the current list of custo
140152
lookup_json_string(http.request.body.raw, "file")
141153
```
142154

155+
</TabItem> <TabItem label="Terraform">
156+
157+
Use the `cloudflare_content_scanning_expression` resource to add a custom scan expression. For example:
158+
159+
```terraform
160+
resource "cloudflare_content_scanning_expression" "my_custom_scan_expression" {
161+
zone_id = <ZONE_ID>
162+
payload = "lookup_json_string(http.request.body.raw, \"file\")"
163+
}
164+
```
165+
143166
</TabItem> </Tabs>
144167

145168
The custom scan expression will scan any string found in an HTTP body with the following JSON string:
@@ -148,7 +171,7 @@ The custom scan expression will scan any string found in an HTTP body with the f
148171
{ "file": "<BASE64_ENCODED_STRING>" }
149172
```
150173

151-
Refer to the [`lookup_json_string()` function reference](/ruleset-engine/rules-language/functions/#lookup_json_string) for more information and additional examples of looking up fields in nested JSON payloads.
174+
Refer to the [`lookup_json_string()`](/ruleset-engine/rules-language/functions/#lookup_json_string) documentation for more information and additional examples of looking up fields in nested JSON payloads.
152175

153176
:::note
154177
The content scanner will automatically decode Base64 strings.

src/content/partials/waf/leaked-credentials-detection-enable.mdx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
{}
33
---
44

5-
import { TabItem, Tabs } from "~/components";
5+
import { TabItem, Tabs, Render } from "~/components";
66

7-
On Free plans, the leaked credentials detection is enabled by default, and no action is required. On paid plans, you can turn on the detection in the Cloudflare dashboard or via API.
7+
On Free plans, the leaked credentials detection is enabled by default, and no action is required. On paid plans, you can turn on the detection in the Cloudflare dashboard, via API, or using Terraform.
88

99
<Tabs syncKey="dashPlusAPI"> <TabItem label="Dashboard">
1010

@@ -24,4 +24,17 @@ curl "https://api.cloudflare.com/client/v4/zones/{zone_id}/leaked-credential-che
2424
--data '{ "enabled": true }'
2525
```
2626

27+
</TabItem> <TabItem label="Terraform">
28+
29+
Use the `cloudflare_leaked_credential_check` resource to enable leaked credentials detection for a zone. For example:
30+
31+
```terraform
32+
resource "cloudflare_leaked_credential_check" "zone_lcc_example" {
33+
zone_id = "<ZONE_ID>"
34+
enabled = true
35+
}
36+
```
37+
38+
For more information, refer to the [Terraform Cloudflare provider documentation](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs).
39+
2740
</TabItem> </Tabs>

0 commit comments

Comments
 (0)