Skip to content

Commit

Permalink
Backport of [NET-10731] Fix issue with nil pointer derefs when sectio…
Browse files Browse the repository at this point in the history
…n name is not specified for GatewayPolicy into release/1.3.x (#4248)

* backport of commit de6bb81

* backport of commit c688597

* backport of commit d1ffb78

---------

Co-authored-by: jm96441n <[email protected]>
  • Loading branch information
hc-github-team-consul-core and jm96441n authored Aug 15, 2024
1 parent 4611fb1 commit ad1069b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/4247.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
api-gateway: fix nil pointer deref bug when the section name in a gateway policy is not specified
```
13 changes: 12 additions & 1 deletion control-plane/api-gateway/binding/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ func validateGateway(gateway gwv1beta1.Gateway, pods []corev1.Pod, consulGateway
return result
}

func stringOrEmtpy(s *gwv1beta1.SectionName) string {
if s == nil {
return ""
}
return string(*s)
}

func validateGatewayPolicies(gateway gwv1beta1.Gateway, policies []v1alpha1.GatewayPolicy, resources *common.ResourceMap) gatewayPolicyValidationResults {
results := make(gatewayPolicyValidationResults, 0, len(policies))

Expand All @@ -175,7 +182,7 @@ func validateGatewayPolicies(gateway gwv1beta1.Gateway, policies []v1alpha1.Gate

exists := listenerExistsForPolicy(gateway, policy)
if !exists {
result.resolvedRefsErrs = append(result.resolvedRefsErrs, errorForMissingListener(policy.Spec.TargetRef.Name, string(*policy.Spec.TargetRef.SectionName)))
result.resolvedRefsErrs = append(result.resolvedRefsErrs, errorForMissingListener(policy.Spec.TargetRef.Name, stringOrEmtpy(policy.Spec.TargetRef.SectionName)))
}

missingJWTProviders := make(map[string]struct{})
Expand Down Expand Up @@ -211,6 +218,10 @@ func validateGatewayPolicies(gateway gwv1beta1.Gateway, policies []v1alpha1.Gate
}

func listenerExistsForPolicy(gateway gwv1beta1.Gateway, policy v1alpha1.GatewayPolicy) bool {
if policy.Spec.TargetRef.SectionName == nil {
return false
}

return gateway.Name == policy.Spec.TargetRef.Name &&
slices.ContainsFunc(gateway.Spec.Listeners, func(l gwv1beta1.Listener) bool { return l.Name == *policy.Spec.TargetRef.SectionName })
}
Expand Down

0 comments on commit ad1069b

Please sign in to comment.