From 00a540eb62b8a71ed3e98d9b3a734c8eaea31c40 Mon Sep 17 00:00:00 2001 From: toribru <161638395+toribru@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:07:20 +0300 Subject: [PATCH 1/2] 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> --- .../ingress/templates/nginx-ingress.yaml | 32 +++++++++++++- .../ingress/tests/cloudflare_proxy_test.yaml | 42 +++++++++++++++++-- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/packages/extra/ingress/templates/nginx-ingress.yaml b/packages/extra/ingress/templates/nginx-ingress.yaml index 006e97501..d95be57d8 100644 --- a/packages/extra/ingress/templates/nginx-ingress.yaml +++ b/packages/extra/ingress/templates/nginx-ingress.yaml @@ -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 diff --git a/packages/extra/ingress/tests/cloudflare_proxy_test.yaml b/packages/extra/ingress/tests/cloudflare_proxy_test.yaml index 24869abe8..5462c3701 100644 --- a/packages/extra/ingress/tests/cloudflare_proxy_test.yaml +++ b/packages/extra/ingress/tests/cloudflare_proxy_test.yaml @@ -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: From ed4a37842800cb124320486c9cd7df7f0eba6ce3 Mon Sep 17 00:00:00 2001 From: Aleksei Sviridkin Date: Thu, 26 Nov 2026 03:31:51 +0300 Subject: [PATCH 2/2] style(ingress): drop blank lines from the Cloudflare geo block The range over the Cloudflare ranges kept the newlines around its actions, so the rendered geo block had an empty line between every entry. nginx accepts either form; the trimmed one is easier to read when debugging the generated config. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin --- packages/extra/ingress/templates/nginx-ingress.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/extra/ingress/templates/nginx-ingress.yaml b/packages/extra/ingress/templates/nginx-ingress.yaml index d95be57d8..4354a532e 100644 --- a/packages/extra/ingress/templates/nginx-ingress.yaml +++ b/packages/extra/ingress/templates/nginx-ingress.yaml @@ -138,9 +138,9 @@ spec: } geo $cozy_cf_source $cozy_cf_trusted { default 0; - {{ range $cidr := splitList "," (include "ingress.cloudflare-ips" .) }} + {{- range $cidr := splitList "," (include "ingress.cloudflare-ips" .) }} {{ $cidr }} 1; - {{ end }} + {{- end }} } map "$cozy_cf_trusted:$http_cf_connecting_ip" $cozy_cf_reject { default 0;