owncloud /
security
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @author Semih Serhat Karakaya <[email protected]> |
||
| 4 | * |
||
| 5 | * @copyright Copyright (c) 2017, ownCloud GmbH |
||
| 6 | * @license AGPL-3.0 |
||
| 7 | * |
||
| 8 | * This code is free software: you can redistribute it and/or modify |
||
| 9 | * it under the terms of the GNU Affero General Public License, version 3, |
||
| 10 | * as published by the Free Software Foundation. |
||
| 11 | * |
||
| 12 | * This program is distributed in the hope that it will be useful, |
||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 15 | * GNU Affero General Public License for more details. |
||
| 16 | * |
||
| 17 | * You should have received a copy of the GNU Affero General Public License, version 3, |
||
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/> |
||
| 19 | * |
||
| 20 | */ |
||
| 21 | |||
| 22 | |||
| 23 | namespace OCA\Security\Tests\Db; |
||
| 24 | |||
| 25 | use OCA\Security\Db\DbService; |
||
| 26 | use OC\AppFramework\Utility\TimeFactory; |
||
| 27 | use OCA\Security\SecurityConfig; |
||
| 28 | use OCP\IDBConnection; |
||
| 29 | use Test\TestCase; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @group DB |
||
| 33 | */ |
||
| 34 | class DbServiceTest extends TestCase {
|
||
| 35 | |||
| 36 | /** @var DbService */ |
||
| 37 | private $dbService; |
||
| 38 | |||
| 39 | /** @var IDBConnection */ |
||
| 40 | private $connection; |
||
| 41 | |||
| 42 | /** @var TimeFactory */ |
||
| 43 | private $factory; |
||
| 44 | |||
| 45 | /** @var \PHPUnit_Framework_MockObject_MockObject | SecurityConfig */ |
||
| 46 | private $configMock; |
||
| 47 | |||
| 48 | /** @var string */ |
||
| 49 | private $dbTable = 'failed_login_attempts'; |
||
| 50 | |||
| 51 | public function setUp() {
|
||
| 52 | parent::setUp(); |
||
| 53 | |||
| 54 | $this->connection = \OC::$server->getDatabaseConnection(); |
||
| 55 | $this->factory = new TimeFactory(); |
||
| 56 | $this->configMock = $this->getMockBuilder('OCA\Security\SecurityConfig')
|
||
| 57 | ->disableOriginalConstructor() |
||
| 58 | ->getMock(); |
||
| 59 | $this->dbService = new DbService($this->connection, $this->factory, $this->configMock); |
||
| 60 | |||
| 61 | $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
||
| 62 | $result = $query->execute()->fetchAll(); |
||
| 63 | $this->assertEmpty($result, 'we need to start with a empty failed_login_attempts table'); |
||
| 64 | } |
||
| 65 | |||
| 66 | public function tearDown() {
|
||
| 67 | parent::tearDown(); |
||
| 68 | $query = $this->connection->getQueryBuilder()->delete($this->dbTable); |
||
| 69 | $query->execute(); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function testAddServer() {
|
||
| 73 | $ip = "192.168.1.1"; |
||
| 74 | $uid = "test"; |
||
| 75 | $this->dbService->addFailedLoginAttempt($uid, $ip); |
||
| 76 | |||
| 77 | $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
||
| 78 | $result = $query->execute()->fetchAll(); |
||
| 79 | $this->assertSame(1, count($result)); |
||
| 80 | $this->assertSame($ip, $result[0]['ip']); |
||
| 81 | $this->assertSame($uid, $result[0]['uid']); |
||
| 82 | } |
||
| 83 | |||
| 84 | View Code Duplication | public function testGetSuspiciousActivityCountForUid() {
|
|
|
0 ignored issues
–
show
|
|||
| 85 | $this->configMock->expects($this->once()) |
||
| 86 | ->method('getBruteForceProtectionTimeThreshold')
|
||
| 87 | ->willReturn('300');
|
||
| 88 | $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
|
||
| 89 | $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
|
||
| 90 | $this->assertEquals(2, $this->dbService->getSuspiciousActivityCountForUid('test1'));
|
||
| 91 | } |
||
| 92 | |||
| 93 | View Code Duplication | public function testGetSuspiciousActivityCountForIp() {
|
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 94 | $this->configMock->expects($this->once()) |
||
| 95 | ->method('getBruteForceProtectionTimeThreshold')
|
||
| 96 | ->willReturn('300');
|
||
| 97 | $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
|
||
| 98 | $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
|
||
| 99 | $this->assertEquals(2, $this->dbService->getSuspiciousActivityCountForIp('192.168.1.1'));
|
||
| 100 | } |
||
| 101 | |||
| 102 | public function testGetSuspiciousActivityCountForUidIpCombination() {
|
||
| 103 | $this->configMock->expects($this->once()) |
||
| 104 | ->method('getBruteForceProtectionTimeThreshold')
|
||
| 105 | ->willReturn('300');
|
||
| 106 | $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
|
||
| 107 | $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
|
||
| 108 | $this->dbService->addFailedLoginAttempt("test2", "192.168.1.1");
|
||
| 109 | $this->assertEquals(2, $this->dbService->getSuspiciousActivityCountForUidIpCombination('test1','192.168.1.1'));
|
||
| 110 | } |
||
| 111 | |||
| 112 | public function testGetLastFailedLoginAttemptTimeForIp() {
|
||
| 113 | $this->configMock->expects($this->once()) |
||
| 114 | ->method('getBruteForceProtectionTimeThreshold')
|
||
| 115 | ->willReturn('300');
|
||
| 116 | $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
|
||
| 117 | $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
|
||
| 118 | $query = $this->connection->getQueryBuilder()->select('attempted_at')->from($this->dbTable);
|
||
| 119 | $result = $query->execute()->fetchAll(); |
||
| 120 | $this->assertEquals($this->dbService->getLastFailedLoginAttemptTimeForIp('192.168.1.1'), $result[1]['attempted_at']);
|
||
| 121 | } |
||
| 122 | |||
| 123 | public function testDeleteSuspiciousAttemptsForIp() {
|
||
| 124 | $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
|
||
| 125 | $this->dbService->addFailedLoginAttempt("test2", "192.168.1.2");
|
||
| 126 | |||
| 127 | $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
||
| 128 | $result = $query->execute()->fetchAll(); |
||
| 129 | $this->assertSame(2, count($result)); |
||
| 130 | $this->assertSame('test1', $result[0]['uid']);
|
||
| 131 | $this->assertSame('test2', $result[1]['uid']);
|
||
| 132 | $this->assertSame("192.168.1.1", $result[0]['ip']);
|
||
| 133 | $this->assertSame("192.168.1.2", $result[1]['ip']);
|
||
| 134 | |||
| 135 | $this->dbService->deleteSuspiciousAttemptsForIp("192.168.1.1");
|
||
| 136 | $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
||
| 137 | $result = $query->execute()->fetchAll(); |
||
| 138 | $this->assertSame(1, count($result)); |
||
| 139 | $this->assertSame('test2', $result[0]['uid']);
|
||
| 140 | $this->assertSame("192.168.1.2", $result[0]['ip']);
|
||
| 141 | } |
||
| 142 | public function testDeleteSuspiciousAttemptsForUidIpCombination() {
|
||
| 143 | $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
|
||
| 144 | $this->dbService->addFailedLoginAttempt("test2", "192.168.1.1");
|
||
| 145 | |||
| 146 | $builder = $this->connection->getQueryBuilder(); |
||
| 147 | $query = $builder->select('*')->from($this->dbTable)
|
||
| 148 | ->Where($builder->expr()->eq('ip', $builder->createNamedParameter("192.168.1.1")));
|
||
| 149 | $result = $query->execute()->fetchAll(); |
||
| 150 | $this->assertSame(2, count($result)); |
||
| 151 | |||
| 152 | $this->dbService->deleteSuspiciousAttemptsForUidIpCombination('test1',"192.168.1.1");
|
||
| 153 | $query = $builder->select('*')->from($this->dbTable)
|
||
| 154 | ->Where($builder->expr()->eq('ip', $builder->createNamedParameter("192.168.1.1")));
|
||
| 155 | $result = $query->execute()->fetchAll(); |
||
| 156 | $this->assertSame(1, count($result)); |
||
| 157 | } |
||
| 158 | } |
||
| 159 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.