Question: How do I upload files that are blocked by AWS WAF?

I need to upload (POST) a file that uses an extension that's blocked by AWS WAF.

Short descriptionTo understand why a POST request is blocked by AWS WAF, note the following points:AWS WAF BODY filters inspect only the first 8,192 bytes of the payload of a POST request for malicious scripts.The SQL injection and cross-site scripting (XSS) rules are sensitive to files that contain random characters in their metadata. These random characters might invoke web ACL rules due to their similarity to an actual XSS or SQL injection signature in AWS WAF.Filtering for specific file types isn't supported by AWS WAF. You must use other methods to eliminate false positives from uploading files or images.First, review the common rules that might block file uploads. If a common rule isn't blocking the upload, then consider additional options to allow blocked files.The following rules commonly block file uploads:CrossSiteScripting_BODYSQLi_BODYWindowsShellCommands_BODYGenericLFI_BODYSizeRestrictions_BODYResolutionFile uploads blocked by SQLi_BODY and CrossSiteScripting_BODY rulesCheck the terminatingRuleMatchDetails field in the AWS WAF comprehensive logs for the rule information.Note: The terminatingRuleMatchDetails field populates only for SQLi_BODY and CrossSiteScripting_BODY attacks.The following is an example of matchedData for CrossSiteScripting_BODY:"terminatingRuleMatchDetails": [{        "conditionType": "XSS",        "location": "BODY",        "matchedData": [            "<?",            "`"        ]The following is an example of matchedData for SQLi_BODY:"terminatingRuleMatchDetails": [{        "conditionType": "SQL_INJECTION",        "location": "BODY",        "matchedData": [            ")",            "*",            "(",            "0"        ]To address blocked uploads by SQLi_BODY or CrossSiteScripting_BODY, choose one of the following options:Option 1If you know the IP address range that accesses the application, then add well-known IP addresses to a safe list rule with IP match conditions. For instructions, see Working with IP match conditions.Option 2Use a safe list with a string or regular expression (regex) match condition to allow the request. You can create a safe list based on a URI, HTTP headers, or a phrase that's associated with the BODY of the AWS WAF files.To create your safe list, create a new custom rule to block XSS or SQLi vectors with an exception condition that's based on matched data. Be sure to override the action for the specific rule that causes the false positive.To create this custom rule, complete the following steps:1.    Open the AWS WAF console.2.    In the navigation pane, under AWS WAF, choose Web ACLs.3.    For Region, select the AWS Region where you created your web ACL.Note: Select Global if your web ACL is set up for Amazon CloudFront.4.    Select your web ACL.5.    In the web ACL Rules tab, choose Rules.6.    Choose Add Rules, and then choose Add my own rules and rule groups.7.    For Name, enter a rule name, and then choose Regular Rule.8.    For If a request, choose matches all the statements (AND).9.    Complete Statement 1 with the following fields:For Inspect, select Has a label.For Match scope, select Label.For Match key, enter the label for the rule that creates the false positive. For example, if the CrossSiteScripting_BODY rule creates the false positive, then enter awswaf:managed:aws:core-rule-set:CrossSiteScripting_Body.10.    Complete Statement 2 with the following fields:Select the check box for Negate statement results.For Inspect, select Body.For Match type, select Contains string.For String to match, enter the value that you want to match to the rule.11.    (Optional) For Text transformation, choose a Text transformation or None.12.    For Action, choose Block.13.    Choose Add rule.14.    For Set rule priority, move the rule below the managed rule that was blocking the request.15.    Choose Save.Important: It’s a best practice to test rules in a non-production environment with the Action set to Count. Evaluate the rule using Amazon CloudWatch metrics combined with AWS WAF sampled requests or AWS WAF logs. When the rule does what you want, change the Action to Block.File uploads blocked by WindowsShellCommands_BODY, GenericLFI_BODY, or SizeRestrictions_BODY rulesTake an HTTP Archive (HAR) file while the file is being uploaded and review it for WindowsShellCommands_BODY, GenericLFI_BODY, or SizeRestrictions_BODY rules. For instructions, see How do I create a HAR file from my browser for an AWS Support case?To allow the false positives for WindowsShellCommands_BODY, GenericLFI_BODY, or SizeRestrictions_BODY, first set the corresponding rule to Count mode. For instructions, see Overriding a rule group's evaluation result to Count.Then, create a custom rule for the specific managed rule that causes the false positive with an exception condition using Labels:1.    Open the AWS WAF console.2.    In the navigation pane, under AWS WAF, choose Web ACLs.3.    For Region, select the AWS Region where you created your web ACL.Note: Select Global if your web ACL is set up for Amazon CloudFront.4.    Select your web ACL.5.    In the web ACL Rules tab, choose Rules.6.    Choose Add Rules, and then choose Add my own rules and rule groups.7.    For Name, enter a rule name, and then choose Regular Rule.8.    For If a request, choose matches all the statements (AND).9.    Complete Statement 1 with the following fields:For Inspect, select Has a label.For Match scope, select Label.For Match key, enter the label for the rule that creates the false positive. For example, if the WindowsShellCommands_BODY rule creates the false positive, then enter awswaf:managed:aws:windows-os:WindowsShellCommands_Body.10.    Complete Statement 2 with the following fields:Select the check box for Negate statement results.For Inspect, select URI path.For Match type, select Exactly matches string.For String to match, enter the URI path where requests are being made.11.    (Optional) For Text transformation, choose a Text transformation or None.12.    For Action, choose Block.13.    Choose Add rule.14.    For Set rule priority, move the rule below the managed rule that was blocking the request.15.    Choose Save.Important: It’s a best practice to test rules in a non-production environment with the Action set to Count. Evaluate the rule using CloudWatch metrics combined with AWS WAF sampled requests or AWS WAF logs. When the rule does what you want, change the Action to Block.Additional options for allowing blocked filesNote: Rules are processed in the order that they're listed in the web ACL. For the following recommendations, be sure to reorder your rule priorities as needed.Choose the best method for your use case:Apply selective exclusion with a string match rule statement (AWS WAF) or a string match condition (AWS WAF Classic). Add specific phrases that are associated with the BODY of the files to your safe list. If false positives occur on a certain URI path, then add the path to your safe list.Use a separate domain for file uploads. Be sure to consider if this is a cost-effective option for your use case.Scan (scrub) files and images for embedded code and data. You can perform this action on the client side before uploading the files. Or, if you create an exclusion rule, then you can perform this action on the backend after uploading the files.Compress files before uploading them.Caution: Make sure that you don't compress malicious files.If the upload happens from a range of known IP addresses, then add those IP addresses to your safe list.Use base64 encoding to encode all image data so that AWS WAF doesn't invoke XSS or SQLi on images.Caution: Be sure to avoid encoding malicious images.Implement image optimization techniques, such as chunk removal or randomization of bits.