Skip to content

Commit

Permalink
security #cve-2021-41267 [HttpKernel] Fix missing extra trusted heade…
Browse files Browse the repository at this point in the history
…r in sub-request (jderusse)

This PR was merged into the 5.3 branch.
  • Loading branch information
nicolas-grekas committed Nov 24, 2021
2 parents 87baa86 + c334006 commit 7e7cf0c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions HttpCache/SubRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static function handle(HttpKernelInterface $kernel, Request $request, int
'X_FORWARDED_HOST' => $trustedHeaderSet & Request::HEADER_X_FORWARDED_HOST,
'X_FORWARDED_PROTO' => $trustedHeaderSet & Request::HEADER_X_FORWARDED_PROTO,
'X_FORWARDED_PORT' => $trustedHeaderSet & Request::HEADER_X_FORWARDED_PORT,
'X_FORWARDED_PREFIX' => $trustedHeaderSet & Request::HEADER_X_FORWARDED_PREFIX,
];
foreach (array_filter($trustedHeaders) as $name => $key) {
$request->headers->remove($name);
Expand Down
6 changes: 6 additions & 0 deletions Tests/HttpCache/SubRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ public function testTrustedHeadersAreKept()
$request->headers->set('X-Forwarded-Host', 'Good');
$request->headers->set('X-Forwarded-Port', '1234');
$request->headers->set('X-Forwarded-Proto', 'https');
$request->headers->set('X-Forwarded-Prefix', '/admin');

$kernel = new TestSubRequestHandlerKernel(function ($request, $type, $catch) {
$this->assertSame('127.0.0.1', $request->server->get('REMOTE_ADDR'));
$this->assertSame('10.0.0.2', $request->getClientIp());
$this->assertSame('Good', $request->headers->get('X-Forwarded-Host'));
$this->assertSame('1234', $request->headers->get('X-Forwarded-Port'));
$this->assertSame('https', $request->headers->get('X-Forwarded-Proto'));
$this->assertSame('/admin', $request->headers->get('X-Forwarded-Prefix'));
});

SubRequestHandler::handle($kernel, $request, HttpKernelInterface::MAIN_REQUEST, true);
Expand All @@ -64,6 +66,7 @@ public function testUntrustedHeadersAreRemoved()
$request->headers->set('X-Forwarded-Host', 'Evil');
$request->headers->set('X-Forwarded-Port', '1234');
$request->headers->set('X-Forwarded-Proto', 'http');
$request->headers->set('X-Forwarded-Prefix', '/admin');
$request->headers->set('Forwarded', 'Evil2');

$kernel = new TestSubRequestHandlerKernel(function ($request, $type, $catch) {
Expand All @@ -72,6 +75,7 @@ public function testUntrustedHeadersAreRemoved()
$this->assertFalse($request->headers->has('X-Forwarded-Host'));
$this->assertFalse($request->headers->has('X-Forwarded-Port'));
$this->assertFalse($request->headers->has('X-Forwarded-Proto'));
$this->assertFalse($request->headers->has('X-Forwarded-Prefix'));
$this->assertSame('for="10.0.0.1";host="localhost";proto=http', $request->headers->get('Forwarded'));
});

Expand Down Expand Up @@ -112,12 +116,14 @@ public function testTrustedXForwardedForHeader()
$request->headers->set('X-Forwarded-For', '10.0.0.2');
$request->headers->set('X-Forwarded-Host', 'foo.bar');
$request->headers->set('X-Forwarded-Proto', 'https');
$request->headers->set('X-Forwarded-Prefix', '/admin');

$kernel = new TestSubRequestHandlerKernel(function ($request, $type, $catch) {
$this->assertSame('127.0.0.1', $request->server->get('REMOTE_ADDR'));
$this->assertSame('10.0.0.2', $request->getClientIp());
$this->assertSame('foo.bar', $request->getHttpHost());
$this->assertSame('https', $request->getScheme());
$this->assertSame('/admin', $request->getBaseUrl());
});

SubRequestHandler::handle($kernel, $request, HttpKernelInterface::MAIN_REQUEST, true);
Expand Down

0 comments on commit 7e7cf0c

Please sign in to comment.