|
9 | 9 | namespace OC\Security\Bruteforce;
|
10 | 10 |
|
11 | 11 | use OC\Security\Bruteforce\Backend\IBackend;
|
| 12 | +use OC\Security\Ip\BruteforceAllowList; |
12 | 13 | use OC\Security\Normalizer\IpAddress;
|
13 | 14 | use OCP\AppFramework\Utility\ITimeFactory;
|
14 | 15 | use OCP\IConfig;
|
|
32 | 33 | class Throttler implements IThrottler {
|
33 | 34 | /** @var bool[] */
|
34 | 35 | private array $hasAttemptsDeleted = [];
|
35 |
| - /** @var bool[] */ |
36 |
| - private array $ipIsWhitelisted = []; |
37 | 36 |
|
38 | 37 | public function __construct(
|
39 | 38 | private ITimeFactory $timeFactory,
|
40 | 39 | private LoggerInterface $logger,
|
41 | 40 | private IConfig $config,
|
42 | 41 | private IBackend $backend,
|
| 42 | + private BruteforceAllowList $allowList, |
43 | 43 | ) {
|
44 | 44 | }
|
45 | 45 |
|
@@ -83,70 +83,7 @@ public function registerAttempt(string $action,
|
83 | 83 | * Check if the IP is whitelisted
|
84 | 84 | */
|
85 | 85 | public function isBypassListed(string $ip): bool {
|
86 |
| - if (isset($this->ipIsWhitelisted[$ip])) { |
87 |
| - return $this->ipIsWhitelisted[$ip]; |
88 |
| - } |
89 |
| - |
90 |
| - if (!$this->config->getSystemValueBool('auth.bruteforce.protection.enabled', true)) { |
91 |
| - $this->ipIsWhitelisted[$ip] = true; |
92 |
| - return true; |
93 |
| - } |
94 |
| - |
95 |
| - $keys = $this->config->getAppKeys('bruteForce'); |
96 |
| - $keys = array_filter($keys, function ($key) { |
97 |
| - return str_starts_with($key, 'whitelist_'); |
98 |
| - }); |
99 |
| - |
100 |
| - if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
101 |
| - $type = 4; |
102 |
| - } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
103 |
| - $type = 6; |
104 |
| - } else { |
105 |
| - $this->ipIsWhitelisted[$ip] = false; |
106 |
| - return false; |
107 |
| - } |
108 |
| - |
109 |
| - $ip = inet_pton($ip); |
110 |
| - |
111 |
| - foreach ($keys as $key) { |
112 |
| - $cidr = $this->config->getAppValue('bruteForce', $key, null); |
113 |
| - |
114 |
| - $cx = explode('/', $cidr); |
115 |
| - $addr = $cx[0]; |
116 |
| - $mask = (int)$cx[1]; |
117 |
| - |
118 |
| - // Do not compare ipv4 to ipv6 |
119 |
| - if (($type === 4 && !filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) || |
120 |
| - ($type === 6 && !filter_var($addr, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))) { |
121 |
| - continue; |
122 |
| - } |
123 |
| - |
124 |
| - $addr = inet_pton($addr); |
125 |
| - |
126 |
| - $valid = true; |
127 |
| - for ($i = 0; $i < $mask; $i++) { |
128 |
| - $part = ord($addr[(int)($i / 8)]); |
129 |
| - $orig = ord($ip[(int)($i / 8)]); |
130 |
| - |
131 |
| - $bitmask = 1 << (7 - ($i % 8)); |
132 |
| - |
133 |
| - $part = $part & $bitmask; |
134 |
| - $orig = $orig & $bitmask; |
135 |
| - |
136 |
| - if ($part !== $orig) { |
137 |
| - $valid = false; |
138 |
| - break; |
139 |
| - } |
140 |
| - } |
141 |
| - |
142 |
| - if ($valid === true) { |
143 |
| - $this->ipIsWhitelisted[$ip] = true; |
144 |
| - return true; |
145 |
| - } |
146 |
| - } |
147 |
| - |
148 |
| - $this->ipIsWhitelisted[$ip] = false; |
149 |
| - return false; |
| 86 | + return $this->allowList->isBypassListed($ip); |
150 | 87 | }
|
151 | 88 |
|
152 | 89 | /**
|
|
0 commit comments