From b3949843f94f3f5bafa4184db4e210f1243fc58f Mon Sep 17 00:00:00 2001 From: matlec Date: Fri, 5 Sep 2025 12:10:29 +0200 Subject: [PATCH] [Security] Fix `HttpUtils::createRequest()` when the base request is forwarded --- src/Symfony/Component/Security/Http/HttpUtils.php | 6 ++++++ .../Component/Security/Http/Tests/HttpUtilsTest.php | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Symfony/Component/Security/Http/HttpUtils.php b/src/Symfony/Component/Security/Http/HttpUtils.php index 0163bb18a95ec..af0c732fd03d6 100644 --- a/src/Symfony/Component/Security/Http/HttpUtils.php +++ b/src/Symfony/Component/Security/Http/HttpUtils.php @@ -70,7 +70,13 @@ public function createRedirectResponse(Request $request, string $path, int $stat */ public function createRequest(Request $request, string $path): Request { + if ($trustedProxies = Request::getTrustedProxies()) { + Request::setTrustedProxies([], Request::getTrustedHeaderSet()); + } $newRequest = Request::create($this->generateUri($request, $path), 'get', [], $request->cookies->all(), [], $request->server->all()); + if ($trustedProxies) { + Request::setTrustedProxies($trustedProxies, Request::getTrustedHeaderSet()); + } static $setSession; diff --git a/src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php b/src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php index e165a4df52c4d..c042b02c9ad5f 100644 --- a/src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php +++ b/src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php @@ -233,6 +233,16 @@ public static function provideSecurityRequestAttributes() ]; } + public function testCreateRequestHandlesTrustedHeaders() + { + Request::setTrustedProxies(['127.0.0.1'], Request::HEADER_X_FORWARDED_PREFIX); + + $this->assertSame( + 'http://localhost/foo/', + (new HttpUtils())->createRequest(Request::create('/', server: ['HTTP_X_FORWARDED_PREFIX' => '/foo']), '/')->getUri(), + ); + } + public function testCheckRequestPath() { $utils = new HttpUtils($this->getUrlGenerator());