Repository navigation
IBX-11181: Filtered client-supplied X-Forwarded-* headers in Varnish VCL #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
887dab8
64a4d28
77bfc56
4f6a7bd
26e2f94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| services: | ||
| _defaults: | ||
| autowire: true | ||
| autoconfigure: true | ||
|
|
||
| Ibexa\Bundle\Cloud\EventSubscriber\TrustedHeaderClientIpEventSubscriber: ~ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Tests\Bundle\Cloud\EventSubscriber; | ||
|
|
||
| use Ibexa\Bundle\Cloud\EventSubscriber\TrustedHeaderClientIpEventSubscriber; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\EventDispatcher\EventDispatcher; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
| use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
| use Symfony\Component\HttpKernel\KernelEvents; | ||
| use Symfony\Component\HttpKernel\KernelInterface; | ||
|
|
||
| /** | ||
| * @covers \Ibexa\Bundle\Cloud\EventSubscriber\TrustedHeaderClientIpEventSubscriber | ||
| */ | ||
| final class TrustedHeaderClientIpEventSubscriberTest extends TestCase | ||
| { | ||
| private const string REAL_CLIENT_IP = '98.76.123.234'; | ||
|
|
||
| // framework.trusted_headers default | ||
| private const int TRUSTED_HEADER_SET = Request::HEADER_X_FORWARDED_FOR | ||
| | Request::HEADER_X_FORWARDED_PORT | ||
| | Request::HEADER_X_FORWARDED_PROTO; | ||
|
|
||
| /** @var string[] */ | ||
| private array $originalTrustedProxies; | ||
|
|
||
| private int $originalTrustedHeaderSet; | ||
|
|
||
| private mixed $originalRemoteAddr; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->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<string, array{bool, array<string, string>, array<string, mixed>}> | ||
| */ | ||
| 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<string, string> $headers | ||
| * @param array<string, mixed> $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<string, string> $headers | ||
| * @param array<string, mixed> $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(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Image for running the varnishtest suite in tests/varnish/. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @micszo I suggest we change the structure of the varnish image in order to make it more general and re-usable. However, that would imply a BC break so not sure if we can do it before 6.0, or if this docker files are "supported" and BC guaranteed . What I suggest is: In
It is a lot of things to do for just removing some duplicated code though ( but keep in mind that it is not only here in BC breaks are:
|
||
| # | ||
| # 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:bookworm-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 \ | ||
| 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~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/ \ | ||
| && 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/* | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Uh oh!
There was an error while loading. Please reload this page.