Skip to content

Commit

Permalink
Merge branch '2.1' into 2.2
Browse files Browse the repository at this point in the history
* 2.1:
  Fix getPort() returning 80 instead of 443 when X-FORWARDED-PROTO is set to https
  • Loading branch information
fabpot committed Apr 30, 2013
2 parents d1cad7e + 0dacd56 commit 01ff076
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -837,8 +837,14 @@ public function getScheme()
*/
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');
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Expand Up @@ -678,6 +678,41 @@ public function testGetHost()
$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
*/
Expand Down

0 comments on commit 01ff076

Please sign in to comment.