Skip to content

Commit d2e13e8

Browse files
authored
fix!: Allow GenericLFI_Body matches for known upload paths. (TBE-137) (#28)
1 parent 2ec0fbc commit d2e13e8

File tree

3 files changed

+80
-9
lines changed

3 files changed

+80
-9
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ to match your desired configuration. For example, to create a new distribution
1313

1414
```hcl
1515
module "cloudfront_waf" {
16-
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.8.2"
16+
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.9.0"
1717
1818
project = "my-project"
1919
environment = "dev"
@@ -76,7 +76,7 @@ distribution at `www.my-project.org`, you could use the following:
7676
7777
```hcl
7878
module "cloudfront_waf" {
79-
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.8.2"
79+
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.9.0"
8080
8181
project = "my-project"
8282
environment = "dev"
@@ -107,7 +107,12 @@ number is too low.
107107
> > WAFInvalidParameterException: Error reason: You exceeded the capacity limit
108108
> > for a rule group or web ACL.
109109
>
110-
> this is a good indication that you may need to set the capacity manually.
110+
> this is a good indication that you may need to set the capacity manually. At
111+
> the end of this message you should see something like:
112+
>
113+
> > field: RULE_GROUP, parameter: **92**
114+
>
115+
> In this case, the minimum capacity for the rule group should be `92`.
111116
112117
In order to override the capacity for a rule group, you can specify the WCUs
113118
through an appropriate variable. For example, to set the capacity for the
@@ -155,7 +160,7 @@ Simply specify the headers you want to add in a map. For example:
155160

156161
```hcl
157162
module "cloudfront_waf" {
158-
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.8.2"
163+
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.9.0"
159164
160165
project = "my-project"
161166
environment = "dev"
@@ -191,7 +196,7 @@ resource "aws_wafv2_ip_set" "security_scanners" {
191196
}
192197
193198
module "cloudfront_waf" {
194-
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.8.2"
199+
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.9.0"
195200
196201
project = "my-project"
197202
environment = "staging"
@@ -230,7 +235,7 @@ For example, to rate limit requests to 300 over a 5-minute period:
230235

231236
```hcl
232237
module "cloudfront_waf" {
233-
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.8.2"
238+
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.9.0"
234239
235240
project = "my-project"
236241
environment = "staging"
@@ -276,7 +281,7 @@ ensure it comes after the common and SQLi rule sets.
276281
277282
```hcl
278283
module "cloudfront_waf" {
279-
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.8.2"
284+
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.9.0"
280285
281286
project = "my-project"
282287
environment = "staging"
@@ -318,7 +323,7 @@ conditions that must be met for the request to be allowed through.
318323
319324
```hcl
320325
module "cloudfront_waf" {
321-
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.8.2"
326+
source = "github.com/codeforamerica/tofu-modules-aws-cloudfront-waf?ref=1.9.0"
322327
323328
project = "my-project"
324329
environment = "staging"

main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,18 @@ resource "aws_wafv2_web_acl" "waf" {
270270
}
271271
}
272272
}
273+
274+
dynamic "rule_action_override" {
275+
for_each = length(var.upload_paths) > 0 ? [true] : []
276+
277+
content {
278+
name = "GenericLFI_BODY"
279+
280+
action_to_use {
281+
count {}
282+
}
283+
}
284+
}
273285
}
274286
}
275287

uploads.tf

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ resource "aws_wafv2_rule_group" "uploads" {
33

44
name_prefix = "${local.prefix}-waf-uploads-"
55
scope = "CLOUDFRONT"
6-
capacity = var.upload_rules_capacity == null ? 9 * length(var.upload_paths) : var.upload_rules_capacity
6+
capacity = var.upload_rules_capacity == null ? 12 * length(var.upload_paths) : var.upload_rules_capacity
77

88
visibility_config {
99
cloudwatch_metrics_enabled = true
@@ -171,6 +171,60 @@ resource "aws_wafv2_rule_group" "uploads" {
171171
}
172172
}
173173

174+
# Block local file inclusion (LFI) requests, unless it was triggered by a file
175+
# upload.
176+
rule {
177+
name = "${local.prefix}-waf-request-lfi"
178+
priority = 4
179+
180+
action {
181+
block {}
182+
}
183+
184+
statement {
185+
and_statement {
186+
statement {
187+
label_match_statement {
188+
key = "awswaf:managed:aws:core-rule-set:GenericLFI_Body"
189+
scope = "LABEL"
190+
}
191+
}
192+
193+
# Create a NOT statement for each of the upload paths. We'll block the
194+
# request if it doesn't match any of the paths.
195+
dynamic "statement" {
196+
for_each = var.upload_paths
197+
198+
content {
199+
not_statement {
200+
statement {
201+
byte_match_statement {
202+
positional_constraint = statement.value.constraint
203+
search_string = statement.value.path
204+
205+
field_to_match {
206+
uri_path {}
207+
}
208+
209+
text_transformation {
210+
priority = 0
211+
type = "NONE"
212+
}
213+
}
214+
}
215+
}
216+
}
217+
}
218+
}
219+
}
220+
221+
visibility_config {
222+
cloudwatch_metrics_enabled = true
223+
metric_name = "${local.prefix}-waf-request-lfi"
224+
sampled_requests_enabled = true
225+
}
226+
}
227+
174228
lifecycle {
175229
create_before_destroy = true
176230
}

0 commit comments

Comments
 (0)