Skip to content

Commit 46612de

Browse files
Ptnan7Jingnan XuNoriZC
authored
fix for CustomBlockResponseBody not using base64 issue (#25350)
* bug fix * Update ChangeLog.md --------- Co-authored-by: Jingnan Xu <[email protected]> Co-authored-by: NoriZC <[email protected]>
1 parent 45ae34d commit 46612de

File tree

5 files changed

+639
-1
lines changed

5 files changed

+639
-1
lines changed

src/FrontDoor/FrontDoor.Test/ScenarioTests/WebApplicationFireWallPolicyTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,12 @@ public void TestPolicyAction()
5050
{
5151
TestRunner.RunTestScript("Test-PolicyAction");
5252
}
53+
54+
[Fact]
55+
[Trait(Category.AcceptanceType, Category.CheckIn)]
56+
public void TestCustomBlockResponseBody()
57+
{
58+
TestRunner.RunTestScript("Test-CustomBlockResponseBody");
59+
}
5360
}
5461
}

src/FrontDoor/FrontDoor.Test/ScenarioTests/WebApplicationFireWallPolicyTests.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,35 @@ function Test-PolicyAction
174174
Assert-True { $removed }
175175
Assert-ThrowsContains { Get-AzFrontDoorWafPolicy -Name $Name -ResourceGroupName $resourceGroupName } "does not exist."
176176
}
177+
178+
function Test-CustomBlockResponseBody
179+
{
180+
$Name = getAssetName
181+
$resourceGroup = TestSetup-CreateResourceGroup
182+
$resourceGroupName = $resourceGroup.ResourceGroupName
183+
$tags = @{"tag1" = "value1"; "tag2" = "value2"}
184+
$matchCondition1 = New-AzFrontDoorWafMatchConditionObject -MatchVariable RequestHeader -OperatorProperty Contains -Selector "UserAgent" -MatchValue "WINDOWS" -Transform "Uppercase"
185+
$customRule1 = New-AzFrontDoorWafCustomRuleObject -Name "Rule1" -RuleType MatchRule -MatchCondition $matchCondition1 -Action Block -Priority 2
186+
187+
# Create exclusion objects
188+
$exclusionRule = New-AzFrontDoorWafManagedRuleExclusionObject -Variable QueryStringArgNames -Operator Equals -Selector "ExcludeInRule"
189+
$exclusionGroup = New-AzFrontDoorWafManagedRuleExclusionObject -Variable QueryStringArgNames -Operator Equals -Selector "ExcludeInGroup"
190+
$exclusionSet = New-AzFrontDoorWafManagedRuleExclusionObject -Variable QueryStringArgNames -Operator Equals -Selector "ExcludeInSet"
191+
192+
$ruleOverride = New-AzFrontDoorWafManagedRuleOverrideObject -RuleId "942100" -Action Log -Exclusion $exclusionRule
193+
$override1 = New-AzFrontDoorWafRuleGroupOverrideObject -RuleGroupName SQLI -ManagedRuleOverride $ruleOverride -Exclusion $exclusionGroup
194+
$managedRule1 = New-AzFrontDoorWafManagedRuleObject -Type DefaultRuleSet -Version "1.0" -RuleGroupOverride $override1 -Exclusion $exclusionSet
195+
$managedRule2 = New-AzFrontDoorWafManagedRuleObject -Type BotProtection -Version "preview-0.1"
196+
197+
New-AzFrontDoorWafPolicy -Name $Name -ResourceGroupName $resourceGroupName -Sku Premium_AzureFrontDoor -Customrule $customRule1 -ManagedRule $managedRule1,$managedRule2 -EnabledState Enabled -Mode Prevention -RequestBodyCheck Disabled -CustomBlockResponseBody "<html><head><title>WAF Demo1</title></head><bodybgcolor=`"#FFB29Z`"><p><h1><strong>WAF Custom Response Page</strong></h1></p><p>Please contact us with the below reference ID: {{azure-ref}}<br></p></body></html>"
198+
199+
$afdWafPolicy = Get-AzFrontDoorWafPolicy -Name $Name -ResourceGroupName $resourceGroupName
200+
Assert-AreEqual $afdWafPolicy.CustomBlockResponseBody "<html><head><title>WAF Demo1</title></head><bodybgcolor=`"#FFB29Z`"><p><h1><strong>WAF Custom Response Page</strong></h1></p><p>Please contact us with the below reference ID: {{azure-ref}}<br></p></body></html>"
201+
202+
$afdWafPolicy.CustomBlockResponseBody = "<html><head><title>WAF Demo2</title></head><bodybgcolor=`"#FFB29Z`"><p><h1><strong>WAF Custom Response Page</strong></h1></p><p>Please contact us with the below reference ID: {{azure-ref}}<br></p></body></html>"
203+
$afdWafPolicy | Update-AzFrontDoorWafPolicy
204+
205+
$retrievedPolicy = Get-AzFrontDoorWafPolicy -Name $Name -ResourceGroupName $resourceGroupName
206+
207+
Assert-AreEqual $retrievedPolicy.CustomBlockResponseBody "<html><head><title>WAF Demo2</title></head><bodybgcolor=`"#FFB29Z`"><p><h1><strong>WAF Custom Response Page</strong></h1></p><p>Please contact us with the below reference ID: {{azure-ref}}<br></p></body></html>"
208+
}

src/FrontDoor/FrontDoor.Test/SessionRecords/Microsoft.Azure.Commands.FrontDoor.Test.ScenarioTests.ScenarioTest.WebApplicationFireWallPolicyTests/TestCustomBlockResponseBody.json

Lines changed: 598 additions & 0 deletions
Large diffs are not rendered by default.

src/FrontDoor/FrontDoor/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed a not converting from string to base in CustomBlockResponseBody bug in updating waf policy
2122

2223
## Version 1.11.0
2324
* Upgraded to api version 2024-02-01

src/FrontDoor/FrontDoor/Helpers/ModelExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ public static SdkFirewallPolicy ToSdkFirewallPolicy(this PSPolicy psPolicy)
766766
{
767767
EnabledState = psPolicy.PolicyEnabledState.ToString(),
768768
Mode = psPolicy.PolicyMode,
769-
CustomBlockResponseBody = psPolicy.CustomBlockResponseBody,
769+
CustomBlockResponseBody = psPolicy.CustomBlockResponseBody == null ? psPolicy.CustomBlockResponseBody : Convert.ToBase64String(Encoding.UTF8.GetBytes(psPolicy.CustomBlockResponseBody)),
770770
CustomBlockResponseStatusCode = psPolicy.CustomBlockResponseStatusCode,
771771
RedirectUrl = psPolicy.RedirectUrl,
772772
RequestBodyCheck = psPolicy.RequestBodyCheck?.ToString(),

0 commit comments

Comments
 (0)