Skip to content

Commit e321196

Browse files
authored
Merge pull request #788 from element-hq/bbz/fix-x-forwarded-for-handling
Fix `X-Forwarded-For` handling with multiple values
2 parents 094f5ac + 2c148db commit e321196

File tree

7 files changed

+31
-24
lines changed

7 files changed

+31
-24
lines changed

.github/element-docs-words.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ dockerhub
88
Gematik
99
GHSA
1010
homeserver
11-
homeservers
1211
Homeserver
12+
homeservers
1313
Jetstack
1414
kubeconform
1515
kubernetes
@@ -22,6 +22,7 @@ postgres
2222
PostgreSQL
2323
pytest
2424
Pytest
25+
Referer
2526
SCIM
2627
shellcheck
2728
templatable

charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ defaults
2626

2727
log global
2828

29+
# The Ingress Controller should appropriately set an X-Forwarded-For header
30+
# We leave it alone if it has, but add in the source address in cases where it hasn't
31+
# or the request hasn't come from the ingress controller (i.e. in-cluster)
32+
option forwardfor if-none
33+
34+
# Set the RFC7239 `Forwarded` header
35+
option forwarded
36+
2937
# wait for 5s when connecting to a server
3038
timeout connect 5s
3139

@@ -88,9 +96,9 @@ frontend http-blackhole
8896
# same as http log, with %Th (handshake time)
8997
log-format "%ci:%cp [%tr] %ft %b/%s %Th/%TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"
9098

91-
capture request header Host len 32
92-
capture request header Referer len 200
93-
capture request header User-Agent len 200
99+
http-request capture hdr(host) len 32
100+
http-request capture req.fhdr(x-forwarded-for) len 64
101+
http-request capture req.fhdr(user-agent) len 200
94102

95103
http-request deny content-type application/json string '{"errcode": "M_FORBIDDEN", "error": "Blocked"}'
96104

charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ frontend startup
2222
frontend synapse-http-in
2323
bind *:8008
2424

25-
# same as http log, with %Th (handshake time)
26-
log-format "%ci:%cp [%tr] %ft %b/%s %Th/%TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"
27-
2825
# if we hit the maxconn on a server, and the queue timeout expires, we want
2926
# to avoid returning 503, since that will cause cloudflare to mark us down.
3027
#
@@ -35,24 +32,12 @@ frontend synapse-http-in
3532
#
3633
errorfile 503 /synapse/429.http
3734

38-
capture request header Host len 32
39-
capture request header Referer len 200
40-
capture request header User-Agent len 200
41-
42-
# before we change the 'src', stash it in a session variable
43-
http-request set-var(sess.orig_src) src if !{ var(sess.orig_src) -m found }
44-
45-
# in case this is not the first request on the connection, restore the
46-
# 'src' to the original, in case we fail to parse the x-f-f header.
47-
http-request set-src var(sess.orig_src)
48-
49-
# Traditionally do this only for traffic from some limited IP addreses
50-
# but the incoming router being what it is, means we have no fixed IP here.
51-
http-request set-src hdr(x-forwarded-for)
35+
# same as http log, with %Th (handshake time)
36+
log-format "%ci:%cp [%tr] %ft %b/%s %Th/%TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"
5237

53-
# We always add a X-Forwarded-For header (clobbering any existing
54-
# headers).
55-
http-request set-header X-Forwarded-For %[src]
38+
http-request capture hdr(host) len 32
39+
http-request capture req.fhdr(x-forwarded-for) len 64
40+
http-request capture req.fhdr(user-agent) len 200
5641

5742
# Ingresses by definition run on both 80 & 443 and there's no customising of that
5843
# It is up to the ingress controller and any annotations provided to it whether

charts/matrix-stack/configs/well-known/partial-haproxy.cfg.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ frontend well-known-in
1313
# same as http log, with %Th (handshake time)
1414
log-format "%ci:%cp [%tr] %ft %b/%s %Th/%TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"
1515

16+
http-request capture hdr(host) len 32
17+
http-request capture req.fhdr(x-forwarded-for) len 64
18+
http-request capture req.fhdr(user-agent) len 200
19+
1620
acl is_delete_put_post_method method DELETE POST PUT
1721
http-request deny status 405 if is_delete_put_post_method
1822

newsfragments/788.changed.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Log the X-Forwarded-For header and stop logging the Referer header in HAProxy.

newsfragments/788.changed.2.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Correct the handling of multiple X-Forwarded-For headers to Synapse.
2+
3+
This may have exhibit itself as requests being incorrectly rate-limited by Synapse.
4+
5+
The source IP logged by HAProxy is now always the IP connecting to HAProxy rather than
6+
a value extracted from the X-Forwarded-For header (if present). This is usually an IP
7+
for the ingress controller.

newsfragments/788.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure consistent captured headers in HAProxy log lines, between all HTTP request processing HAProxy frontends.

0 commit comments

Comments
 (0)