Skip to content

Commit

Permalink
Merge pull request #22774 from nextcloud/bugfix/noid/no-delay-without-ip
Browse files Browse the repository at this point in the history
Don't break when the IP is empty
  • Loading branch information
MorrisJobke committed Sep 10, 2020
2 parents 40eabfb + c25063d commit 3d61f7b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/private/Security/Bruteforce/Throttler.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ private function isIPWhitelisted(string $ip): bool {
* @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;
Expand Down
24 changes: 22 additions & 2 deletions tests/lib/Security/Bruteforce/CapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ protected function setUp(): void {
parent::setUp();

$this->request = $this->createMock(IRequest::class);
$this->request->method('getRemoteAddress')
->willReturn('10.10.10.10');

$this->throttler = $this->createMock(Throttler::class);

Expand All @@ -57,6 +55,9 @@ public function testGetCapabilities() {
->with('10.10.10.10')
->willReturn(42);

$this->request->method('getRemoteAddress')
->willReturn('10.10.10.10');

$expected = [
'bruteforce' => [
'delay' => 42
Expand All @@ -66,4 +67,23 @@ public function testGetCapabilities() {

$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);
}
}

0 comments on commit 3d61f7b

Please sign in to comment.