Fix getPort() returning 80 instead of 443 when X-FORWARDED-PROTO is set to https

This commit is contained in:
Philipp Strube 2013-04-28 18:01:03 +02:00 committed by Fabien Potencier
parent 02e9b19f29
commit 2a531d78be
2 changed files with 43 additions and 2 deletions

View File

@ -793,8 +793,14 @@ class Request
*/
public function getPort()
{
if (self::$trustProxy && self::$trustedHeaders[self::HEADER_CLIENT_PORT] && $port = $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PORT])) {
return $port;
if (self::$trustProxy) {
if (self::$trustedHeaders[self::HEADER_CLIENT_PORT] && $port = $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PORT])) {
return $port;
}
if (self::$trustedHeaders[self::HEADER_CLIENT_PROTO] && 'https' === $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_PROTO], 'http')) {
return 443;
}
}
return $this->server->get('SERVER_PORT');

View File

@ -583,6 +583,41 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->stopTrustingProxyData();
}
public function testGetPort()
{
$request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
'HTTP_X_FORWARDED_PROTO' => 'https',
'HTTP_X_FORWARDED_PORT' => '443'
));
$port = $request->getPort();
$this->assertEquals(80, $port, 'Without trusted proxies FORWARDED_PROTO and FORWARDED_PORT are ignored.');
Request::setTrustedProxies(array('1.1.1.1'));
$request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
'HTTP_X_FORWARDED_PROTO' => 'https',
'HTTP_X_FORWARDED_PORT' => '8443'
));
$port = $request->getPort();
$this->assertEquals(8443, $port, 'With PROTO and PORT set PORT takes precedence.');
$request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
'HTTP_X_FORWARDED_PROTO' => 'https'
));
$port = $request->getPort();
$this->assertEquals(443, $port, 'With only PROTO set getPort() defaults to 443.');
$request = Request::create('http://example.com', 'GET', array(), array(), array(), array(
'HTTP_X_FORWARDED_PROTO' => 'http'
));
$port = $request->getPort();
$this->assertEquals(80, $port, 'If X_FORWARDED_PROTO is set to http return 80.');
Request::setTrustedProxies(array());
}
/**
* @expectedException RuntimeException
*/