Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure trusted_proxies is an array #29522

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions apps/settings/lib/Controller/CheckSetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,16 +330,20 @@ private function isPhpSupported(): array {
*
* @return bool
*/
private function forwardedForHeadersWorking() {
private function forwardedForHeadersWorking(): bool {
$trustedProxies = $this->config->getSystemValue('trusted_proxies', []);
$remoteAddress = $this->request->getHeader('REMOTE_ADDR');

if (empty($trustedProxies) && $this->request->getHeader('X-Forwarded-Host') !== '') {
return false;
}

if (\is_array($trustedProxies) && \in_array($remoteAddress, $trustedProxies, true) && $remoteAddress !== '127.0.0.1') {
return $remoteAddress !== $this->request->getRemoteAddress();
if (\is_array($trustedProxies)) {
if (\in_array($remoteAddress, $trustedProxies, true) && $remoteAddress !== '127.0.0.1') {
return $remoteAddress !== $this->request->getRemoteAddress();
}
} else {
return false;
}

// either not enabled or working correctly
Expand Down
79 changes: 56 additions & 23 deletions apps/settings/tests/Controller/CheckSetupControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
use OCP\ITempManager;
use OCP\IURLGenerator;
use OCP\Lock\ILockingProvider;
use OCP\Notification\IManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -102,6 +103,8 @@ class CheckSetupControllerTest extends TestCase {
private $connection;
/** @var ITempManager|\PHPUnit\Framework\MockObject\MockObject */
private $tempManager;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
private $notificationManager;

/**
* Holds a list of directories created during tests.
Expand Down Expand Up @@ -145,6 +148,7 @@ protected function setUp(): void {
$this->connection = $this->getMockBuilder(IDBConnection::class)
->disableOriginalConstructor()->getMock();
$this->tempManager = $this->getMockBuilder(ITempManager::class)->getMock();
$this->notificationManager = $this->getMockBuilder(IManager::class)->getMock();
$this->checkSetupController = $this->getMockBuilder(CheckSetupController::class)
->setConstructorArgs([
'settings',
Expand All @@ -164,6 +168,7 @@ protected function setUp(): void {
$this->iniGetWrapper,
$this->connection,
$this->tempManager,
$this->notificationManager,
])
->setMethods([
'isReadOnlyConfig',
Expand All @@ -186,7 +191,6 @@ protected function setUp(): void {
'hasBigIntConversionPendingColumns',
'isMysqlUsedWithoutUTF8MB4',
'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed',
'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed',
])->getMock();
}

Expand Down Expand Up @@ -342,7 +346,7 @@ public function testIsPhpSupportedTrue() {
* @param string $remoteAddr
* @param bool $result
*/
public function testForwardedForHeadersWorking(array $trustedProxies, string $remoteAddrNotForwarded, string $remoteAddr, bool $result) {
public function testForwardedForHeadersWorking(array $trustedProxies, string $remoteAddrNotForwarded, string $remoteAddr, bool $result): void {
$this->config->expects($this->once())
->method('getSystemValue')
->with('trusted_proxies', [])
Expand All @@ -363,7 +367,7 @@ public function testForwardedForHeadersWorking(array $trustedProxies, string $re
);
}

public function dataForwardedForHeadersWorking() {
public function dataForwardedForHeadersWorking(): array {
return [
// description => trusted proxies, getHeader('REMOTE_ADDR'), getRemoteAddr, expected result
'no trusted proxies' => [[], '2.2.2.2', '2.2.2.2', true],
Expand All @@ -373,7 +377,28 @@ public function dataForwardedForHeadersWorking() {
];
}

public function testForwardedHostPresentButTrustedProxiesEmpty() {
public function testForwardedHostPresentButTrustedProxiesNotAnArray(): void {
$this->config->expects($this->once())
->method('getSystemValue')
->with('trusted_proxies', [])
->willReturn('1.1.1.1');
$this->request->expects($this->atLeastOnce())
->method('getHeader')
->willReturnMap([
['REMOTE_ADDR', '1.1.1.1'],
['X-Forwarded-Host', 'nextcloud.test']
]);
$this->request->expects($this->any())
->method('getRemoteAddress')
->willReturn('1.1.1.1');

$this->assertEquals(
false,
self::invokePrivate($this->checkSetupController, 'forwardedForHeadersWorking')
);
}

public function testForwardedHostPresentButTrustedProxiesEmpty(): void {
$this->config->expects($this->once())
->method('getSystemValue')
->with('trusted_proxies', [])
Expand Down Expand Up @@ -594,7 +619,7 @@ public function testCheck() {
'eol' => true,
'version' => PHP_VERSION
],
'forwardedForHeadersWorking' => true,
'forwardedForHeadersWorking' => false,
'reverseProxyDocs' => 'reverse-proxy-doc-link',
'isCorrectMemcachedPHPModuleInstalled' => true,
'hasPassedCodeIntegrityCheck' => true,
Expand Down Expand Up @@ -623,6 +648,8 @@ public function testCheck() {
'imageMagickLacksSVGSupport' => false,
'isDefaultPhoneRegionSet' => false,
'OCA\Settings\SetupChecks\SupportedDatabase' => ['pass' => true, 'description' => '', 'severity' => 'info'],
'isFairUseOfFreePushService' => false,
'temporaryDirectoryWritable' => false,
]
);
$this->assertEquals($expected, $this->checkSetupController->check());
Expand All @@ -647,6 +674,8 @@ public function testGetCurlVersion() {
$this->secureRandom,
$this->iniGetWrapper,
$this->connection,
$this->tempManager,
$this->notificationManager,
])
->setMethods(null)->getMock();

Expand Down Expand Up @@ -1401,23 +1430,25 @@ public function testIsMysqlUsedWithoutUTF8MB4(string $db, bool $useUTF8MB4, bool
});

$checkSetupController = new CheckSetupController(
'settings',
$this->request,
$this->config,
$this->clientService,
$this->urlGenerator,
$this->l10n,
$this->checker,
$this->logger,
$this->dispatcher,
$this->db,
$this->lockingProvider,
$this->dateTimeFormatter,
$this->memoryInfo,
$this->secureRandom,
$this->iniGetWrapper,
$this->connection
);
'settings',
$this->request,
$this->config,
$this->clientService,
$this->urlGenerator,
$this->l10n,
$this->checker,
$this->logger,
$this->dispatcher,
$this->db,
$this->lockingProvider,
$this->dateTimeFormatter,
$this->memoryInfo,
$this->secureRandom,
$this->iniGetWrapper,
$this->connection,
$this->tempManager,
$this->notificationManager
);

$this->assertSame($expected, $this->invokePrivate($checkSetupController, 'isMysqlUsedWithoutUTF8MB4'));
}
Expand Down Expand Up @@ -1466,7 +1497,9 @@ public function testIsEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed(string $m
$this->memoryInfo,
$this->secureRandom,
$this->iniGetWrapper,
$this->connection
$this->connection,
$this->tempManager,
$this->notificationManager
);

$this->assertSame($expected, $this->invokePrivate($checkSetupController, 'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed'));
Expand Down