2016-07-20 19:36:15 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-07-20 19:36:15 +03:00
|
|
|
namespace Test\Security\Bruteforce;
|
|
|
|
|
|
|
|
use OC\AppFramework\Utility\TimeFactory;
|
|
|
|
use OC\Security\Bruteforce\Throttler;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Based on the unit tests from Paragonie's Airship CMS
|
|
|
|
* Ref: https://github.com/paragonie/airship/blob/7e5bad7e3c0fbbf324c11f963fd1f80e59762606/test/unit/Engine/Security/AirBrakeTest.php
|
|
|
|
*
|
|
|
|
* @package Test\Security\Bruteforce
|
|
|
|
*/
|
|
|
|
class ThrottlerTest extends TestCase {
|
|
|
|
/** @var Throttler */
|
|
|
|
private $throttler;
|
|
|
|
/** @var IDBConnection */
|
|
|
|
private $dbConnection;
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2016-11-14 16:05:01 +03:00
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
2016-07-20 19:36:15 +03:00
|
|
|
private $config;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-09-07 21:20:08 +03:00
|
|
|
$this->dbConnection = $this->createMock(IDBConnection::class);
|
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2016-07-20 19:36:15 +03:00
|
|
|
|
|
|
|
$this->throttler = new Throttler(
|
|
|
|
$this->dbConnection,
|
|
|
|
new TimeFactory(),
|
|
|
|
$this->logger,
|
|
|
|
$this->config
|
|
|
|
);
|
2017-05-01 19:31:45 +03:00
|
|
|
parent::setUp();
|
2016-07-20 19:36:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCutoff() {
|
|
|
|
// precisely 31 second shy of 12 hours
|
2017-05-01 19:31:45 +03:00
|
|
|
$cutoff = self::invokePrivate($this->throttler, 'getCutoff', [43169]);
|
2016-07-20 19:36:15 +03:00
|
|
|
$this->assertSame(0, $cutoff->y);
|
|
|
|
$this->assertSame(0, $cutoff->m);
|
|
|
|
$this->assertSame(0, $cutoff->d);
|
|
|
|
$this->assertSame(11, $cutoff->h);
|
|
|
|
$this->assertSame(59, $cutoff->i);
|
|
|
|
$this->assertSame(29, $cutoff->s);
|
2017-05-01 19:31:45 +03:00
|
|
|
$cutoff = self::invokePrivate($this->throttler, 'getCutoff', [86401]);
|
2016-07-20 19:36:15 +03:00
|
|
|
$this->assertSame(0, $cutoff->y);
|
|
|
|
$this->assertSame(0, $cutoff->m);
|
|
|
|
$this->assertSame(1, $cutoff->d);
|
|
|
|
$this->assertSame(0, $cutoff->h);
|
|
|
|
$this->assertSame(0, $cutoff->i);
|
|
|
|
// Leap second tolerance:
|
|
|
|
$this->assertLessThan(2, $cutoff->s);
|
|
|
|
}
|
|
|
|
|
2016-11-14 16:05:01 +03:00
|
|
|
public function dataIsIPWhitelisted() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'10.10.10.10',
|
|
|
|
[
|
|
|
|
'whitelist_0' => '10.10.10.0/24',
|
|
|
|
],
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'10.10.10.10',
|
|
|
|
[
|
|
|
|
'whitelist_0' => '192.168.0.0/16',
|
|
|
|
],
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'10.10.10.10',
|
|
|
|
[
|
|
|
|
'whitelist_0' => '192.168.0.0/16',
|
|
|
|
'whitelist_1' => '10.10.10.0/24',
|
|
|
|
],
|
|
|
|
true,
|
|
|
|
],
|
2019-02-12 01:22:20 +03:00
|
|
|
[
|
|
|
|
'10.10.10.10',
|
|
|
|
[
|
|
|
|
'whitelist_0' => '10.10.10.11/31',
|
|
|
|
],
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'10.10.10.10',
|
|
|
|
[
|
|
|
|
'whitelist_0' => '10.10.10.9/31',
|
|
|
|
],
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'10.10.10.10',
|
|
|
|
[
|
|
|
|
'whitelist_0' => '10.10.10.15/29',
|
|
|
|
],
|
|
|
|
true,
|
|
|
|
],
|
2016-11-14 16:05:01 +03:00
|
|
|
[
|
|
|
|
'dead:beef:cafe::1',
|
|
|
|
[
|
|
|
|
'whitelist_0' => '192.168.0.0/16',
|
|
|
|
'whitelist_1' => '10.10.10.0/24',
|
|
|
|
'whitelist_2' => 'deaf:beef:cafe:1234::/64'
|
|
|
|
],
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'dead:beef:cafe::1',
|
|
|
|
[
|
|
|
|
'whitelist_0' => '192.168.0.0/16',
|
|
|
|
'whitelist_1' => '10.10.10.0/24',
|
|
|
|
'whitelist_2' => 'deaf:beef::/64'
|
|
|
|
],
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'dead:beef:cafe::1',
|
|
|
|
[
|
|
|
|
'whitelist_0' => '192.168.0.0/16',
|
|
|
|
'whitelist_1' => '10.10.10.0/24',
|
|
|
|
'whitelist_2' => 'deaf:cafe::/8'
|
|
|
|
],
|
|
|
|
true,
|
|
|
|
],
|
2019-02-12 01:22:20 +03:00
|
|
|
[
|
|
|
|
'dead:beef:cafe::1111',
|
|
|
|
[
|
|
|
|
'whitelist_0' => 'dead:beef:cafe::1100/123',
|
|
|
|
|
|
|
|
],
|
|
|
|
true,
|
|
|
|
],
|
2016-11-14 16:05:01 +03:00
|
|
|
[
|
|
|
|
'invalid',
|
|
|
|
[],
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $ip
|
2017-05-01 21:36:25 +03:00
|
|
|
* @param string[] $whitelists
|
2016-11-14 16:05:01 +03:00
|
|
|
* @param bool $isWhiteListed
|
2017-05-01 19:31:45 +03:00
|
|
|
* @param bool $enabled
|
2016-11-14 16:05:01 +03:00
|
|
|
*/
|
2017-05-01 19:31:45 +03:00
|
|
|
private function isIpWhiteListedHelper($ip,
|
|
|
|
$whitelists,
|
|
|
|
$isWhiteListed,
|
|
|
|
$enabled) {
|
2016-11-14 16:05:01 +03:00
|
|
|
$this->config->method('getAppKeys')
|
|
|
|
->with($this->equalTo('bruteForce'))
|
|
|
|
->willReturn(array_keys($whitelists));
|
2017-05-01 19:31:45 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('auth.bruteforce.protection.enabled', true)
|
|
|
|
->willReturn($enabled);
|
2016-11-14 16:05:01 +03:00
|
|
|
|
|
|
|
$this->config->method('getAppValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function($app, $key, $default) use ($whitelists) {
|
2016-11-14 16:05:01 +03:00
|
|
|
if ($app !== 'bruteForce') {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
if (isset($whitelists[$key])) {
|
|
|
|
return $whitelists[$key];
|
|
|
|
}
|
|
|
|
return $default;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-11-14 16:05:01 +03:00
|
|
|
|
|
|
|
$this->assertSame(
|
2017-05-01 19:31:45 +03:00
|
|
|
($enabled === false) ? true : $isWhiteListed,
|
|
|
|
self::invokePrivate($this->throttler, 'isIPWhitelisted', [$ip])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataIsIPWhitelisted
|
|
|
|
*
|
|
|
|
* @param string $ip
|
|
|
|
* @param string[] $whitelists
|
|
|
|
* @param bool $isWhiteListed
|
|
|
|
*/
|
|
|
|
public function testIsIpWhiteListedWithEnabledProtection($ip,
|
|
|
|
$whitelists,
|
|
|
|
$isWhiteListed) {
|
|
|
|
$this->isIpWhiteListedHelper(
|
|
|
|
$ip,
|
|
|
|
$whitelists,
|
|
|
|
$isWhiteListed,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataIsIPWhitelisted
|
|
|
|
*
|
|
|
|
* @param string $ip
|
|
|
|
* @param string[] $whitelists
|
|
|
|
* @param bool $isWhiteListed
|
|
|
|
*/
|
|
|
|
public function testIsIpWhiteListedWithDisabledProtection($ip,
|
|
|
|
$whitelists,
|
|
|
|
$isWhiteListed) {
|
|
|
|
$this->isIpWhiteListedHelper(
|
|
|
|
$ip,
|
|
|
|
$whitelists,
|
2016-11-14 16:05:01 +03:00
|
|
|
$isWhiteListed,
|
2017-05-01 19:31:45 +03:00
|
|
|
false
|
2016-11-14 16:05:01 +03:00
|
|
|
);
|
|
|
|
}
|
2016-07-20 19:36:15 +03:00
|
|
|
}
|