From 1c6eae90173b2b323a8e4633cda5f37eafa5a1d4 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Tue, 24 Feb 2015 12:24:55 +0100 Subject: [PATCH 1/2] Get the real protocol behind several proxies X-Forwarded-Proto contains a list of protocols if ownCloud is behind multiple reverse proxies. This is a revival of https://github.com/owncloud/core/pull/11157 using the new IRequest public API. --- lib/private/appframework/http/request.php | 12 +++++++++--- tests/lib/appframework/http/RequestTest.php | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/private/appframework/http/request.php b/lib/private/appframework/http/request.php index 959ea27328..f415d6faf2 100644 --- a/lib/private/appframework/http/request.php +++ b/lib/private/appframework/http/request.php @@ -480,8 +480,8 @@ class Request implements \ArrayAccess, \Countable, IRequest { } /** - * Returns the server protocol. It respects reverse proxy servers and load - * balancers. + * Returns the server protocol. It respects one or more reverse proxies servers + * and load balancers * @return string Server protocol (http or https) */ public function getServerProtocol() { @@ -491,7 +491,13 @@ class Request implements \ArrayAccess, \Countable, IRequest { } if (isset($this->server['HTTP_X_FORWARDED_PROTO'])) { - $proto = strtolower($this->server['HTTP_X_FORWARDED_PROTO']); + if (strpos($this->server['HTTP_X_FORWARDED_PROTO'], ',') !== false) { + $parts = explode(',', $this->server['HTTP_X_FORWARDED_PROTO']); + $proto = strtolower(trim(current($parts))); + } else { + $proto = strtolower($this->server['HTTP_X_FORWARDED_PROTO']); + } + // Verify that the protocol is always HTTP or HTTPS // default to http if an invalid value is provided return $proto === 'https' ? 'https' : 'http'; diff --git a/tests/lib/appframework/http/RequestTest.php b/tests/lib/appframework/http/RequestTest.php index 3185a0093c..282d13a339 100644 --- a/tests/lib/appframework/http/RequestTest.php +++ b/tests/lib/appframework/http/RequestTest.php @@ -593,6 +593,27 @@ class RequestTest extends \Test\TestCase { $this->assertSame('http', $request->getServerProtocol()); } + public function testGetServerProtocolBehindLoadBalancers() { + $this->config + ->expects($this->once()) + ->method('getSystemValue') + ->with('overwriteprotocol') + ->will($this->returnValue('')); + + $request = new Request( + [ + 'server' => [ + 'HTTP_X_FORWARDED_PROTO' => 'https,http,http' + ], + ], + $this->secureRandom, + $this->config, + $this->stream + ); + + $this->assertSame('https', $request->getServerProtocol()); + } + /** * @dataProvider userAgentProvider * @param string $testAgent From 9adcd15cb35ef4f02db073116dcce78c735cd292 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Tue, 24 Feb 2015 13:37:34 +0100 Subject: [PATCH 2/2] Use [0] instead of current as HHVM might have problems with that --- lib/private/appframework/http/request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/appframework/http/request.php b/lib/private/appframework/http/request.php index f415d6faf2..7cbeaa0480 100644 --- a/lib/private/appframework/http/request.php +++ b/lib/private/appframework/http/request.php @@ -493,7 +493,7 @@ class Request implements \ArrayAccess, \Countable, IRequest { if (isset($this->server['HTTP_X_FORWARDED_PROTO'])) { if (strpos($this->server['HTTP_X_FORWARDED_PROTO'], ',') !== false) { $parts = explode(',', $this->server['HTTP_X_FORWARDED_PROTO']); - $proto = strtolower(trim(current($parts))); + $proto = strtolower(trim($parts[0])); } else { $proto = strtolower($this->server['HTTP_X_FORWARDED_PROTO']); }