|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 Lukas Reschke <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
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 |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OC\Security\RateLimiting; |
|
23
|
|
|
|
|
24
|
|
|
use OC\Security\Normalizer\IpAddress; |
|
25
|
|
|
use OC\Security\RateLimiting\Backend\IBackend; |
|
26
|
|
|
use OC\Security\RateLimiting\Exception\RateLimitExceededException; |
|
27
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
|
28
|
|
|
use OCP\IRequest; |
|
29
|
|
|
use OCP\IUser; |
|
30
|
|
|
use OCP\IUserSession; |
|
31
|
|
|
|
|
32
|
|
|
class Limiter { |
|
33
|
|
|
/** @var IBackend */ |
|
34
|
|
|
private $backend; |
|
35
|
|
|
/** @var ITimeFactory */ |
|
36
|
|
|
private $timeFactory; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param IUserSession $userSession |
|
40
|
|
|
* @param IRequest $request |
|
41
|
|
|
* @param ITimeFactory $timeFactory |
|
42
|
|
|
* @param IBackend $backend |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(IUserSession $userSession, |
|
|
|
|
|
|
45
|
|
|
IRequest $request, |
|
|
|
|
|
|
46
|
|
|
ITimeFactory $timeFactory, |
|
47
|
|
|
IBackend $backend) { |
|
48
|
|
|
$this->backend = $backend; |
|
49
|
|
|
$this->timeFactory = $timeFactory; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $methodIdentifier |
|
54
|
|
|
* @param string $userIdentifier |
|
55
|
|
|
* @param int $period |
|
56
|
|
|
* @param int $limit |
|
57
|
|
|
* @throws RateLimitExceededException |
|
58
|
|
|
*/ |
|
59
|
|
|
private function register($methodIdentifier, |
|
60
|
|
|
$userIdentifier, |
|
61
|
|
|
$period, |
|
62
|
|
|
$limit) { |
|
63
|
|
|
$existingAttempts = $this->backend->getAttempts($methodIdentifier, $userIdentifier, (int)$period); |
|
64
|
|
|
if ($existingAttempts >= (int)$limit) { |
|
65
|
|
|
throw new RateLimitExceededException(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->backend->registerAttempt($methodIdentifier, $userIdentifier, $this->timeFactory->getTime()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Registers attempt for an anonymous request |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $identifier |
|
75
|
|
|
* @param int $anonLimit |
|
76
|
|
|
* @param int $anonPeriod |
|
77
|
|
|
* @param string $ip |
|
78
|
|
|
* @throws RateLimitExceededException |
|
79
|
|
|
*/ |
|
80
|
|
|
public function registerAnonRequest($identifier, |
|
81
|
|
|
$anonLimit, |
|
82
|
|
|
$anonPeriod, |
|
83
|
|
|
$ip) { |
|
84
|
|
|
$ipSubnet = (new IpAddress($ip))->getSubnet(); |
|
85
|
|
|
|
|
86
|
|
|
$anonHashIdentifier = hash('sha512', 'anon::' . $identifier . $ipSubnet); |
|
87
|
|
|
$this->register($identifier, $anonHashIdentifier, $anonPeriod, $anonLimit); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Registers attempt for an authenticated request |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $identifier |
|
94
|
|
|
* @param int $userLimit |
|
95
|
|
|
* @param int $userPeriod |
|
96
|
|
|
* @param IUser $user |
|
97
|
|
|
* @throws RateLimitExceededException |
|
98
|
|
|
*/ |
|
99
|
|
|
public function registerUserRequest($identifier, |
|
100
|
|
|
$userLimit, |
|
101
|
|
|
$userPeriod, |
|
102
|
|
|
IUser $user) { |
|
103
|
|
|
$userHashIdentifier = hash('sha512', 'user::' . $identifier . $user->getUID()); |
|
104
|
|
|
$this->register($identifier, $userHashIdentifier, $userPeriod, $userLimit); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.