Skip to content

Commit 9673579

Browse files
Merge pull request #29522 from nextcloud/bugfix/26256/make-sure-trusted_proxies-is-an-array
Make sure trusted_proxies is an array
2 parents f5954a2 + e8f7a08 commit 9673579

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-26
lines changed

apps/settings/lib/Controller/CheckSetupController.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -330,16 +330,20 @@ private function isPhpSupported(): array {
330330
*
331331
* @return bool
332332
*/
333-
private function forwardedForHeadersWorking() {
333+
private function forwardedForHeadersWorking(): bool {
334334
$trustedProxies = $this->config->getSystemValue('trusted_proxies', []);
335335
$remoteAddress = $this->request->getHeader('REMOTE_ADDR');
336336

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

341-
if (\is_array($trustedProxies) && \in_array($remoteAddress, $trustedProxies, true) && $remoteAddress !== '127.0.0.1') {
342-
return $remoteAddress !== $this->request->getRemoteAddress();
341+
if (\is_array($trustedProxies)) {
342+
if (\in_array($remoteAddress, $trustedProxies, true) && $remoteAddress !== '127.0.0.1') {
343+
return $remoteAddress !== $this->request->getRemoteAddress();
344+
}
345+
} else {
346+
return false;
343347
}
344348

345349
// either not enabled or working correctly

apps/settings/tests/Controller/CheckSetupControllerTest.php

+56-23
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
use OCP\ITempManager;
5656
use OCP\IURLGenerator;
5757
use OCP\Lock\ILockingProvider;
58+
use OCP\Notification\IManager;
5859
use PHPUnit\Framework\MockObject\MockObject;
5960
use Psr\Http\Message\ResponseInterface;
6061
use Psr\Log\LoggerInterface;
@@ -102,6 +103,8 @@ class CheckSetupControllerTest extends TestCase {
102103
private $connection;
103104
/** @var ITempManager|\PHPUnit\Framework\MockObject\MockObject */
104105
private $tempManager;
106+
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
107+
private $notificationManager;
105108

106109
/**
107110
* Holds a list of directories created during tests.
@@ -145,6 +148,7 @@ protected function setUp(): void {
145148
$this->connection = $this->getMockBuilder(IDBConnection::class)
146149
->disableOriginalConstructor()->getMock();
147150
$this->tempManager = $this->getMockBuilder(ITempManager::class)->getMock();
151+
$this->notificationManager = $this->getMockBuilder(IManager::class)->getMock();
148152
$this->checkSetupController = $this->getMockBuilder(CheckSetupController::class)
149153
->setConstructorArgs([
150154
'settings',
@@ -164,6 +168,7 @@ protected function setUp(): void {
164168
$this->iniGetWrapper,
165169
$this->connection,
166170
$this->tempManager,
171+
$this->notificationManager,
167172
])
168173
->setMethods([
169174
'isReadOnlyConfig',
@@ -186,7 +191,6 @@ protected function setUp(): void {
186191
'hasBigIntConversionPendingColumns',
187192
'isMysqlUsedWithoutUTF8MB4',
188193
'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed',
189-
'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed',
190194
])->getMock();
191195
}
192196

@@ -342,7 +346,7 @@ public function testIsPhpSupportedTrue() {
342346
* @param string $remoteAddr
343347
* @param bool $result
344348
*/
345-
public function testForwardedForHeadersWorking(array $trustedProxies, string $remoteAddrNotForwarded, string $remoteAddr, bool $result) {
349+
public function testForwardedForHeadersWorking(array $trustedProxies, string $remoteAddrNotForwarded, string $remoteAddr, bool $result): void {
346350
$this->config->expects($this->once())
347351
->method('getSystemValue')
348352
->with('trusted_proxies', [])
@@ -363,7 +367,7 @@ public function testForwardedForHeadersWorking(array $trustedProxies, string $re
363367
);
364368
}
365369

366-
public function dataForwardedForHeadersWorking() {
370+
public function dataForwardedForHeadersWorking(): array {
367371
return [
368372
// description => trusted proxies, getHeader('REMOTE_ADDR'), getRemoteAddr, expected result
369373
'no trusted proxies' => [[], '2.2.2.2', '2.2.2.2', true],
@@ -373,7 +377,28 @@ public function dataForwardedForHeadersWorking() {
373377
];
374378
}
375379

376-
public function testForwardedHostPresentButTrustedProxiesEmpty() {
380+
public function testForwardedHostPresentButTrustedProxiesNotAnArray(): void {
381+
$this->config->expects($this->once())
382+
->method('getSystemValue')
383+
->with('trusted_proxies', [])
384+
->willReturn('1.1.1.1');
385+
$this->request->expects($this->atLeastOnce())
386+
->method('getHeader')
387+
->willReturnMap([
388+
['REMOTE_ADDR', '1.1.1.1'],
389+
['X-Forwarded-Host', 'nextcloud.test']
390+
]);
391+
$this->request->expects($this->any())
392+
->method('getRemoteAddress')
393+
->willReturn('1.1.1.1');
394+
395+
$this->assertEquals(
396+
false,
397+
self::invokePrivate($this->checkSetupController, 'forwardedForHeadersWorking')
398+
);
399+
}
400+
401+
public function testForwardedHostPresentButTrustedProxiesEmpty(): void {
377402
$this->config->expects($this->once())
378403
->method('getSystemValue')
379404
->with('trusted_proxies', [])
@@ -594,7 +619,7 @@ public function testCheck() {
594619
'eol' => true,
595620
'version' => PHP_VERSION
596621
],
597-
'forwardedForHeadersWorking' => true,
622+
'forwardedForHeadersWorking' => false,
598623
'reverseProxyDocs' => 'reverse-proxy-doc-link',
599624
'isCorrectMemcachedPHPModuleInstalled' => true,
600625
'hasPassedCodeIntegrityCheck' => true,
@@ -623,6 +648,8 @@ public function testCheck() {
623648
'imageMagickLacksSVGSupport' => false,
624649
'isDefaultPhoneRegionSet' => false,
625650
'OCA\Settings\SetupChecks\SupportedDatabase' => ['pass' => true, 'description' => '', 'severity' => 'info'],
651+
'isFairUseOfFreePushService' => false,
652+
'temporaryDirectoryWritable' => false,
626653
]
627654
);
628655
$this->assertEquals($expected, $this->checkSetupController->check());
@@ -647,6 +674,8 @@ public function testGetCurlVersion() {
647674
$this->secureRandom,
648675
$this->iniGetWrapper,
649676
$this->connection,
677+
$this->tempManager,
678+
$this->notificationManager,
650679
])
651680
->setMethods(null)->getMock();
652681

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

14031432
$checkSetupController = new CheckSetupController(
1404-
'settings',
1405-
$this->request,
1406-
$this->config,
1407-
$this->clientService,
1408-
$this->urlGenerator,
1409-
$this->l10n,
1410-
$this->checker,
1411-
$this->logger,
1412-
$this->dispatcher,
1413-
$this->db,
1414-
$this->lockingProvider,
1415-
$this->dateTimeFormatter,
1416-
$this->memoryInfo,
1417-
$this->secureRandom,
1418-
$this->iniGetWrapper,
1419-
$this->connection
1420-
);
1433+
'settings',
1434+
$this->request,
1435+
$this->config,
1436+
$this->clientService,
1437+
$this->urlGenerator,
1438+
$this->l10n,
1439+
$this->checker,
1440+
$this->logger,
1441+
$this->dispatcher,
1442+
$this->db,
1443+
$this->lockingProvider,
1444+
$this->dateTimeFormatter,
1445+
$this->memoryInfo,
1446+
$this->secureRandom,
1447+
$this->iniGetWrapper,
1448+
$this->connection,
1449+
$this->tempManager,
1450+
$this->notificationManager
1451+
);
14211452

14221453
$this->assertSame($expected, $this->invokePrivate($checkSetupController, 'isMysqlUsedWithoutUTF8MB4'));
14231454
}
@@ -1466,7 +1497,9 @@ public function testIsEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed(string $m
14661497
$this->memoryInfo,
14671498
$this->secureRandom,
14681499
$this->iniGetWrapper,
1469-
$this->connection
1500+
$this->connection,
1501+
$this->tempManager,
1502+
$this->notificationManager
14701503
);
14711504

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

0 commit comments

Comments
 (0)