fix(ingress): restore visitor IPs through SSL passthrough

With cloudflareProxy enabled, HTTPS reaches nginx from the
ssl-passthrough proxy on loopback. Without trusting that proxy, the
whitelist sees 127.0.0.1 instead of the visitor address; trusting it
without checking the PROXY-protocol peer would reopen the spoofing
closed by #4444.

Checking the transport peer before the whitelist preserves that
protection, while port 80 needs its direct peer because no
PROXY-protocol address exists there.

Tested with helm-unittest and an isolated ingress-nginx controller:
trusted PROXY peers pass the whitelist with the restored visitor
address, while spoofed headers on HTTP and HTTPS return 403.

Closes: #5327
Assisted-by: LLM
Signed-off-by: toribru <161638395+toribru@users.noreply.github.com>
This commit is contained in:
toribru committed 2026-11-26 01:30:15 +03:00
1 parent 8cab9b7d7b
commit 00a540eb62
2 files changed
+69 -5

No files matched your search

@@ -120,9 +120,37 @@ spec:
whitelist-source-range: "{{ join "," . }}"
{{- end }}
{{- if .Values.cloudflareProxy }}
proxy-real-ip-cidr: "{{ include "ingress.cloudflare-ips" . }}"
{{- /* ssl-passthrough forwards HTTPS from loopback with the edge address
in PROXY protocol. Trust loopback only together with the rewrite-phase
guard below, or a direct client could choose CF-Connecting-IP. Port 80
has no PROXY protocol peer, so use its original socket address: realip
has already replaced remote_addr with the visitor address when the map
is evaluated. This values override also has to carry the system chart's
keepalive_time, because it replaces that chart's http-snippet value as
a whole. */}}
proxy-real-ip-cidr: "{{ include "ingress.cloudflare-ips" . }},127.0.0.1/32,::1/128"
use-forwarded-headers: "true"
server-snippet: "real_ip_header CF-Connecting-IP;"
http-snippet: |
keepalive_time 12h;
map $proxy_protocol_addr $cozy_cf_source {
"" $realip_remote_addr;
default $proxy_protocol_addr;
}
geo $cozy_cf_source $cozy_cf_trusted {
default 0;
{{ range $cidr := splitList "," (include "ingress.cloudflare-ips" .) }}
{{ $cidr }} 1;
{{ end }}
}
map "$cozy_cf_trusted:$http_cf_connecting_ip" $cozy_cf_reject {
default 0;
"~^0:.+$" 1;
}
server-snippet: |
real_ip_header CF-Connecting-IP;
if ($cozy_cf_reject = 1) {
return 403;
}
{{- end }}
{{- if $proxyProtocol }}
{{- /* real-ip-header + enable-real-ip make the PROXY-protocol frame the
@@ -8,7 +8,7 @@ set:
_cluster: {}
cloudflareProxy: true
tests:
- it: trusts CF-Connecting-IP only from the Cloudflare ranges
- it: trusts CF-Connecting-IP from Cloudflare ranges and the passthrough proxy
asserts:
- matchRegex:
path: spec.values.ingress-nginx.controller.config.proxy-real-ip-cidr
@@ -16,12 +16,48 @@ tests:
- notMatchRegex:
path: spec.values.ingress-nginx.controller.config.proxy-real-ip-cidr
pattern: (^|,)(0\.0\.0\.0/0|::/0)(,|$)
- matchRegex:
path: spec.values.ingress-nginx.controller.config.proxy-real-ip-cidr
pattern: '(^|,)127\.0\.0\.1/32(,|$)'
- matchRegex:
path: spec.values.ingress-nginx.controller.config.proxy-real-ip-cidr
pattern: '(^|,)::1/128(,|$)'
- equal:
path: spec.values.ingress-nginx.controller.config.use-forwarded-headers
value: "true"
- equal:
- matchRegex:
path: spec.values.ingress-nginx.controller.config.server-snippet
value: "real_ip_header CF-Connecting-IP;"
pattern: 'real_ip_header CF-Connecting-IP;'
- it: rejects a spoofed Cloudflare header before the whitelist
asserts:
- matchRegex:
path: spec.values.ingress-nginx.controller.config.http-snippet
pattern: 'geo \$cozy_cf_source \$cozy_cf_trusted \{\n default 0;'
- matchRegex:
path: spec.values.ingress-nginx.controller.config.http-snippet
pattern: '173\.245\.48\.0/20 1;'
- matchRegex:
path: spec.values.ingress-nginx.controller.config.http-snippet
pattern: '2400:cb00::/32 1;'
- matchRegex:
path: spec.values.ingress-nginx.controller.config.http-snippet
pattern: 'map "\$cozy_cf_trusted:\$http_cf_connecting_ip" \$cozy_cf_reject \{\n default 0;\n "~\^0:\.\+\$" 1;\n\}'
- matchRegex:
path: spec.values.ingress-nginx.controller.config.server-snippet
pattern: 'if \(\$cozy_cf_reject = 1\) \{\n return 403;\n\}'
- it: checks the original socket peer when PROXY protocol is absent on port 80
asserts:
- matchRegex:
path: spec.values.ingress-nginx.controller.config.http-snippet
pattern: 'map \$proxy_protocol_addr \$cozy_cf_source \{\n "" \$realip_remote_addr;\n default \$proxy_protocol_addr;\n\}'
- it: preserves the controller keepalive override
asserts:
- matchRegex:
path: spec.values.ingress-nginx.controller.config.http-snippet
pattern: 'keepalive_time 12h;'
- it: does not use set_real_ip_from, which is not an ingress-nginx ConfigMap key
asserts: