Skip to content

Commit 600bc22

Browse files
committed
Warning if x-forwarded-host present but trusted_proxies empty
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent a80bae3 commit 600bc22

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

settings/Controller/CheckSetupController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,14 @@ private function forwardedForHeadersWorking() {
289289
$trustedProxies = $this->config->getSystemValue('trusted_proxies', []);
290290
$remoteAddress = $this->request->getHeader('REMOTE_ADDR');
291291

292-
if (\is_array($trustedProxies) && \in_array($remoteAddress, $trustedProxies)) {
292+
if (empty($trustedProxies) && $this->request->getHeader('X-Forwarded-Host')) {
293+
return false;
294+
}
295+
296+
if (\is_array($trustedProxies) && \in_array($remoteAddress, $trustedProxies, true)) {
293297
return $remoteAddress !== $this->request->getRemoteAddress();
294298
}
299+
295300
// either not enabled or working correctly
296301
return true;
297302
}

tests/Settings/Controller/CheckSetupControllerTest.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,19 +310,21 @@ public function testIsPhpSupportedTrue() {
310310
* @dataProvider dataForwardedForHeadersWorking
311311
*
312312
* @param array $trustedProxies
313-
* @param string $remoteAddrNoForwarded
313+
* @param string $remoteAddrNotForwarded
314314
* @param string $remoteAddr
315315
* @param bool $result
316316
*/
317-
public function testForwardedForHeadersWorking(array $trustedProxies, string $remoteAddrNoForwarded, string $remoteAddr, bool $result) {
317+
public function testForwardedForHeadersWorking(array $trustedProxies, string $remoteAddrNotForwarded, string $remoteAddr, bool $result) {
318318
$this->config->expects($this->once())
319319
->method('getSystemValue')
320320
->with('trusted_proxies', [])
321321
->willReturn($trustedProxies);
322-
$this->request->expects($this->once())
322+
$this->request->expects($this->atLeastOnce())
323323
->method('getHeader')
324-
->with('REMOTE_ADDR')
325-
->willReturn($remoteAddrNoForwarded);
324+
->willReturnMap([
325+
['REMOTE_ADDR', $remoteAddrNotForwarded],
326+
['X-Forwarded-Host', '']
327+
]);
326328
$this->request->expects($this->any())
327329
->method('getRemoteAddress')
328330
->willReturn($remoteAddr);
@@ -343,6 +345,27 @@ public function dataForwardedForHeadersWorking() {
343345
];
344346
}
345347

348+
public function testForwardedHostPresentButTrustedProxiesEmpty() {
349+
$this->config->expects($this->once())
350+
->method('getSystemValue')
351+
->with('trusted_proxies', [])
352+
->willReturn([]);
353+
$this->request->expects($this->atLeastOnce())
354+
->method('getHeader')
355+
->willReturnMap([
356+
['REMOTE_ADDR', '1.1.1.1'],
357+
['X-Forwarded-Host', 'nextcloud.test']
358+
]);
359+
$this->request->expects($this->any())
360+
->method('getRemoteAddress')
361+
->willReturn('1.1.1.1');
362+
363+
$this->assertEquals(
364+
false,
365+
self::invokePrivate($this->checkSetupController, 'forwardedForHeadersWorking')
366+
);
367+
}
368+
346369
public function testCheck() {
347370
$this->config->expects($this->at(0))
348371
->method('getAppValue')
@@ -365,10 +388,12 @@ public function testCheck() {
365388
->with('appstoreenabled', true)
366389
->will($this->returnValue(false));
367390

368-
$this->request->expects($this->once())
391+
$this->request->expects($this->atLeastOnce())
369392
->method('getHeader')
370-
->with('REMOTE_ADDR')
371-
->willReturn('4.3.2.1');
393+
->willReturnMap([
394+
['REMOTE_ADDR', '4.3.2.1'],
395+
['X-Forwarded-Host', '']
396+
]);
372397

373398
$client = $this->getMockBuilder('\OCP\Http\Client\IClient')
374399
->disableOriginalConstructor()->getMock();

0 commit comments

Comments
 (0)