fix(ingress): restore visitor IPs through SSL passthrough (#5443)

## What this PR does

Closes #5327.

With `cloudflareProxy: true`, HTTPS reaches nginx through
ssl-passthrough on loopback, so an IP whitelist sees `127.0.0.1` instead
of the visitor and rejects legitimate Cloudflare traffic. This change
validates the original transport peer before the whitelist, restoring
the visitor IP for Cloudflare requests without reopening the spoofing
fixed by #4444.

Direct HTTPS requests that bypass Cloudflare still appear as
`127.0.0.1`, as before.

### Validation

- `helm unittest packages/extra/ingress`: 34/34 tests passed, including
a separate run with helm-unittest 1.1.2.
- `make --directory=packages/extra/ingress show NAMESPACE=tenant-root`:
passed against the test cluster after rebasing on `upstream/main`.
- On an isolated ingress-nginx v1.11.6 controller, `nginx -t` passed.
Spoofed headers over HTTP and HTTPS returned 403. A trusted Cloudflare
PROXY-protocol source with an allowed visitor passed the whitelist and
the access log contained the visitor IP; an untrusted source or a
visitor outside the whitelist returned 403.
- A real Cloudflare end-to-end request was not tested.

### Screenshots

Not applicable; this PR does not change the UI.

### Downstream repositories

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
fix(ingress): restore visitor IP handling for Cloudflare-proxied HTTPS through ssl-passthrough while rejecting spoofed `CF-Connecting-IP` headers before IP whitelists
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Cloudflare-proxied ingress accepts `CF-Connecting-IP` only from
trusted Cloudflare address ranges and returns HTTP 403 when an untrusted
source provides the header.
* When no PROXY protocol address is available, source checks use the
direct connection address.
* Loopback addresses are included in trusted real-IP ranges, and the
existing keepalive setting is preserved.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Aleksei Sviridkin authored and GitHub committed 2026-11-26 04:39:50 +03:00
commit 180c01170f
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: