Skip to content

Commit 3ebf245

Browse files
authored
fix: return err if direct response size exceeds limit (#5710)
* fix: return err if direct response size exceeds limit Signed-off-by: Arko Dasgupta <[email protected]> * lint Signed-off-by: Arko Dasgupta <[email protected]> * add another check Signed-off-by: Arko Dasgupta <[email protected]> --------- Signed-off-by: Arko Dasgupta <[email protected]>
1 parent b5763db commit 3ebf245

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

internal/gatewayapi/backendtrafficpolicy.go

+21
Original file line numberDiff line numberDiff line change
@@ -930,19 +930,37 @@ func buildResponseOverride(policy *egv1a1.BackendTrafficPolicy, resources *resou
930930
}, nil
931931
}
932932

933+
func checkResponseBodySize(b *string) error {
934+
// Make this configurable in the future
935+
// https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route.proto.html#max_direct_response_body_size_bytes
936+
maxDirectResponseSize := 4096
937+
lenB := len(*b)
938+
if lenB > maxDirectResponseSize {
939+
return fmt.Errorf("response.body size %d greater than the max size %d", lenB, maxDirectResponseSize)
940+
}
941+
942+
return nil
943+
}
944+
933945
func getCustomResponseBody(body *egv1a1.CustomResponseBody, resources *resource.Resources, policyNs string) (*string, error) {
934946
if body != nil && body.Type != nil && *body.Type == egv1a1.ResponseValueTypeValueRef {
935947
cm := resources.GetConfigMap(policyNs, string(body.ValueRef.Name))
936948
if cm != nil {
937949
b, dataOk := cm.Data["response.body"]
938950
switch {
939951
case dataOk:
952+
if err := checkResponseBodySize(&b); err != nil {
953+
return nil, err
954+
}
940955
return &b, nil
941956
case len(cm.Data) > 0: // Fallback to the first key if response.body is not found
942957
for _, value := range cm.Data {
943958
b = value
944959
break
945960
}
961+
if err := checkResponseBodySize(&b); err != nil {
962+
return nil, err
963+
}
946964
return &b, nil
947965
default:
948966
return nil, fmt.Errorf("can't find the key response.body in the referenced configmap %s", body.ValueRef.Name)
@@ -952,6 +970,9 @@ func getCustomResponseBody(body *egv1a1.CustomResponseBody, resources *resource.
952970
return nil, fmt.Errorf("can't find the referenced configmap %s", body.ValueRef.Name)
953971
}
954972
} else if body != nil && body.Inline != nil {
973+
if err := checkResponseBodySize(body.Inline); err != nil {
974+
return nil, err
975+
}
955976
return body.Inline, nil
956977
}
957978

internal/gatewayapi/testdata/httproute-with-direct-response.in.yaml

+33-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ httpRoutes:
4949
- apiVersion: gateway.networking.k8s.io/v1
5050
kind: HTTPRoute
5151
metadata:
52-
name: direct-response-with-errors
52+
name: direct-response-with-value-not-found
5353
namespace: default
5454
spec:
5555
parentRefs:
@@ -67,6 +67,27 @@ httpRoutes:
6767
group: gateway.envoyproxy.io
6868
kind: HTTPRouteFilter
6969
name: direct-response-value-ref-not-found
70+
- apiVersion: gateway.networking.k8s.io/v1
71+
kind: HTTPRoute
72+
metadata:
73+
name: direct-response-too-long
74+
namespace: default
75+
spec:
76+
parentRefs:
77+
- name: gateway-1
78+
namespace: envoy-gateway
79+
sectionName: http
80+
rules:
81+
- matches:
82+
- path:
83+
type: PathPrefix
84+
value: /too-long
85+
filters:
86+
- type: ExtensionRef
87+
extensionRef:
88+
group: gateway.envoyproxy.io
89+
kind: HTTPRouteFilter
90+
name: direct-response-too-long
7091
configMaps:
7192
- apiVersion: v1
7293
kind: ConfigMap
@@ -117,3 +138,14 @@ httpFilters:
117138
group: ""
118139
kind: ConfigMap
119140
name: value-ref-response
141+
- apiVersion: gateway.envoyproxy.io/v1alpha1
142+
kind: HTTPRouteFilter
143+
metadata:
144+
name: direct-response-too-long
145+
namespace: default
146+
spec:
147+
directResponse:
148+
contentType: text/plain
149+
body:
150+
type: Inline
151+
inline: "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"

internal/gatewayapi/testdata/httproute-with-direct-response.out.yaml

+43-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ gateways:
1717
protocol: HTTP
1818
status:
1919
listeners:
20-
- attachedRoutes: 2
20+
- attachedRoutes: 3
2121
conditions:
2222
- lastTransitionTime: null
2323
message: Sending translated listener configuration to the data plane
@@ -95,7 +95,7 @@ httpRoutes:
9595
kind: HTTPRoute
9696
metadata:
9797
creationTimestamp: null
98-
name: direct-response-with-errors
98+
name: direct-response-with-value-not-found
9999
namespace: default
100100
spec:
101101
parentRefs:
@@ -131,6 +131,47 @@ httpRoutes:
131131
name: gateway-1
132132
namespace: envoy-gateway
133133
sectionName: http
134+
- apiVersion: gateway.networking.k8s.io/v1
135+
kind: HTTPRoute
136+
metadata:
137+
creationTimestamp: null
138+
name: direct-response-too-long
139+
namespace: default
140+
spec:
141+
parentRefs:
142+
- name: gateway-1
143+
namespace: envoy-gateway
144+
sectionName: http
145+
rules:
146+
- filters:
147+
- extensionRef:
148+
group: gateway.envoyproxy.io
149+
kind: HTTPRouteFilter
150+
name: direct-response-too-long
151+
type: ExtensionRef
152+
matches:
153+
- path:
154+
type: PathPrefix
155+
value: /too-long
156+
status:
157+
parents:
158+
- conditions:
159+
- lastTransitionTime: null
160+
message: 'Invalid filter HTTPRouteFilter: response.body size 4097 greater
161+
than the max size 4096'
162+
reason: UnsupportedValue
163+
status: "False"
164+
type: Accepted
165+
- lastTransitionTime: null
166+
message: Resolved all the Object references for the Route
167+
reason: ResolvedRefs
168+
status: "True"
169+
type: ResolvedRefs
170+
controllerName: gateway.envoyproxy.io/gatewayclass-controller
171+
parentRef:
172+
name: gateway-1
173+
namespace: envoy-gateway
174+
sectionName: http
134175
infraIR:
135176
envoy-gateway/gateway-1:
136177
proxy:

0 commit comments

Comments
 (0)