Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/Symfony/Component/Security/Http/HttpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,21 @@ 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());

$context = $this->urlGenerator?->getContext();
if ($baseUrl = $context?->getBaseUrl()) {
$context->setBaseUrl('');
}

try {
$newRequest = Request::create($this->generateUri($request, $path), 'get', [], $request->cookies->all(), [], $request->server->all());
} finally {
if ($trustedProxies) {
Request::setTrustedProxies($trustedProxies, Request::getTrustedHeaderSet());
}
if ($baseUrl) {
$context->setBaseUrl($baseUrl);
}
}

static $setSession;
Expand Down
23 changes: 22 additions & 1 deletion src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Security\Http\SecurityRequestAttributes;

Expand Down Expand Up @@ -233,7 +236,7 @@ public static function provideSecurityRequestAttributes()
];
}

public function testCreateRequestHandlesTrustedHeaders()
public function testCreateRequestFromPathHandlesTrustedHeaders()
{
Request::setTrustedProxies(['127.0.0.1'], Request::HEADER_X_FORWARDED_PREFIX);

Expand All @@ -243,6 +246,24 @@ public function testCreateRequestHandlesTrustedHeaders()
);
}

public function testCreateRequestFromRouteHandlesTrustedHeaders()
{
Request::setTrustedProxies(['127.0.0.1'], Request::HEADER_X_FORWARDED_PREFIX);

$request = Request::create('/', server: ['HTTP_X_FORWARDED_PREFIX' => '/foo']);

$urlGenerator = new UrlGenerator(
$routeCollection = new RouteCollection(),
(new RequestContext())->fromRequest($request),
);
$routeCollection->add('root', new Route('/'));

$this->assertSame(
'http://localhost/foo/',
(new HttpUtils($urlGenerator))->createRequest($request, 'root')->getUri(),
);
}

public function testCheckRequestPath()
{
$utils = new HttpUtils($this->getUrlGenerator());
Expand Down