Skip to content

Commit

Permalink
Fix install check when a reverse proxy is used
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbestle authored and bastianallgeier committed Apr 27, 2020
1 parent 91e6d07 commit 790ffbc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
28 changes: 23 additions & 5 deletions src/Cms/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,40 @@ public function isInstalled(): bool
*/
public function isLocal(): bool
{
$server = $this->app->server();
$host = $server->host();
$server = $this->app->server();
$visitor = $this->app->visitor();
$host = $server->host();

if ($host === 'localhost') {
return true;
}

if (in_array($server->address(), ['::1', '127.0.0.1', '0.0.0.0']) === true) {
if (Str::endsWith($host, '.local') === true) {
return true;
}

if (Str::endsWith($host, '.local') === true) {
if (Str::endsWith($host, '.test') === true) {
return true;
}

if (Str::endsWith($host, '.test') === true) {
if (in_array($visitor->ip(), ['::1', '127.0.0.1']) === true) {
// ensure that there is no reverse proxy in between

if (
isset($_SERVER['HTTP_X_FORWARDED_FOR']) === true &&
in_array($_SERVER['HTTP_X_FORWARDED_FOR'], ['::1', '127.0.0.1']) === false
) {
return false;
}

if (
isset($_SERVER['HTTP_CLIENT_IP']) === true &&
in_array($_SERVER['HTTP_CLIENT_IP'], ['::1', '127.0.0.1']) === false
) {
return false;
}

// no reverse proxy or the real client also comes from localhost
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Visitor
*/
public function __construct(array $arguments = [])
{
$this->ip($arguments['ip'] ?? getenv('REMOTE_ADDR'));
$this->ip($arguments['ip'] ?? $_SERVER['REMOTE_ADDR'] ?? '');
$this->userAgent($arguments['userAgent'] ?? $_SERVER['HTTP_USER_AGENT'] ?? '');
$this->acceptedLanguage($arguments['acceptedLanguage'] ?? $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '');
$this->acceptedMimeType($arguments['acceptedMimeType'] ?? $_SERVER['HTTP_ACCEPT'] ?? '');
Expand Down
37 changes: 19 additions & 18 deletions tests/Cms/System/SystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,34 @@ public function testIsLocalWithServerNames($name, $expected)

$system = new System($this->app);
$this->assertEquals($expected, $system->isLocal());

// reset SERVER_NAME
$_SERVER['SERVER_NAME'] = null;
}

public function serverAddressProvider()
public function clientAddressProvider()
{
return [
['127.0.0.1', true],
['::1', true],
['0.0.0.0', true],
['1.2.3.4', false],
['127.0.0.1', '127.0.0.1', true],
['::1', '::1', true],
['127.0.0.1', '::1', true],
['::1', '127.0.0.1', true],
['1.2.3.4', '127.0.0.1', false],
['127.0.0.1', '1.2.3.4', false],
];
}

/**
* @dataProvider serverAddressProvider
* @dataProvider clientAddressProvider
*/
public function testIsLocalWithServerAddresses($address, $expected)
public function testIsLocalWithClientAddresses(string $address, string $forwardedAddress, bool $expected)
{
$_SERVER['SERVER_ADDR'] = $address;

$system = new System($this->app);
$this->assertEquals($expected, $system->isLocal());

// reset SERVER_ADDR
$_SERVER['SERVER_ADDR'] = null;
$_SERVER['REMOTE_ADDR'] = $address;
$_SERVER['HTTP_X_FORWARDED_FOR'] = $forwardedAddress;
$this->assertSame($expected, $system->isLocal());

unset($_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['HTTP_CLIENT_IP'] = $forwardedAddress;
$this->assertSame($expected, $system->isLocal());
}

public function indexUrlProvider()
Expand Down Expand Up @@ -172,7 +173,7 @@ public function testLicenseUrl($url, $expected)

public function testIsInstallableOnLocalhost()
{
$_SERVER['SERVER_NAME'] = 'localhost';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';

$system = new System($this->app);

Expand All @@ -181,7 +182,7 @@ public function testIsInstallableOnLocalhost()

public function testIsInstallableOnPublicServer()
{
$_SERVER['SERVER_NAME'] = 'getkirby.com';
$_SERVER['REMOTE_ADDR'] = '1.2.3.4';

$system = new System($this->app);

Expand All @@ -190,7 +191,7 @@ public function testIsInstallableOnPublicServer()

public function testIsInstallableOnPublicServerWithOverride()
{
$_SERVER['SERVER_NAME'] = 'getkirby.com';
$_SERVER['REMOTE_ADDR'] = '1.2.3.4';

$app = $this->app->clone([
'options' => [
Expand Down

0 comments on commit 790ffbc

Please sign in to comment.