Skip to content

Commit 5b2cc69

Browse files
committed
test: guard against RouteBaseDirectives aliasing leak
Add TestGetCorazaDirectivesDoesNotAliasRouteBaseDirectives: seed the shared base slice with spare capacity, build an Enforce policy, then build a second Observe policy and assert the first's SecRuleEngine token was not overwritten. Fails on the aliased code, passes with the slices.Clone fix.
1 parent 5a18b84 commit 5b2cc69

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

internal/controller/trafficprotectionpolicy_controller_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,36 @@ func TestGetCorazaDirectivesForTrafficProtectionPolicy(t *testing.T) {
992992
}
993993
}
994994

995+
func TestGetCorazaDirectivesDoesNotAliasRouteBaseDirectives(t *testing.T) {
996+
base := make([]string, 2, 8)
997+
base[0] = "Include @crs-setup-conf"
998+
base[1] = "Include @recommended-conf"
999+
1000+
operatorConfig := config.NetworkServicesOperator{
1001+
Gateway: config.GatewayConfig{
1002+
Coraza: config.CorazaConfig{RouteBaseDirectives: base},
1003+
},
1004+
}
1005+
reconciler := &TrafficProtectionPolicyReconciler{Config: operatorConfig}
1006+
1007+
enforce := reconciler.getCorazaDirectivesForTrafficProtectionPolicy(&policyContext{ptr.To(
1008+
newTrafficProtectionPolicy("default", "tpp-enforce", func(tpp *networkingv1alpha.TrafficProtectionPolicy) {
1009+
tpp.Spec.Mode = networkingv1alpha.TrafficProtectionPolicyEnforce
1010+
}),
1011+
)})
1012+
assert.Contains(t, enforce, "SecRuleEngine On")
1013+
1014+
reconciler.getCorazaDirectivesForTrafficProtectionPolicy(&policyContext{ptr.To(
1015+
newTrafficProtectionPolicy("default", "tpp-observe"),
1016+
)})
1017+
1018+
assert.Contains(t, enforce, "SecRuleEngine On",
1019+
"second policy leaked into the first via a shared RouteBaseDirectives backing array")
1020+
assert.EqualValues(t, []string{"Include @crs-setup-conf", "Include @recommended-conf"},
1021+
reconciler.Config.Gateway.Coraza.RouteBaseDirectives,
1022+
"shared base directives were mutated")
1023+
}
1024+
9951025
// nolint:unparam
9961026
func newTrafficProtectionPolicy(
9971027
namespace,

0 commit comments

Comments
 (0)