From 887dab8ec516c34576916316cbc7cba299e53e4b Mon Sep 17 00:00:00 2001 From: Vidar Langseid Date: Fri, 14 Aug 2026 09:30:47 +0200 Subject: [PATCH 1/5] IBX-11181: Filtered client-supplied X-Forwarded-* headers in Varnish VCL --- .../upsun/common/5.0/.platform/varnish.vcl | 23 ++++++++++++++++++- .../upsun/common/5.0/.platform/varnish9.vcl | 21 +++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/resources/upsun/common/5.0/.platform/varnish.vcl b/resources/upsun/common/5.0/.platform/varnish.vcl index 3898afb..0cef000 100644 --- a/resources/upsun/common/5.0/.platform/varnish.vcl +++ b/resources/upsun/common/5.0/.platform/varnish.vcl @@ -9,7 +9,7 @@ //import std; import xkey; -// Includes not available on Platform.sh, so inlining parameters.vlc: +// Includes not available on Platform.sh, so inlining parameters.vcl: acl invalidators { "127.0.0.1"; "192.168.0.0"/16; @@ -32,6 +32,27 @@ sub vcl_recv { // Add a Surrogate-Capability header to announce ESI support. set req.http.Surrogate-Capability = "abc=ESI/1.0"; + // Stop clients from injecting reverse proxy headers, as the application trusts them once + // framework.trusted_proxies is configured. + // + // The Ibexa Cloud router is authoritative for X-Forwarded-Proto, X-Client-IP and Client-Cdn, + // and discards the values a client sends for them, so those are kept as is. + // See https://fixed.docs.upsun.com/development/headers.html + // + // The headers below are not filtered by the router, so strip them here: + unset req.http.Forwarded; + unset req.http.X-Forwarded-Host; + unset req.http.X-Forwarded-Prefix; + + // X-Forwarded-For is only appended to by the router, so any leading entry is what the client + // sent. X-Client-IP is always set by the router, and holds the end-user IP also when the + // request came in via a CDN, so use that as the only value. + if (req.http.X-Client-IP) { + set req.http.X-Forwarded-For = req.http.X-Client-IP; + } else { + unset req.http.X-Forwarded-For; + } + // Ensure that the Symfony Router generates URLs correctly with Varnish if (req.http.X-Forwarded-Proto == "https" ) { set req.http.X-Forwarded-Port = "443"; diff --git a/resources/upsun/common/5.0/.platform/varnish9.vcl b/resources/upsun/common/5.0/.platform/varnish9.vcl index 5a90e4d..1c8d4cd 100644 --- a/resources/upsun/common/5.0/.platform/varnish9.vcl +++ b/resources/upsun/common/5.0/.platform/varnish9.vcl @@ -32,6 +32,27 @@ sub vcl_recv { // Add a Surrogate-Capability header to announce ESI support. set req.http.Surrogate-Capability = "abc=ESI/1.0"; + // Stop clients from injecting reverse proxy headers, as the application trusts them once + // framework.trusted_proxies is configured. + // + // The Ibexa Cloud router is authoritative for X-Forwarded-Proto, X-Client-IP and Client-Cdn, + // and discards the values a client sends for them, so those are kept as is. + // See https://fixed.docs.upsun.com/development/headers.html + // + // The headers below are not filtered by the router, so strip them here: + unset req.http.Forwarded; + unset req.http.X-Forwarded-Host; + unset req.http.X-Forwarded-Prefix; + + // X-Forwarded-For is only appended to by the router, so any leading entry is what the client + // sent. X-Client-IP is always set by the router, and holds the end-user IP also when the + // request came in via a CDN, so use that as the only value. + if (req.http.X-Client-IP) { + set req.http.X-Forwarded-For = req.http.X-Client-IP; + } else { + unset req.http.X-Forwarded-For; + } + // Ensure that the Symfony Router generates URLs correctly with Varnish if (req.http.X-Forwarded-Proto == "https" ) { set req.http.X-Forwarded-Port = "443"; From 64a4d282807bcb6d9b393eb9fac5df652a3d9d87 Mon Sep 17 00:00:00 2001 From: Vidar Langseid Date: Fri, 14 Aug 2026 13:58:18 +0200 Subject: [PATCH 2/5] Added varnishtest coverage for reverse proxy header filtering --- .github/workflows/backend-ci.yaml | 26 ++++++++++++++++++ tests/varnish/Dockerfile | 44 ++++++++++++++++++++++++++++++ tests/varnish/Dockerfile-9 | 21 +++++++++++++++ tests/varnish/README.md | 44 ++++++++++++++++++++++++++++++ tests/varnish/no-cdn.vtc | 38 ++++++++++++++++++++++++++ tests/varnish/no-client-ip.vtc | 29 ++++++++++++++++++++ tests/varnish/run.sh | 45 +++++++++++++++++++++++++++++++ tests/varnish/via-cdn.vtc | 36 +++++++++++++++++++++++++ 8 files changed, 283 insertions(+) create mode 100644 tests/varnish/Dockerfile create mode 100644 tests/varnish/Dockerfile-9 create mode 100644 tests/varnish/README.md create mode 100644 tests/varnish/no-cdn.vtc create mode 100644 tests/varnish/no-client-ip.vtc create mode 100755 tests/varnish/run.sh create mode 100644 tests/varnish/via-cdn.vtc diff --git a/.github/workflows/backend-ci.yaml b/.github/workflows/backend-ci.yaml index 916ce53..476a225 100644 --- a/.github/workflows/backend-ci.yaml +++ b/.github/workflows/backend-ci.yaml @@ -62,3 +62,29 @@ jobs: - name: Run test suite run: composer run-script --timeout=600 test + + varnish-vcl-tests: + name: Varnish VCL tests + runs-on: "ubuntu-24.04" + timeout-minutes: 20 + + steps: + - uses: actions/checkout@v6 + + # Builds varnish-modules from source, as the xkey vmod is not packaged for Varnish 6.0LTS. + - name: Build varnishtest image + run: docker build -t ibexa-varnishtest:6.0 tests/varnish + + - name: Run varnishtest suite + env: + IMAGE: ibexa-varnishtest:6.0 + run: tests/varnish/run.sh + + - name: Build Varnish 9 varnishtest image + run: docker build -t ibexa-varnishtest:9 -f tests/varnish/Dockerfile-9 tests/varnish + + - name: Run varnishtest suite against varnish9.vcl + env: + IMAGE: ibexa-varnishtest:9 + VCL: resources/upsun/common/5.0/.platform/varnish9.vcl + run: tests/varnish/run.sh diff --git a/tests/varnish/Dockerfile b/tests/varnish/Dockerfile new file mode 100644 index 0000000..1e967e9 --- /dev/null +++ b/tests/varnish/Dockerfile @@ -0,0 +1,44 @@ +# Image for running the varnishtest suite in tests/varnish/. +# +# Mirrors doc/docker/Dockerfile-varnish from ibexa/docker. It has to be Varnish 6.0LTS specifically: +# this VCL returns "miss" from vcl_hit, which 6.5 and later reject. The xkey vmod is not packaged +# for 6.0LTS, so varnish-modules is built from source, as in the image this mirrors. + +FROM debian:bullseye-slim + +ENV DEBIAN_FRONTEND=noninteractive + +ARG PACKAGECLOUD_URL=https://packagecloud.io/install/repositories/varnishcache/varnish60lts/script.deb.sh +ARG VARNISH_MODULES_VERSION=0.15.0 + +RUN set -xe \ + && buildDeps=" \ + make \ + automake \ + autotools-dev \ + libedit-dev \ + libjemalloc-dev \ + libncurses-dev \ + libpcre3-dev \ + libtool \ + pkg-config \ + python3-docutils \ + varnish-dev=6.0.12-1~bullseye \ + " \ + && apt-get update -q -y \ + && apt-get install -q -y --no-install-recommends ca-certificates curl \ + \ + && curl -s ${PACKAGECLOUD_URL} | bash \ + && apt-get install -q -y --allow-unauthenticated --no-install-recommends varnish=6.0.12-1~bullseye $buildDeps \ + \ + && curl -A "Docker" -o /tmp/varnish-modules.tar.gz -D - -L -s https://github.com/varnish/varnish-modules/archive/refs/tags/${VARNISH_MODULES_VERSION}.tar.gz \ + && tar zxpf /tmp/varnish-modules.tar.gz -C /tmp/ \ + && cd /tmp/varnish-modules-${VARNISH_MODULES_VERSION} \ + && ./bootstrap \ + && ./configure \ + && make \ + && make install \ + && rm -f /tmp/varnish-modules.tar.gz && rm -Rf /tmp/varnish-modules-${VARNISH_MODULES_VERSION} \ + \ + && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $buildDeps \ + && rm -rf /var/lib/apt/lists/* diff --git a/tests/varnish/Dockerfile-9 b/tests/varnish/Dockerfile-9 new file mode 100644 index 0000000..050c189 --- /dev/null +++ b/tests/varnish/Dockerfile-9 @@ -0,0 +1,21 @@ +# Image for running the varnishtest suite in tests/varnish/ against varnish9.vcl. +# +# Mirrors docker/Dockerfile-varnish9 from ibexa/docker: the VCL imports the xkey vmod, which the +# official image does not ship. + +FROM varnish:9.0 + +SHELL ["/bin/bash", "-c"] + +# Root is only needed to install the vmod; the image drops back to the unprivileged user the base +# image runs as by default. +USER root +RUN set -e && \ + read -ra vmod_deps <<< "$VMOD_DEPS" && \ + varnish_version=$(dpkg-query -W -f='${Version}' varnish) && \ + apt-get update && \ + apt-get -y --no-install-recommends install "${vmod_deps[@]/varnish-dev/varnish-dev=$varnish_version}" && \ + install-vmod https://github.com/varnish/varnish-modules/releases/download/0.28.0/varnish-modules-0.28.0.tar.gz && \ + rm -rf /var/lib/apt/lists/* + +USER varnish diff --git a/tests/varnish/README.md b/tests/varnish/README.md new file mode 100644 index 0000000..7891841 --- /dev/null +++ b/tests/varnish/README.md @@ -0,0 +1,44 @@ +Varnish VCL tests +================= + +`varnishtest` cases for `resources/upsun/common/5.0/.platform/varnish.vcl` and `varnish9.vcl`, the +VCLs shipped to Upsun / Ibexa Cloud installations. + +They load the real VCL file into a real Varnish and assert on the request the **backend** receives, +which is where the reverse proxy header filtering in `vcl_recv` can be observed. + +Cases +----- + +| Case | Scenario | Asserts | +|---|---|---| +| `no-cdn.vtc` | request straight through the Ibexa Cloud router | the client supplied part of `X-Forwarded-For` is dropped and the header is rebuilt from `X-Client-IP`; `X-Forwarded-Host` / `-Prefix` / `Forwarded` are stripped; `X-Forwarded-Proto` is kept | +| `via-cdn.vtc` | request arriving through a supported CDN | `X-Forwarded-For`, `X-Client-IP` and `Client-Cdn` are kept as the router left them, so Fastly detection still works | +| `no-client-ip.vtc` | no `X-Client-IP`, so the request did not come through the router | `X-Forwarded-For` is dropped entirely rather than trusted | + +The header behaviour being relied on is documented at +. + +Running them +------------ + +```bash +docker build -t ibexa-varnishtest:6.0 tests/varnish +IMAGE=ibexa-varnishtest:6.0 tests/varnish/run.sh + +docker build -t ibexa-varnishtest:9 -f tests/varnish/Dockerfile-9 tests/varnish +IMAGE=ibexa-varnishtest:9 VCL=resources/upsun/common/5.0/.platform/varnish9.vcl tests/varnish/run.sh +``` + +Notes +----- + +- The `varnish.vcl` image has to be **Varnish 6.0LTS specifically**. This VCL returns `miss` from + `vcl_hit`, which 6.5 and later reject, and the xkey vmod it imports is not packaged for 6.0LTS — + hence the source build in `Dockerfile`, mirroring `doc/docker/Dockerfile-varnish` in `ibexa/docker`. +- The VCL cannot be loaded as it stands: Platform.sh supplies the VCL version declaration, the `std` + import and the `app.backend()` director. `run.sh` prepends the first two and swaps the director for + a stub, so the shipped file itself is what gets tested. +- The backend listens on a fixed port (9081) so the stub can point at it. +- Each case uses its own URL and its own Varnish instance: `X-Forwarded-*` is not part of the cache + key, so cases sharing a cache would be served each other's responses. diff --git a/tests/varnish/no-cdn.vtc b/tests/varnish/no-cdn.vtc new file mode 100644 index 0000000..0515dc4 --- /dev/null +++ b/tests/varnish/no-cdn.vtc @@ -0,0 +1,38 @@ +varnishtest "Without a CDN, X-Forwarded-For is rebuilt from the router supplied X-Client-IP" + +# The Ibexa Cloud router only *appends* the real client IP to whatever the client sent in +# X-Forwarded-For, so every leading entry is client controlled and must not survive. +# See https://fixed.docs.upsun.com/development/headers.html + +server s1 -listen "127.0.0.1:9081" { + rxreq + + # Client supplied prefix dropped: only the router's own value is left. + expect req.http.X-Forwarded-For == "203.0.113.9" + + # Not filtered by the router, so the VCL strips them. + expect req.http.X-Forwarded-Host == + expect req.http.X-Forwarded-Prefix == + expect req.http.Forwarded == + + # Router authoritative, kept as is. + expect req.http.X-Client-IP == "203.0.113.9" + expect req.http.X-Forwarded-Proto == "https" + expect req.http.X-Forwarded-Port == "443" + + txresp +} -start + +varnish v1 -arg "-f /etc/varnish/default.vcl" -start + +client c1 { + txreq -url "/no-cdn" \ + -hdr "X-Forwarded-For: 6.6.6.6, 203.0.113.9" \ + -hdr "X-Client-IP: 203.0.113.9" \ + -hdr "X-Forwarded-Proto: https" \ + -hdr "X-Forwarded-Host: evil.example" \ + -hdr "X-Forwarded-Prefix: /admin" \ + -hdr "Forwarded: for=6.6.6.6;host=evil.example;proto=https" + rxresp + expect resp.status == 200 +} -run diff --git a/tests/varnish/no-client-ip.vtc b/tests/varnish/no-client-ip.vtc new file mode 100644 index 0000000..195bd68 --- /dev/null +++ b/tests/varnish/no-client-ip.vtc @@ -0,0 +1,29 @@ +varnishtest "Without X-Client-IP there is nothing trustworthy, so X-Forwarded-For is dropped" + +# X-Client-IP is always set by the Ibexa Cloud router. If it is absent the request did not come +# through the router, so the client supplied X-Forwarded-For must not be handed to the application. + +server s1 -listen "127.0.0.1:9081" { + rxreq + + expect req.http.X-Forwarded-For == + expect req.http.X-Client-IP == + + expect req.http.X-Forwarded-Host == + expect req.http.X-Forwarded-Prefix == + expect req.http.Forwarded == + + txresp +} -start + +varnish v1 -arg "-f /etc/varnish/default.vcl" -start + +client c1 { + txreq -url "/no-client-ip" \ + -hdr "X-Forwarded-For: 6.6.6.6" \ + -hdr "X-Forwarded-Host: evil.example" \ + -hdr "X-Forwarded-Prefix: /admin" \ + -hdr "Forwarded: for=6.6.6.6;host=evil.example" + rxresp + expect resp.status == 200 +} -run diff --git a/tests/varnish/run.sh b/tests/varnish/run.sh new file mode 100755 index 0000000..3aa9b33 --- /dev/null +++ b/tests/varnish/run.sh @@ -0,0 +1,45 @@ +#!/bin/bash +# +# Runs the varnishtest suite for the Platform.sh / Ibexa Cloud VCL. +# +# That VCL is not loadable as it stands: Platform.sh supplies the VCL version declaration, the std +# import and the app.backend() director. This script prepends the first two and swaps the director +# for a stub pointing at the test backend, then loads the resulting file into a real Varnish, so +# what is tested is the shipped file rather than a copy of it. +# +# Usage: +# docker build -t ibexa-varnishtest:7 tests/varnish +# IMAGE=ibexa-varnishtest:7 tests/varnish/run.sh +# +# docker build -t ibexa-varnishtest:9 -f tests/varnish/Dockerfile-9 tests/varnish +# IMAGE=ibexa-varnishtest:9 VCL=resources/upsun/common/5.0/.platform/varnish9.vcl tests/varnish/run.sh + +set -euo pipefail + +TESTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ROOTDIR="$(cd "$TESTDIR/../.." && pwd)" +VCL="${VCL:-$ROOTDIR/resources/upsun/common/5.0/.platform/varnish.vcl}" +IMAGE="${IMAGE:-ibexa-varnishtest:7}" + +WORKDIR="$(mktemp -d)" +trap 'rm -rf "$WORKDIR"' EXIT + +{ + echo "// Prologue supplied by Platform.sh at runtime, added here so the file can be loaded." + echo "vcl 4.1;" + echo "import std;" + echo "backend stub { .host = \"127.0.0.1\"; .port = \"9081\"; }" + sed 's/app\.backend()/stub/' "$VCL" +} > "$WORKDIR/default.vcl" + +echo "==> $(basename "$(dirname "$(dirname "$VCL")")")/$(basename "$VCL") (${IMAGE})" + +for vtc in "$TESTDIR"/*.vtc; do + echo "--> $(basename "$vtc")" + docker run --rm \ + -v "$WORKDIR/default.vcl:/etc/varnish/default.vcl:ro" \ + -v "$vtc:/case.vtc:ro" \ + --entrypoint varnishtest "$IMAGE" /case.vtc +done + +echo "All varnishtest cases passed." diff --git a/tests/varnish/via-cdn.vtc b/tests/varnish/via-cdn.vtc new file mode 100644 index 0000000..49b313c --- /dev/null +++ b/tests/varnish/via-cdn.vtc @@ -0,0 +1,36 @@ +varnishtest "Behind a supported CDN, the router supplied end client IP and Client-Cdn are kept" + +# When the request arrives via a CDN the router replaces X-Forwarded-For with the end client IP and +# sets Client-Cdn, discarding whatever the client sent for either. Ibexa DXP uses Client-Cdn to +# detect Fastly, so it has to reach the application. +# See https://fixed.docs.upsun.com/development/headers.html + +server s1 -listen "127.0.0.1:9081" { + rxreq + + expect req.http.X-Forwarded-For == "198.51.100.7" + expect req.http.X-Client-IP == "198.51.100.7" + expect req.http.Client-Cdn == "fastly" + + expect req.http.X-Forwarded-Host == + expect req.http.X-Forwarded-Prefix == + expect req.http.Forwarded == + + expect req.http.X-Forwarded-Proto == "https" + expect req.http.X-Forwarded-Port == "443" + + txresp +} -start + +varnish v1 -arg "-f /etc/varnish/default.vcl" -start + +client c1 { + txreq -url "/via-cdn" \ + -hdr "X-Forwarded-For: 198.51.100.7" \ + -hdr "X-Client-IP: 198.51.100.7" \ + -hdr "Client-Cdn: fastly" \ + -hdr "X-Forwarded-Proto: https" \ + -hdr "X-Forwarded-Host: evil.example" + rxresp + expect resp.status == 200 +} -run From 77bfc5622c81fe98ff863e2364ad063bf54bb0dd Mon Sep 17 00:00:00 2001 From: Vidar Langseid Date: Wed, 14 Oct 2026 11:19:12 +0200 Subject: [PATCH 3/5] Moved the varnishtest image off end-of-life Debian 11 --- tests/varnish/Dockerfile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/varnish/Dockerfile b/tests/varnish/Dockerfile index 1e967e9..97cb7fb 100644 --- a/tests/varnish/Dockerfile +++ b/tests/varnish/Dockerfile @@ -4,7 +4,7 @@ # this VCL returns "miss" from vcl_hit, which 6.5 and later reject. The xkey vmod is not packaged # for 6.0LTS, so varnish-modules is built from source, as in the image this mirrors. -FROM debian:bullseye-slim +FROM debian:bookworm-slim ENV DEBIAN_FRONTEND=noninteractive @@ -23,13 +23,14 @@ RUN set -xe \ libtool \ pkg-config \ python3-docutils \ - varnish-dev=6.0.12-1~bullseye \ + sphinx-common \ + varnish-dev=6.0.12-1~bookworm \ " \ && apt-get update -q -y \ && apt-get install -q -y --no-install-recommends ca-certificates curl \ \ && curl -s ${PACKAGECLOUD_URL} | bash \ - && apt-get install -q -y --allow-unauthenticated --no-install-recommends varnish=6.0.12-1~bullseye $buildDeps \ + && apt-get install -q -y --allow-unauthenticated --no-install-recommends varnish=6.0.12-1~bookworm $buildDeps \ \ && curl -A "Docker" -o /tmp/varnish-modules.tar.gz -D - -L -s https://github.com/varnish/varnish-modules/archive/refs/tags/${VARNISH_MODULES_VERSION}.tar.gz \ && tar zxpf /tmp/varnish-modules.tar.gz -C /tmp/ \ From 4f6a7bd4f64c982a3514215d7abf08b9863390c2 Mon Sep 17 00:00:00 2001 From: Vidar Langseid Date: Mon, 19 Oct 2026 14:12:27 +0200 Subject: [PATCH 4/5] Ported TrustedHeaderClientIpEventSubscriber from ibexa/core#699 and registered it as a service --- .../TrustedHeaderClientIpEventSubscriber.php | 31 +--- .../config/services/event_subscribers.yaml | 6 + ...ustedHeaderClientIpEventSubscriberTest.php | 153 ++++++++++++++++++ 3 files changed, 165 insertions(+), 25 deletions(-) create mode 100644 src/bundle/Resources/config/services/event_subscribers.yaml create mode 100644 tests/bundle/EventSubscriber/TrustedHeaderClientIpEventSubscriberTest.php diff --git a/src/bundle/EventSubscriber/TrustedHeaderClientIpEventSubscriber.php b/src/bundle/EventSubscriber/TrustedHeaderClientIpEventSubscriber.php index c7e4d0c..4eb33e1 100644 --- a/src/bundle/EventSubscriber/TrustedHeaderClientIpEventSubscriber.php +++ b/src/bundle/EventSubscriber/TrustedHeaderClientIpEventSubscriber.php @@ -15,46 +15,27 @@ final class TrustedHeaderClientIpEventSubscriber implements EventSubscriberInterface { - private const string PLATFORM_SH_TRUSTED_HEADER_CLIENT_IP = 'X-Client-IP'; - - public function __construct( - private readonly ?string $trustedHeaderName - ) {} - public static function getSubscribedEvents(): array { return [ - KernelEvents::REQUEST => ['onKernelRequest', PHP_INT_MAX - 1], + KernelEvents::REQUEST => ['onKernelRequest', PHP_INT_MAX], ]; } public function onKernelRequest(RequestEvent $event): void { $request = $event->getRequest(); - - $trustedProxies = Request::getTrustedProxies(); $trustedHeaderSet = Request::getTrustedHeaderSet(); - $trustedHeaderName = $this->trustedHeaderName; - if (null === $trustedHeaderName && $this->isUpsunProxy($request)) { - $trustedHeaderName = self::PLATFORM_SH_TRUSTED_HEADER_CLIENT_IP; - } - - if (null === $trustedHeaderName) { + // -1 means framework.trusted_proxies is not configured; passing it on would trust every forwarded header + if ($trustedHeaderSet === -1) { return; } - $trustedClientIp = $request->headers->get($trustedHeaderName); - - if (null !== $trustedClientIp) { - if ($trustedHeaderSet !== -1) { - $trustedHeaderSet |= Request::HEADER_X_FORWARDED_FOR; - } - $request->headers->set('X_FORWARDED_FOR', $trustedClientIp); + if ($this->isUpsunProxy($request) && $request->headers->get('Client-Cdn') === 'fastly') { + /** @var int<0, 63> $trustedHeaderSet */ + Request::setTrustedProxies(['REMOTE_ADDR'], $trustedHeaderSet); } - - /** @var int<0, 63> $trustedHeaderSet */ - Request::setTrustedProxies($trustedProxies, $trustedHeaderSet); } private function isUpsunProxy(Request $request): bool diff --git a/src/bundle/Resources/config/services/event_subscribers.yaml b/src/bundle/Resources/config/services/event_subscribers.yaml new file mode 100644 index 0000000..c01a029 --- /dev/null +++ b/src/bundle/Resources/config/services/event_subscribers.yaml @@ -0,0 +1,6 @@ +services: + _defaults: + autowire: true + autoconfigure: true + + Ibexa\Bundle\Cloud\EventSubscriber\TrustedHeaderClientIpEventSubscriber: ~ diff --git a/tests/bundle/EventSubscriber/TrustedHeaderClientIpEventSubscriberTest.php b/tests/bundle/EventSubscriber/TrustedHeaderClientIpEventSubscriberTest.php new file mode 100644 index 0000000..d1b56f1 --- /dev/null +++ b/tests/bundle/EventSubscriber/TrustedHeaderClientIpEventSubscriberTest.php @@ -0,0 +1,153 @@ +originalTrustedProxies = Request::getTrustedProxies(); + $this->originalTrustedHeaderSet = Request::getTrustedHeaderSet(); + $this->originalRemoteAddr = $_SERVER['REMOTE_ADDR'] ?? null; + + // Request::setTrustedProxies() resolves 'REMOTE_ADDR' from $_SERVER, not from the request + $_SERVER['REMOTE_ADDR'] = self::REAL_CLIENT_IP; + Request::setTrustedProxies([], self::TRUSTED_HEADER_SET); + } + + protected function tearDown(): void + { + $_SERVER['REMOTE_ADDR'] = $this->originalRemoteAddr; + + /** @var int<0, 63> $trustedHeaderSet */ + $trustedHeaderSet = $this->originalTrustedHeaderSet; + Request::setTrustedProxies($this->originalTrustedProxies, $trustedHeaderSet); + + parent::tearDown(); + } + + /** + * @return iterable, array}> + */ + public static function getTrustedHeaderEventSubscriberTestData(): iterable + { + yield 'request from random client received outside Ibexa Cloud' => [ + false, + [], + [], + ]; + + yield 'request from random client, forging Client-Cdn, received outside Ibexa Cloud' => [ + false, + ['Client-Cdn' => 'fastly'], + [], + ]; + + yield 'request from random client received on Ibexa Cloud' => [ + false, + [], + ['PLATFORM_RELATIONSHIPS' => true], + ]; + + yield 'request via Fastly received on Ibexa Cloud' => [ + true, + ['Client-Cdn' => 'fastly'], + ['PLATFORM_RELATIONSHIPS' => true], + ]; + } + + /** + * @dataProvider getTrustedHeaderEventSubscriberTestData + * + * @param array $headers + * @param array $server + */ + public function testTrustedHeaderEventSubscriberWithTrustedProxy( + bool $isFromTrustedProxy, + array $headers, + array $server + ): void { + $request = $this->dispatchRequestEvent($headers, $server); + + self::assertSame($isFromTrustedProxy, $request->isFromTrustedProxy()); + self::assertSame(self::TRUSTED_HEADER_SET, Request::getTrustedHeaderSet()); + } + + public function testTrustedHeaderEventSubscriberWithoutTrustedProxiesConfigured(): void + { + // framework.trusted_proxies not configured, so Symfony never sets a trusted header set + // @phpstan-ignore argument.type + Request::setTrustedProxies([], -1); + + $request = $this->dispatchRequestEvent( + [ + 'Client-Cdn' => 'fastly', + 'X-Forwarded-Host' => 'forged.example.com', + ], + ['PLATFORM_RELATIONSHIPS' => true] + ); + + self::assertFalse($request->isFromTrustedProxy()); + self::assertSame('localhost', $request->getHost()); + } + + /** + * @param array $headers + * @param array $server + */ + private function dispatchRequestEvent( + array $headers, + array $server + ): Request { + $eventDispatcher = new EventDispatcher(); + $eventDispatcher->addSubscriber(new TrustedHeaderClientIpEventSubscriber()); + + $request = Request::create('/', Request::METHOD_GET, [], [], [], array_merge( + $server, + ['REMOTE_ADDR' => self::REAL_CLIENT_IP], + )); + $request->headers->add($headers); + + $event = $eventDispatcher->dispatch(new RequestEvent( + $this->createMock(KernelInterface::class), + $request, + HttpKernelInterface::MAIN_REQUEST + ), KernelEvents::REQUEST); + + return $event->getRequest(); + } +} From 26e2f943a0a82a4918da127bb8d401de03c5aa57 Mon Sep 17 00:00:00 2001 From: Vidar Langseid Date: Mon, 19 Oct 2026 14:56:20 +0200 Subject: [PATCH 5/5] Apply batched suggestions from code review Co-authored-by: Bartek Wajda --- .github/workflows/backend-ci.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/backend-ci.yaml b/.github/workflows/backend-ci.yaml index 476a225..e6d7ab5 100644 --- a/.github/workflows/backend-ci.yaml +++ b/.github/workflows/backend-ci.yaml @@ -65,11 +65,11 @@ jobs: varnish-vcl-tests: name: Varnish VCL tests - runs-on: "ubuntu-24.04" + runs-on: "ubuntu-26.04" timeout-minutes: 20 steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@v7 # Builds varnish-modules from source, as the xkey vmod is not packaged for Varnish 6.0LTS. - name: Build varnishtest image