]> source.dussan.org Git - nextcloud-server.git/commitdiff
Don't break when the IP is empty 22774/head
authorJoas Schilling <coding@schilljs.com>
Thu, 10 Sep 2020 12:20:27 +0000 (14:20 +0200)
committerJoas Schilling <coding@schilljs.com>
Thu, 10 Sep 2020 12:20:27 +0000 (14:20 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Security/Bruteforce/Throttler.php
tests/lib/Security/Bruteforce/CapabilitiesTest.php

index 7b98b3d8288eac3dd9c65bb5b0b8f275517d24d2..61e18088e9456f4eac26e5f2788e67c913334451 100644 (file)
@@ -227,6 +227,10 @@ class Throttler {
         * @return int
         */
        public function getAttempts(string $ip, string $action = '', float $maxAgeHours = 12): int {
+               if ($ip === '') {
+                       return 0;
+               }
+
                $ipAddress = new IpAddress($ip);
                if ($this->isIPWhitelisted((string)$ipAddress)) {
                        return 0;
index 16c2b2caf15406ff75dee8510f48043c693a4785..cd43d94f8cbe64e8770b7006a9153d90755465c6 100644 (file)
@@ -40,8 +40,6 @@ class CapabilitiesTest extends TestCase {
                parent::setUp();
 
                $this->request = $this->createMock(IRequest::class);
-               $this->request->method('getRemoteAddress')
-                       ->willReturn('10.10.10.10');
 
                $this->throttler = $this->createMock(Throttler::class);
 
@@ -57,6 +55,9 @@ class CapabilitiesTest extends TestCase {
                        ->with('10.10.10.10')
                        ->willReturn(42);
 
+               $this->request->method('getRemoteAddress')
+                       ->willReturn('10.10.10.10');
+
                $expected = [
                        'bruteforce' => [
                                'delay' => 42
@@ -66,4 +67,23 @@ class CapabilitiesTest extends TestCase {
 
                $this->assertEquals($expected, $result);
        }
+
+       public function testGetCapabilitiesOnCli() {
+               $this->throttler->expects($this->atLeastOnce())
+                       ->method('getDelay')
+                       ->with('')
+                       ->willReturn(0);
+
+               $this->request->method('getRemoteAddress')
+                       ->willReturn('');
+
+               $expected = [
+                       'bruteforce' => [
+                               'delay' => 0
+                       ]
+               ];
+               $result = $this->capabilities->getCapabilities();
+
+               $this->assertEquals($expected, $result);
+       }
 }