From ae3358181a40f55a4c6df67ea3e8a0f4a1429ce0 Mon Sep 17 00:00:00 2001 From: Vidar Langseid Date: Wed, 28 Oct 2026 17:23:47 +0100 Subject: [PATCH 1/3] IBX-11181: Filtered client-supplied X-Forwarded-* headers in Varnish VCL (#87) For more details see https://ibexa.atlassian.net/browse/IBX-11181 and https://github.com/ibexa/http-cache/pull/87 Key changes: * Stripped client-supplied `X-Forwarded-*`, `Forwarded`, `X-Client-IP` and `Client-Cdn` headers in `vcl_recv` of `varnish5/6/7/9.vcl` unless the peer is in the new `trusted_proxies` ACL * Overwrote `X-Forwarded-For` with the real client IP for untrusted peers * Added the `trusted_proxies` ACL to `parameters.vcl` and documented it in the Varnish README * Added varnishtest coverage for trusted and untrusted peers, run in a new CI job * Added Behat coverage for header filtering and trusted proxy pass-through --- .github/workflows/backend-ci.yaml | 19 +++++ .github/workflows/browser-tests.yaml | 27 +++++++ behat_suites.yml | 34 +++++++-- composer.json | 1 + docs/varnish/README.md | 14 ++++ docs/varnish/vcl/parameters.vcl | 13 ++++ docs/varnish/vcl/varnish5.vcl | 23 ++++++ docs/varnish/vcl/varnish6.vcl | 23 ++++++ docs/varnish/vcl/varnish7.vcl | 23 ++++++ docs/varnish/vcl/varnish9.vcl | 23 ++++++ features/setup/setup.feature | 13 ++++ features/varnish/proxyHeaders.feature | 72 +++++++++++++++++++ .../views/tests/cache/proxy_headers.html.twig | 22 ++++++ src/lib/Behat/Context/ProxyHeaderContext.php | 28 ++++++++ tests/varnish/Dockerfile | 20 ++++++ tests/varnish/Dockerfile-9 | 21 ++++++ tests/varnish/README.md | 40 +++++++++++ tests/varnish/fixtures/parameters-trusted.vcl | 23 ++++++ .../varnish/fixtures/parameters-untrusted.vcl | 23 ++++++ tests/varnish/run.sh | 69 ++++++++++++++++++ tests/varnish/trusted.vtc | 41 +++++++++++ tests/varnish/untrusted.vtc | 41 +++++++++++ 22 files changed, 607 insertions(+), 6 deletions(-) create mode 100644 features/varnish/proxyHeaders.feature create mode 100644 src/bundle/Resources/views/tests/cache/proxy_headers.html.twig create mode 100644 src/lib/Behat/Context/ProxyHeaderContext.php 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/fixtures/parameters-trusted.vcl create mode 100644 tests/varnish/fixtures/parameters-untrusted.vcl create mode 100755 tests/varnish/run.sh create mode 100644 tests/varnish/trusted.vtc create mode 100644 tests/varnish/untrusted.vtc diff --git a/.github/workflows/backend-ci.yaml b/.github/workflows/backend-ci.yaml index d14b1655..45ac3eb2 100644 --- a/.github/workflows/backend-ci.yaml +++ b/.github/workflows/backend-ci.yaml @@ -87,6 +87,25 @@ jobs: - name: Run test suite run: composer run-script --timeout=600 test + varnish-vcl-tests: + name: Varnish VCL tests + runs-on: "ubuntu-26.04" + timeout-minutes: 10 + + steps: + - uses: actions/checkout@v7 + + - name: Build varnishtest images + run: | + docker build -t ibexa-varnishtest:7 tests/varnish + docker build -t ibexa-varnishtest:9 -f tests/varnish/Dockerfile-9 tests/varnish + + - name: Run varnishtest suite + env: + IMAGE_VARNISH7: ibexa-varnishtest:7 + IMAGE_VARNISH9: ibexa-varnishtest:9 + run: tests/varnish/run.sh varnish7.vcl varnish9.vcl + integration-tests: name: "REST integration tests" uses: ibexa/rest/.github/workflows/integration-tests-callable.yaml@4.6 diff --git a/.github/workflows/browser-tests.yaml b/.github/workflows/browser-tests.yaml index df031d8f..da66ee95 100644 --- a/.github/workflows/browser-tests.yaml +++ b/.github/workflows/browser-tests.yaml @@ -34,6 +34,24 @@ jobs: test-suite: '--mode=standard --profile=httpCache --suite=varnish7' test-setup-phase-1: '--mode=standard --profile=httpCache --suite=setup' secrets: inherit + varnish7-trusted-proxy: + name: "Varnish 7 integration tests (trusted proxy)" + uses: ibexa/gh-workflows/.github/workflows/browser-tests.yml@main + with: + project-edition: 'oss' + setup: "doc/docker/base-dev.yml:doc/docker/varnish7.yml:doc/docker/varnish-trusted-proxy.yml:doc/docker/selenium.yml" + test-suite: '--mode=standard --profile=httpCache --suite=varnish-trusted-proxy' + test-setup-phase-1: '--mode=standard --profile=httpCache --suite=setup' + secrets: inherit + varnish6-trusted-proxy: + name: "Varnish 6 integration tests (trusted proxy)" + uses: ibexa/gh-workflows/.github/workflows/browser-tests.yml@main + with: + project-edition: 'oss' + setup: "doc/docker/base-dev.yml:doc/docker/varnish.yml:doc/docker/varnish-trusted-proxy.yml:doc/docker/selenium.yml" + test-suite: '--mode=standard --profile=httpCache --suite=varnish-trusted-proxy' + test-setup-phase-1: '--mode=standard --profile=httpCache --suite=setup' + secrets: inherit varnish6-translation-aware: name: "Varnish integration tests (translation-aware)" uses: ibexa/gh-workflows/.github/workflows/browser-tests.yml@main @@ -83,6 +101,15 @@ jobs: test-suite: '--mode=standard --profile=httpCache --suite=varnish9' test-setup-phase-1: '--mode=standard --profile=httpCache --suite=setup' secrets: inherit + varnish9-trusted-proxy: + name: "Varnish 9 integration tests (trusted proxy)" + uses: ibexa/gh-workflows/.github/workflows/browser-tests.yml@main + with: + project-edition: 'oss' + setup: "doc/docker/base-dev.yml:doc/docker/varnish9.yml:doc/docker/varnish-trusted-proxy.yml:doc/docker/selenium.yml" + test-suite: '--mode=standard --profile=httpCache --suite=varnish-trusted-proxy' + test-setup-phase-1: '--mode=standard --profile=httpCache --suite=setup' + secrets: inherit varnish9-translation-aware: name: "Varnish 9 integration tests (translation-aware)" uses: ibexa/gh-workflows/.github/workflows/browser-tests.yml@main diff --git a/behat_suites.yml b/behat_suites.yml index e162445b..aefc28d1 100644 --- a/behat_suites.yml +++ b/behat_suites.yml @@ -17,7 +17,7 @@ httpCache: paths: - '%paths.base%/vendor/ibexa/http-cache/features/varnish' filters: - tags: '@varnish6&&~@translationAware' + tags: '@varnish6&&~@translationAware&&~@trustedProxy' contexts: - Ibexa\Behat\API\Context\TestContext - Ibexa\Behat\API\Context\ContentTypeContext @@ -28,11 +28,12 @@ httpCache: - Ibexa\Behat\Browser\Context\AuthenticationContext - Behat\MinkExtension\Context\MinkContext - Ibexa\Behat\Browser\Context\ContentPreviewContext + - Ibexa\HttpCache\Behat\Context\ProxyHeaderContext varnish6-translation-aware: paths: - '%paths.base%/vendor/ibexa/http-cache/features/varnish' filters: - tags: '@varnish6&&~@translationNotAware' + tags: '@varnish6&&~@translationNotAware&&~@trustedProxy' contexts: - Ibexa\Behat\API\Context\TestContext - Ibexa\Behat\API\Context\ContentTypeContext @@ -43,11 +44,12 @@ httpCache: - Ibexa\Behat\Browser\Context\AuthenticationContext - Behat\MinkExtension\Context\MinkContext - Ibexa\Behat\Browser\Context\ContentPreviewContext + - Ibexa\HttpCache\Behat\Context\ProxyHeaderContext varnish7: paths: - '%paths.base%/vendor/ibexa/http-cache/features/varnish' filters: - tags: '@varnish7&&~@translationAware' + tags: '@varnish7&&~@translationAware&&~@trustedProxy' contexts: - Ibexa\Behat\API\Context\TestContext - Ibexa\Behat\API\Context\ContentTypeContext @@ -58,11 +60,28 @@ httpCache: - Ibexa\Behat\Browser\Context\AuthenticationContext - Behat\MinkExtension\Context\MinkContext - Ibexa\Behat\Browser\Context\ContentPreviewContext + - Ibexa\HttpCache\Behat\Context\ProxyHeaderContext + varnish-trusted-proxy: + paths: + - '%paths.base%/vendor/ibexa/http-cache/features/varnish' + filters: + tags: '@trustedProxy' + contexts: + - Ibexa\Behat\API\Context\TestContext + - Ibexa\Behat\API\Context\ContentTypeContext + - Ibexa\Behat\Core\Context\TimeContext + - Ibexa\Behat\Core\Context\ConfigurationContext + - Ibexa\Behat\API\Context\ContentContext + - Ibexa\Behat\Browser\Context\BrowserContext + - Ibexa\Behat\Browser\Context\AuthenticationContext + - Behat\MinkExtension\Context\MinkContext + - Ibexa\Behat\Browser\Context\ContentPreviewContext + - Ibexa\HttpCache\Behat\Context\ProxyHeaderContext varnish7-translation-aware: paths: - '%paths.base%/vendor/ibexa/http-cache/features/varnish' filters: - tags: '@varnish7&&~@translationNotAware' + tags: '@varnish7&&~@translationNotAware&&~@trustedProxy' contexts: - Ibexa\Behat\API\Context\TestContext - Ibexa\Behat\API\Context\ContentTypeContext @@ -73,11 +92,12 @@ httpCache: - Ibexa\Behat\Browser\Context\AuthenticationContext - Behat\MinkExtension\Context\MinkContext - Ibexa\Behat\Browser\Context\ContentPreviewContext + - Ibexa\HttpCache\Behat\Context\ProxyHeaderContext varnish9: paths: - '%paths.base%/vendor/ibexa/http-cache/features/varnish' filters: - tags: '@varnish9&&~@translationAware' + tags: '@varnish9&&~@translationAware&&~@trustedProxy' contexts: - Ibexa\Behat\API\Context\TestContext - Ibexa\Behat\API\Context\ContentTypeContext @@ -88,11 +108,12 @@ httpCache: - Ibexa\Behat\Browser\Context\AuthenticationContext - Behat\MinkExtension\Context\MinkContext - Ibexa\Behat\Browser\Context\ContentPreviewContext + - Ibexa\HttpCache\Behat\Context\ProxyHeaderContext varnish9-translation-aware: paths: - '%paths.base%/vendor/ibexa/http-cache/features/varnish' filters: - tags: '@varnish9&&~@translationNotAware' + tags: '@varnish9&&~@translationNotAware&&~@trustedProxy' contexts: - Ibexa\Behat\API\Context\TestContext - Ibexa\Behat\API\Context\ContentTypeContext @@ -103,6 +124,7 @@ httpCache: - Ibexa\Behat\Browser\Context\AuthenticationContext - Behat\MinkExtension\Context\MinkContext - Ibexa\Behat\Browser\Context\ContentPreviewContext + - Ibexa\HttpCache\Behat\Context\ProxyHeaderContext setup: paths: - '%paths.base%/vendor/ibexa/http-cache/features/setup/setup.feature' diff --git a/composer.json b/composer.json index 34810d95..5b543277 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,7 @@ "toflar/psr6-symfony-http-cache-store": "^2.2 || ^3.0" }, "require-dev": { + "ibexa/behat": "~4.6.0", "ibexa/ci-scripts": "^0.2@dev", "ibexa/doctrine-schema": "~4.6.0@dev", "ibexa/doctrine-migrations": "~4.6.x-dev", diff --git a/docs/varnish/README.md b/docs/varnish/README.md index af35befa..703ca570 100644 --- a/docs/varnish/README.md +++ b/docs/varnish/README.md @@ -13,6 +13,20 @@ For Varnish to work properly with eZ, you'll need to use the provided configurat * [eZ Platform 2.5LTs optimized Varnish 5.1+ VCL](vcl/varnish5.vcl) +Trusted proxies +--------------- +The VCL strips the `X-Forwarded-*` and `Forwarded` request headers from every client that is not +listed in the `trusted_proxies` ACL in [parameters.vcl](vcl/parameters.vcl). The application trusts +those headers as soon as `framework.trusted_proxies` is configured, so leaving them unfiltered lets +a client spoof the scheme, host and IP address it is seen with. + +If a TLS terminator, load balancer or CDN runs in front of Varnish, add its IP to that ACL — +otherwise the `X-Forwarded-Proto` it sets is discarded and URLs are generated as `http://`. + +_Upgrading: `parameters.vcl` and the VCL above must be updated together. The VCL references the +`trusted_proxies` ACL, so an older `parameters.vcl` without it makes Varnish fail to load the +configuration._ + For tuning the VCL further to you needs, see the following relevant examples: - [FOSHttpCache documentation](https://foshttpcache.readthedocs.io/en/latest/varnish-configuration.html) - Symfony documentation [4.4](https://symfony.com/doc/4.4/http_cache/varnish.html) diff --git a/docs/varnish/vcl/parameters.vcl b/docs/varnish/vcl/parameters.vcl index b211df55..bb3cf66a 100644 --- a/docs/varnish/vcl/parameters.vcl +++ b/docs/varnish/vcl/parameters.vcl @@ -26,3 +26,16 @@ acl debuggers { "127.0.0.1"; "192.168.0.0"/16; } + +// ACL for reverse proxies, TLS terminators and CDNs running in front of Varnish +// +// Only requests coming from these are allowed to set the "X-Forwarded-*" and "Forwarded" headers, +// see vcl_recv. Requests from anyone else get them stripped, as the application trusts them once +// framework.trusted_proxies is configured, which would otherwise let a client spoof the scheme, +// host and client IP it is seen with. +// +// Add the IP of your TLS terminator/load balancer/CDN here if one runs in front of Varnish, +// otherwise leave this as is. +acl trusted_proxies { + "127.0.0.1"; +} diff --git a/docs/varnish/vcl/varnish5.vcl b/docs/varnish/vcl/varnish5.vcl index 74c8343c..abc396ae 100644 --- a/docs/varnish/vcl/varnish5.vcl +++ b/docs/varnish/vcl/varnish5.vcl @@ -23,6 +23,29 @@ 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. See parameters.vcl for the trusted_proxies ACL. + if (client.ip !~ trusted_proxies) { + // Varnish appends client.ip to X-Forwarded-For before vcl_recv is entered, so overwrite + // rather than unset, to keep the real client IP as the only entry. + set req.http.X-Forwarded-For = client.ip; + unset req.http.Forwarded; + unset req.http.X-Forwarded-Host; + unset req.http.X-Forwarded-Prefix; + unset req.http.X-Forwarded-Port; + // To prevent the Ibexa Cloud detection in Ibexa DXP from kicking in: + unset req.http.X-Client-IP; + unset req.http.Client-Cdn; + + // Varnish itself only knows the scheme when a TLS terminator hands the connection over + // using the PROXY protocol, in which case server.ip is the address the client connected to. + if (std.port(server.ip) == 443) { + set req.http.X-Forwarded-Proto = "https"; + } else { + unset req.http.X-Forwarded-Proto; + } + } + // 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/docs/varnish/vcl/varnish6.vcl b/docs/varnish/vcl/varnish6.vcl index b7cab5d9..08da92d8 100644 --- a/docs/varnish/vcl/varnish6.vcl +++ b/docs/varnish/vcl/varnish6.vcl @@ -23,6 +23,29 @@ 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. See parameters.vcl for the trusted_proxies ACL. + if (client.ip !~ trusted_proxies) { + // Varnish appends client.ip to X-Forwarded-For before vcl_recv is entered, so overwrite + // rather than unset, to keep the real client IP as the only entry. + set req.http.X-Forwarded-For = client.ip; + unset req.http.Forwarded; + unset req.http.X-Forwarded-Host; + unset req.http.X-Forwarded-Prefix; + unset req.http.X-Forwarded-Port; + // To prevent the Ibexa Cloud detection in Ibexa DXP from kicking in: + unset req.http.X-Client-IP; + unset req.http.Client-Cdn; + + // Varnish itself only knows the scheme when a TLS terminator hands the connection over + // using the PROXY protocol, in which case server.ip is the address the client connected to. + if (std.port(server.ip) == 443) { + set req.http.X-Forwarded-Proto = "https"; + } else { + unset req.http.X-Forwarded-Proto; + } + } + // 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/docs/varnish/vcl/varnish7.vcl b/docs/varnish/vcl/varnish7.vcl index 2e9afa91..dc40c912 100644 --- a/docs/varnish/vcl/varnish7.vcl +++ b/docs/varnish/vcl/varnish7.vcl @@ -21,6 +21,29 @@ 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. See parameters.vcl for the trusted_proxies ACL. + if (client.ip !~ trusted_proxies) { + // Varnish appends client.ip to X-Forwarded-For before vcl_recv is entered, so overwrite + // rather than unset, to keep the real client IP as the only entry. + set req.http.X-Forwarded-For = client.ip; + unset req.http.Forwarded; + unset req.http.X-Forwarded-Host; + unset req.http.X-Forwarded-Prefix; + unset req.http.X-Forwarded-Port; + // To prevent the Ibexa Cloud detection in Ibexa DXP from kicking in: + unset req.http.X-Client-IP; + unset req.http.Client-Cdn; + + // Varnish itself only knows the scheme when a TLS terminator hands the connection over + // using the PROXY protocol, in which case server.ip is the address the client connected to. + if (std.port(server.ip) == 443) { + set req.http.X-Forwarded-Proto = "https"; + } else { + unset req.http.X-Forwarded-Proto; + } + } + // 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/docs/varnish/vcl/varnish9.vcl b/docs/varnish/vcl/varnish9.vcl index d3f68466..3725ed0f 100644 --- a/docs/varnish/vcl/varnish9.vcl +++ b/docs/varnish/vcl/varnish9.vcl @@ -21,6 +21,29 @@ 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. See parameters.vcl for the trusted_proxies ACL. + if (client.ip !~ trusted_proxies) { + // Varnish appends client.ip to X-Forwarded-For before vcl_recv is entered, so overwrite + // rather than unset, to keep the real client IP as the only entry. + set req.http.X-Forwarded-For = client.ip; + unset req.http.Forwarded; + unset req.http.X-Forwarded-Host; + unset req.http.X-Forwarded-Prefix; + unset req.http.X-Forwarded-Port; + // To prevent the Ibexa Cloud detection in Ibexa DXP from kicking in: + unset req.http.X-Client-IP; + unset req.http.Client-Cdn; + + // Varnish itself only knows the scheme when a TLS terminator hands the connection over + // using the PROXY protocol, in which case server.ip is the address the client connected to. + if (std.port(server.ip) == 443) { + set req.http.X-Forwarded-Proto = "https"; + } else { + unset req.http.X-Forwarded-Proto; + } + } + // 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/features/setup/setup.feature b/features/setup/setup.feature index e914de26..a59eb61b 100644 --- a/features/setup/setup.feature +++ b/features/setup/setup.feature @@ -70,3 +70,16 @@ Feature: Set system to desired state before tests And I add policies to "Anonymous" | module | function | | user | login | + + @APIUser:admin + Scenario: Set up the system to test filtering of reverse proxy headers + Given I create a "proxyHeadersContentType" content type in "Content" with "proxyHeadersContentType" identifier + | Field Type | Name | Identifier | Required | Searchable | Translatable | + | Text line | Name | name | yes | yes | yes | + And I append configuration to "ibexa.system.default.content_view.full" + """ + proxyHeadersContentType: + template: "@IbexaHttpCache/tests/cache/proxy_headers.html.twig" + match: + Identifier\ContentType: [proxyHeadersContentType] + """ diff --git a/features/varnish/proxyHeaders.feature b/features/varnish/proxyHeaders.feature new file mode 100644 index 00000000..3b994a94 --- /dev/null +++ b/features/varnish/proxyHeaders.feature @@ -0,0 +1,72 @@ +@varnish6 @varnish7 @varnish9 +Feature: As a site administrator I want Varnish to drop reverse proxy headers sent by clients + + # The application trusts X-Forwarded-* as soon as framework.trusted_proxies is configured, so a + # client must not be able to set them. Varnish strips them for every client that is not listed + # in the trusted_proxies ACL - which is every client here, as the ACL deliberately does not + # contain the Docker network. + # + # The rendered page reports the headers the application received, since that cannot be observed + # from the response alone. See src/bundle/Resources/views/tests/cache/proxy_headers.html.twig. + + @admin + Scenario Outline: Client supplied reverse proxy headers never reach the application + Given I create "proxyHeadersContentType" Content items in root in "eng-GB" + | name | + | | + And I am viewing the pages on siteaccess "site" as "" "" + And I set request header "X-Forwarded-Host" to "evil.example" + And I set request header "X-Forwarded-Proto" to "https" + And I set request header "X-Forwarded-Port" to "443" + And I set request header "X-Forwarded-Prefix" to "/admin" + And I set request header "X-Forwarded-For" to "6.6.6.6" + And I set request header "X-Client-IP" to "6.6.6.6" + And I set request header "Client-Cdn" to "fastly" + And I set request header "Forwarded" to "for=6.6.6.6;host=evil.example;proto=https" + When I visit "" on siteaccess "site" + # A cache hit would show the headers of whichever request populated the cache + And response headers contain + | Header | Value | + | x-cache | MISS | + Then I should see "XFHOST:-" + And I should see "XFPROTO:-" + And I should see "XFPREFIX:-" + And I should see "FORWARDED:-" + And I should see "XCLIENTIP:-" + And I should see "CLIENTCDN:-" + # Derived by the VCL from the stripped X-Forwarded-Proto, not taken from the client + And I should see "XFPORT:80" + # X-Forwarded-For is overwritten with the real client IP rather than unset + And I should not see "6.6.6.6" + And I should not see "evil.example" + + Examples: + | user | password | itemName | + | admin | publish | ProxyProbeAdmin | + | anonymous | | ProxyProbeAnonymous | + + # Runs only with the varnish-trusted-proxy overlay applied, where the app container is added to the + # trusted_proxies ACL. Guards against the filtering degrading into a blanket strip, which would + # break every setup with a TLS terminator, load balancer or CDN in front of Varnish. + @admin @trustedProxy + Scenario: Reverse proxy headers from a trusted proxy do reach the application + Given I create "proxyHeadersContentType" Content items in root in "eng-GB" + | name | + | ProxyProbeTrusted | + And I am viewing the pages on siteaccess "site" as "anonymous" "" + And I set request header "X-Forwarded-Host" to "upstream.example" + And I set request header "X-Forwarded-Proto" to "https" + And I set request header "X-Forwarded-Prefix" to "/admin" + And I set request header "X-Client-IP" to "203.0.113.9" + And I set request header "Client-Cdn" to "fastly" + When I visit "ProxyProbeTrusted" on siteaccess "site" + And response headers contain + | Header | Value | + | x-cache | MISS | + Then I should see "XFHOST:upstream.example" + And I should see "XFPROTO:https" + And I should see "XFPREFIX:/admin" + And I should see "XCLIENTIP:203.0.113.9" + And I should see "CLIENTCDN:fastly" + # Derived by the VCL from the trusted X-Forwarded-Proto + And I should see "XFPORT:443" diff --git a/src/bundle/Resources/views/tests/cache/proxy_headers.html.twig b/src/bundle/Resources/views/tests/cache/proxy_headers.html.twig new file mode 100644 index 00000000..ad285894 --- /dev/null +++ b/src/bundle/Resources/views/tests/cache/proxy_headers.html.twig @@ -0,0 +1,22 @@ +{# Renders the reverse proxy headers as the application received them. + + Used by features/varnish/proxyHeaders.feature to assert what Varnish forwarded to the backend, + which cannot be observed from the response alone. Test fixture, not part of the public API. #} + + + + Proxy headers received by the application + + +
+
XFF:{{ app.request.headers.get('x-forwarded-for')|default('-') }}
+
XFHOST:{{ app.request.headers.get('x-forwarded-host')|default('-') }}
+
XFPROTO:{{ app.request.headers.get('x-forwarded-proto')|default('-') }}
+
XFPORT:{{ app.request.headers.get('x-forwarded-port')|default('-') }}
+
XFPREFIX:{{ app.request.headers.get('x-forwarded-prefix')|default('-') }}
+
FORWARDED:{{ app.request.headers.get('forwarded')|default('-') }}
+
XCLIENTIP:{{ app.request.headers.get('x-client-ip')|default('-') }}
+
CLIENTCDN:{{ app.request.headers.get('client-cdn')|default('-') }}
+
+ + diff --git a/src/lib/Behat/Context/ProxyHeaderContext.php b/src/lib/Behat/Context/ProxyHeaderContext.php new file mode 100644 index 00000000..9b8df3d3 --- /dev/null +++ b/src/lib/Behat/Context/ProxyHeaderContext.php @@ -0,0 +1,28 @@ +getSession()->setRequestHeader($name, $value); + } +} diff --git a/tests/varnish/Dockerfile b/tests/varnish/Dockerfile new file mode 100644 index 00000000..aa81002f --- /dev/null +++ b/tests/varnish/Dockerfile @@ -0,0 +1,20 @@ +# Image for running the varnishtest suite in tests/varnish/. +# +# Mirrors doc/docker/Dockerfile-varnish9 from ibexa/docker: the VCLs import the xkey vmod, which the +# official image does not ship. + +FROM varnish:7.4 + +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" && \ + apt-get update && \ + apt-get -y --no-install-recommends install "${vmod_deps[@]}" /pkgs/*.deb && \ + install-vmod https://github.com/varnish/varnish-modules/releases/download/0.23.0/varnish-modules-0.23.0.tar.gz && \ + rm -rf /var/lib/apt/lists/* + +USER varnish diff --git a/tests/varnish/Dockerfile-9 b/tests/varnish/Dockerfile-9 new file mode 100644 index 00000000..772882c1 --- /dev/null +++ b/tests/varnish/Dockerfile-9 @@ -0,0 +1,21 @@ +# Image for running the varnishtest suite in tests/varnish/ against varnish9.vcl. +# +# Mirrors doc/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 00000000..375968c5 --- /dev/null +++ b/tests/varnish/README.md @@ -0,0 +1,40 @@ +Varnish VCL tests +================= + +`varnishtest` cases for the VCLs in [`docs/varnish/vcl`](../../docs/varnish/vcl). Each case loads the +real VCL file into a real Varnish, with a fixture standing in for `parameters.vcl`, and asserts on +the request the **backend** receives — which is where the reverse proxy header filtering in +`vcl_recv` can be observed. + +Cases +----- + +| Case | Fixture | Asserts | +|---|---|---| +| `untrusted.vtc` | `fixtures/parameters-untrusted.vcl` (client not in `trusted_proxies`) | every client supplied `X-Forwarded-*` / `Forwarded` / `X-Client-IP` / `Client-Cdn` is gone, `X-Forwarded-For` holds only the real client IP | +| `trusted.vtc` | `fixtures/parameters-trusted.vcl` (client in `trusted_proxies`) | the same headers are passed through untouched — guards against the filtering degrading into a blanket strip, which would break every setup with a TLS terminator or CDN in front of Varnish | + +Running them +------------ + +```bash +docker build -t ibexa-varnishtest:7 tests/varnish +docker build -t ibexa-varnishtest:9 -f tests/varnish/Dockerfile-9 tests/varnish +IMAGE_VARNISH7=ibexa-varnishtest:7 IMAGE_VARNISH9=ibexa-varnishtest:9 tests/varnish/run.sh varnish7.vcl varnish9.vcl +``` + +`run.sh` with no arguments runs every VCL. `varnish5.vcl` and `varnish6.vcl` target Varnish 6.0LTS +and do not compile on 7.x — their `vcl_hit` returns `miss`, which 7.x rejects — so they need a +Varnish 6.0 image carrying xkey, which you can point at with `IMAGE_VARNISH6`. CI only runs +`varnish7.vcl` and `varnish9.vcl`; the 6.0 line is covered end to end by the `varnish6` +browser-test job, which builds `varnish5.vcl`. + +`run.sh` also asserts that `varnish5.vcl` and `varnish6.vcl` stay identical apart from their +two-line header comment, since they are documented as 1:1 copies. + +Notes +----- + +- The backend listens on a fixed port (9081) so that the fixtures, which are static files, can + point a `backend` at it. +- Varnish rejects an unused ACL, so a fixture must define exactly the ACLs its VCL references. diff --git a/tests/varnish/fixtures/parameters-trusted.vcl b/tests/varnish/fixtures/parameters-trusted.vcl new file mode 100644 index 00000000..72ccad09 --- /dev/null +++ b/tests/varnish/fixtures/parameters-trusted.vcl @@ -0,0 +1,23 @@ +// Test fixture standing in for parameters.vcl: the client IS a trusted proxy. +// +// The backend is on a fixed port so that it can be referenced from a static file, see the +// "server s1 -listen" line in the .vtc files. + +backend ezplatform { + .host = "127.0.0.1"; + .port = "9081"; +} + +acl invalidators { + "127.0.0.1"; +} + +acl debuggers { + "127.0.0.1"; +} + +// Contains 127.0.0.1, so that varnishtest's client is treated as a reverse proxy in front of +// Varnish, the way a TLS terminator or CDN would be. +acl trusted_proxies { + "127.0.0.1"; +} diff --git a/tests/varnish/fixtures/parameters-untrusted.vcl b/tests/varnish/fixtures/parameters-untrusted.vcl new file mode 100644 index 00000000..bcfa5906 --- /dev/null +++ b/tests/varnish/fixtures/parameters-untrusted.vcl @@ -0,0 +1,23 @@ +// Test fixture standing in for parameters.vcl: the client is NOT a trusted proxy. +// +// The backend is on a fixed port so that it can be referenced from a static file, see the +// "server s1 -listen" line in the .vtc files. + +backend ezplatform { + .host = "127.0.0.1"; + .port = "9081"; +} + +acl invalidators { + "127.0.0.1"; +} + +acl debuggers { + "127.0.0.1"; +} + +// Deliberately does not contain 127.0.0.1, so that varnishtest's client is treated as an +// ordinary client rather than as a reverse proxy. +acl trusted_proxies { + "192.0.2.1"; +} diff --git a/tests/varnish/run.sh b/tests/varnish/run.sh new file mode 100755 index 00000000..165612f3 --- /dev/null +++ b/tests/varnish/run.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# +# Runs the varnishtest suite for the VCLs in docs/varnish/vcl/. +# +# Each case is run against a Varnish that has loaded the real VCL file, with a fixture standing in +# for parameters.vcl, so what is asserted is the shipped configuration rather than a copy of it. +# +# Usage: +# tests/varnish/run.sh # all VCLs, using Docker +# tests/varnish/run.sh varnish7.vcl # a single VCL +# +# varnish5.vcl and varnish6.vcl target Varnish 6.0LTS and do not compile on 7.x (vcl_hit returns +# miss, which 7.x rejects), so they need a different image than varnish7.vcl. varnish9.vcl needs a +# third, built from Dockerfile-9. All images must carry the xkey vmod, since the VCLs import it. + +set -euo pipefail + +TESTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +VCLDIR="$(cd "$TESTDIR/../../docs/varnish/vcl" && pwd)" + +IMAGE_VARNISH6="${IMAGE_VARNISH6:-ibexa-varnishtest:6}" +IMAGE_VARNISH7="${IMAGE_VARNISH7:-ibexa-varnishtest:7}" +IMAGE_VARNISH9="${IMAGE_VARNISH9:-ibexa-varnishtest:9}" + +image_for() { + local vcl="$1" + + case "$vcl" in + varnish7.vcl) echo "$IMAGE_VARNISH7" ;; + varnish9.vcl) echo "$IMAGE_VARNISH9" ;; + *) echo "$IMAGE_VARNISH6" ;; + esac +} + +run_case() { + local vcl="$1" case_name="$2" image + image="$(image_for "$vcl")" + + echo "==> $vcl / $case_name (${image})" + docker run --rm \ + -v "$VCLDIR/$vcl:/etc/varnish/default.vcl:ro" \ + -v "$TESTDIR/fixtures/parameters-${case_name}.vcl:/etc/varnish/parameters.vcl:ro" \ + -v "$TESTDIR/${case_name}.vtc:/${case_name}.vtc:ro" \ + --entrypoint varnishtest "$image" "/${case_name}.vtc" +} + +# varnish5.vcl is kept as a 1:1 copy of varnish6.vcl for BC. Guard that, so the two cannot drift. +check_copies_in_sync() { + if ! diff <(tail -n +3 "$VCLDIR/varnish5.vcl") <(tail -n +3 "$VCLDIR/varnish6.vcl") > /dev/null; then + echo "FAIL: varnish5.vcl and varnish6.vcl differ beyond their two-line header comment." >&2 + echo " They are documented as 1:1 copies - apply every change to both." >&2 + return 1 + fi + echo "==> varnish5.vcl and varnish6.vcl are in sync" +} + +vcls=("${@:-varnish5.vcl varnish6.vcl varnish7.vcl varnish9.vcl}") +# shellcheck disable=SC2206 +vcls=(${vcls[@]}) + +check_copies_in_sync + +for vcl in "${vcls[@]}"; do + for case_name in untrusted trusted; do + run_case "$vcl" "$case_name" + done +done + +echo "All varnishtest cases passed." diff --git a/tests/varnish/trusted.vtc b/tests/varnish/trusted.vtc new file mode 100644 index 00000000..13dc9728 --- /dev/null +++ b/tests/varnish/trusted.vtc @@ -0,0 +1,41 @@ +varnishtest "Proxy headers from a trusted proxy are passed on to the backend untouched" + +# Guards against the filtering turning into a blanket strip, which would break every setup with a +# TLS terminator, load balancer or CDN in front of Varnish. +# +# Run with fixtures/parameters-trusted.vcl mounted as /etc/varnish/parameters.vcl, and the VCL +# under test as /etc/varnish/default.vcl. See run.sh. + +server s1 -listen "127.0.0.1:9081" { + rxreq + + expect req.http.X-Forwarded-Host == "upstream.example" + expect req.http.X-Forwarded-Prefix == "/admin" + expect req.http.X-Forwarded-Proto == "https" + expect req.http.Forwarded == "for=203.0.113.9;host=upstream.example;proto=https" + expect req.http.X-Client-IP == "203.0.113.9" + expect req.http.Client-Cdn == "fastly" + + # Left as Varnish built it: what the proxy sent, with the proxy's own address appended. + expect req.http.X-Forwarded-For == "203.0.113.9, 127.0.0.1" + + # Derived by the VCL from the trusted X-Forwarded-Proto. + expect req.http.X-Forwarded-Port == "443" + + txresp +} -start + +varnish v1 -arg "-f /etc/varnish/default.vcl" -start + +client c1 { + txreq -url "/trusted" \ + -hdr "X-Forwarded-Host: upstream.example" \ + -hdr "X-Forwarded-Proto: https" \ + -hdr "X-Forwarded-Prefix: /admin" \ + -hdr "X-Forwarded-For: 203.0.113.9" \ + -hdr "X-Client-IP: 203.0.113.9" \ + -hdr "Client-Cdn: fastly" \ + -hdr "Forwarded: for=203.0.113.9;host=upstream.example;proto=https" + rxresp + expect resp.status == 200 +} -run diff --git a/tests/varnish/untrusted.vtc b/tests/varnish/untrusted.vtc new file mode 100644 index 00000000..04d769a8 --- /dev/null +++ b/tests/varnish/untrusted.vtc @@ -0,0 +1,41 @@ +varnishtest "Proxy headers from a client that is not a trusted proxy never reach the backend" + +# Run with fixtures/parameters-untrusted.vcl mounted as /etc/varnish/parameters.vcl, and the VCL +# under test as /etc/varnish/default.vcl. See run.sh. + +server s1 -listen "127.0.0.1:9081" { + rxreq + + # Everything the client made up is gone. + expect req.http.X-Forwarded-Host == + expect req.http.X-Forwarded-Prefix == + expect req.http.X-Forwarded-Proto == + expect req.http.Forwarded == + expect req.http.X-Client-IP == + expect req.http.Client-Cdn == + + # Varnish appends client.ip to X-Forwarded-For before vcl_recv is entered, so the header is + # overwritten rather than unset: the real client IP is the only entry left. + expect req.http.X-Forwarded-For == "127.0.0.1" + + # Derived by the VCL from the (now absent) X-Forwarded-Proto, not taken from the client. + expect req.http.X-Forwarded-Port == "80" + + txresp +} -start + +varnish v1 -arg "-f /etc/varnish/default.vcl" -start + +client c1 { + txreq -url "/untrusted" \ + -hdr "X-Forwarded-Host: evil.example" \ + -hdr "X-Forwarded-Proto: https" \ + -hdr "X-Forwarded-Port: 443" \ + -hdr "X-Forwarded-Prefix: /admin" \ + -hdr "X-Forwarded-For: 6.6.6.6" \ + -hdr "X-Client-IP: 6.6.6.6" \ + -hdr "Client-Cdn: fastly" \ + -hdr "Forwarded: for=6.6.6.6;host=evil.example;proto=https" + rxresp + expect resp.status == 200 +} -run From 62e1c2607c45b6f22e2277563c2f8792c018a8b0 Mon Sep 17 00:00:00 2001 From: Vidar Langseid Date: Thu, 29 Oct 2026 11:42:39 +0100 Subject: [PATCH 2/3] Renamed the varnishtest fixture backend to ibexa to match the 5.0 VCLs --- tests/varnish/fixtures/parameters-trusted.vcl | 2 +- tests/varnish/fixtures/parameters-untrusted.vcl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/varnish/fixtures/parameters-trusted.vcl b/tests/varnish/fixtures/parameters-trusted.vcl index 72ccad09..999ee9de 100644 --- a/tests/varnish/fixtures/parameters-trusted.vcl +++ b/tests/varnish/fixtures/parameters-trusted.vcl @@ -3,7 +3,7 @@ // The backend is on a fixed port so that it can be referenced from a static file, see the // "server s1 -listen" line in the .vtc files. -backend ezplatform { +backend ibexa { .host = "127.0.0.1"; .port = "9081"; } diff --git a/tests/varnish/fixtures/parameters-untrusted.vcl b/tests/varnish/fixtures/parameters-untrusted.vcl index bcfa5906..d57cb9d5 100644 --- a/tests/varnish/fixtures/parameters-untrusted.vcl +++ b/tests/varnish/fixtures/parameters-untrusted.vcl @@ -3,7 +3,7 @@ // The backend is on a fixed port so that it can be referenced from a static file, see the // "server s1 -listen" line in the .vtc files. -backend ezplatform { +backend ibexa { .host = "127.0.0.1"; .port = "9081"; } From 65e806f724b22f0f6d332fa24a2a314b715e013a Mon Sep 17 00:00:00 2001 From: Vidar Langseid Date: Thu, 29 Oct 2026 15:16:58 +0100 Subject: [PATCH 3/3] IBX-11181: Fixed ibexa packages x-dev versions constraints (#111) For more details see https://ibexa.atlassian.net/browse/IBX-11181 and https://github.com/ibexa/http-cache/pull/111 Key changes: * Fixed the `ibexa/rest`, `ibexa/behat` and `ibexa/doctrine-schema` version constraints in `composer.json` to use `~4.6.x-dev`, aligning them with the other 4.6 Ibexa packages. * Removed the `ibexa/ci-scripts` dev dependency from `composer.json`. --------- Co-Authored-By: Andrew Longosz --- composer.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 5b543277..b5f8615c 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "require": { "php": "^7.4 || ^8.0", "ibexa/core": "~4.6.x-dev", - "ibexa/rest": "~4.6.0@dev", + "ibexa/rest": "~4.6.x-dev", "friendsofsymfony/http-cache-bundle": "^2.8", "friendsofsymfony/http-cache": "^2.9", "symfony/http-foundation": "^5.0", @@ -30,9 +30,8 @@ "toflar/psr6-symfony-http-cache-store": "^2.2 || ^3.0" }, "require-dev": { - "ibexa/behat": "~4.6.0", - "ibexa/ci-scripts": "^0.2@dev", - "ibexa/doctrine-schema": "~4.6.0@dev", + "ibexa/behat": "~4.6.x-dev", + "ibexa/doctrine-schema": "~4.6.x-dev", "ibexa/doctrine-migrations": "~4.6.x-dev", "phpunit/phpunit": "^9.6", "matthiasnoback/symfony-dependency-injection-test": "^4.3",