Skip to content

Commit dc8e6fc

Browse files
authored
feat(translator): make append_x_forwarded_host configurable in HTTPRouteFilter (#8527)
* feat(translator): make appendXForwardedHost configurable in HTTPRouteFilter Add appendXForwardedHost field to HTTPURLRewriteFilter to allow users to control whether the original Host header is appended to X-Forwarded-Host when hostname rewriting is configured. Defaults to true for backward compatibility. Fixes #8386 Signed-off-by: rborale5 <rborale5@gmail.com>
1 parent c86eb78 commit dc8e6fc

22 files changed

Lines changed: 840 additions & 1 deletion

api/v1alpha1/httproutefilter_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ type HTTPURLRewriteFilter struct {
6565
//
6666
// +optional
6767
Path *HTTPPathModifier `json:"path,omitempty"`
68+
// AppendXForwardedHost controls whether the original Host header value is
69+
// appended to the X-Forwarded-Host header when hostname rewriting is configured.
70+
// Defaults to true for backward compatibility.
71+
//
72+
// +optional
73+
AppendXForwardedHost *bool `json:"appendXForwardedHost,omitempty"`
6874
}
6975

7076
// HTTPDirectResponseFilter defines the configuration to return a fixed response.

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/gateway-crds-helm/templates/generated/gateway.envoyproxy.io_httproutefilters.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ spec:
400400
description: HTTPURLRewriteFilter define rewrites of HTTP URL components
401401
such as path and host
402402
properties:
403+
appendXForwardedHost:
404+
description: |-
405+
AppendXForwardedHost controls whether the original Host header value is
406+
appended to the X-Forwarded-Host header when hostname rewriting is configured.
407+
Defaults to true for backward compatibility.
408+
type: boolean
403409
hostname:
404410
description: |-
405411
Hostname is the value to be used to replace the Host header value during

charts/gateway-helm/charts/crds/crds/generated/gateway.envoyproxy.io_httproutefilters.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,12 @@ spec:
399399
description: HTTPURLRewriteFilter define rewrites of HTTP URL components
400400
such as path and host
401401
properties:
402+
appendXForwardedHost:
403+
description: |-
404+
AppendXForwardedHost controls whether the original Host header value is
405+
appended to the X-Forwarded-Host header when hostname rewriting is configured.
406+
Defaults to true for backward compatibility.
407+
type: boolean
402408
hostname:
403409
description: |-
404410
Hostname is the value to be used to replace the Host header value during

internal/gatewayapi/filters.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,13 @@ func (t *Translator) processExtensionRefHTTPFilter(extFilter *gwapiv1.LocalObjec
899899
}
900900
}
901901

902+
if hrf.Spec.URLRewrite.AppendXForwardedHost != nil {
903+
if filterContext.URLRewrite == nil {
904+
filterContext.URLRewrite = &ir.URLRewrite{}
905+
}
906+
filterContext.URLRewrite.AppendXForwardedHost = hrf.Spec.URLRewrite.AppendXForwardedHost
907+
}
908+
902909
}
903910

904911
if hrf.Spec.DirectResponse != nil {
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
gateways:
2+
- apiVersion: gateway.networking.k8s.io/v1
3+
kind: Gateway
4+
metadata:
5+
namespace: envoy-gateway
6+
name: gateway-1
7+
spec:
8+
gatewayClassName: envoy-gateway-class
9+
listeners:
10+
- name: http
11+
protocol: HTTP
12+
port: 80
13+
hostname: "*.envoyproxy.io"
14+
allowedRoutes:
15+
namespaces:
16+
from: All
17+
httpRoutes:
18+
- apiVersion: gateway.networking.k8s.io/v1
19+
kind: HTTPRoute
20+
metadata:
21+
namespace: default
22+
name: httproute-disable-append
23+
spec:
24+
hostnames:
25+
- gateway.envoyproxy.io
26+
parentRefs:
27+
- namespace: envoy-gateway
28+
name: gateway-1
29+
sectionName: http
30+
rules:
31+
- matches:
32+
- path:
33+
value: "/disable-append"
34+
backendRefs:
35+
- name: service-1
36+
port: 8080
37+
filters:
38+
- type: ExtensionRef
39+
extensionRef:
40+
group: gateway.envoyproxy.io
41+
kind: HTTPRouteFilter
42+
name: header-host-no-append
43+
- apiVersion: gateway.networking.k8s.io/v1
44+
kind: HTTPRoute
45+
metadata:
46+
namespace: default
47+
name: httproute-enable-append
48+
spec:
49+
hostnames:
50+
- gateway.envoyproxy.io
51+
parentRefs:
52+
- namespace: envoy-gateway
53+
name: gateway-1
54+
sectionName: http
55+
rules:
56+
- matches:
57+
- path:
58+
value: "/enable-append"
59+
backendRefs:
60+
- name: service-1
61+
port: 8080
62+
filters:
63+
- type: ExtensionRef
64+
extensionRef:
65+
group: gateway.envoyproxy.io
66+
kind: HTTPRouteFilter
67+
name: header-host-with-append
68+
- apiVersion: gateway.networking.k8s.io/v1
69+
kind: HTTPRoute
70+
metadata:
71+
namespace: default
72+
name: httproute-only-append-flag
73+
spec:
74+
hostnames:
75+
- gateway.envoyproxy.io
76+
parentRefs:
77+
- namespace: envoy-gateway
78+
name: gateway-1
79+
sectionName: http
80+
rules:
81+
- matches:
82+
- path:
83+
value: "/only-append-flag"
84+
backendRefs:
85+
- name: service-1
86+
port: 8080
87+
filters:
88+
- type: ExtensionRef
89+
extensionRef:
90+
group: gateway.envoyproxy.io
91+
kind: HTTPRouteFilter
92+
name: only-append-flag
93+
httpFilters:
94+
- apiVersion: gateway.envoyproxy.io/v1alpha1
95+
kind: HTTPRouteFilter
96+
metadata:
97+
name: header-host-no-append
98+
namespace: default
99+
spec:
100+
urlRewrite:
101+
hostname:
102+
type: Header
103+
header: my-host
104+
appendXForwardedHost: false
105+
- apiVersion: gateway.envoyproxy.io/v1alpha1
106+
kind: HTTPRouteFilter
107+
metadata:
108+
name: header-host-with-append
109+
namespace: default
110+
spec:
111+
urlRewrite:
112+
hostname:
113+
type: Header
114+
header: my-host
115+
appendXForwardedHost: true
116+
- apiVersion: gateway.envoyproxy.io/v1alpha1
117+
kind: HTTPRouteFilter
118+
metadata:
119+
name: only-append-flag
120+
namespace: default
121+
spec:
122+
urlRewrite:
123+
appendXForwardedHost: false

0 commit comments

Comments
 (0)