diff options
author | ArthurHoaro <arthur@hoa.ro> | 2019-05-25 16:13:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-25 16:13:56 +0200 |
commit | c3a04e328f2c8d40890b0b26b118a193110634ce (patch) | |
tree | 14ec8fd8d1d9d65d2e5234191cad40be9691d035 | |
parent | 8ed59f107e5b373f3517b3c33e817e70375829c8 (diff) | |
parent | b49a04f796b9ad8533c53fee541132a4c2214909 (diff) | |
download | Shaarli-c3a04e328f2c8d40890b0b26b118a193110634ce.tar.gz Shaarli-c3a04e328f2c8d40890b0b26b118a193110634ce.tar.zst Shaarli-c3a04e328f2c8d40890b0b26b118a193110634ce.zip |
Merge pull request #1273 from ArthurHoaro/feature/ban-manager
Rewrite IP ban management
-rw-r--r-- | application/security/BanManager.php | 213 | ||||
-rw-r--r-- | application/security/LoginManager.php | 100 | ||||
-rw-r--r-- | doc/md/Server-configuration.md | 2 | ||||
-rw-r--r-- | index.php | 2 | ||||
-rw-r--r-- | tests/security/BanManagerTest.php | 393 | ||||
-rw-r--r-- | tests/security/LoginManagerTest.php | 104 |
6 files changed, 630 insertions, 184 deletions
diff --git a/application/security/BanManager.php b/application/security/BanManager.php new file mode 100644 index 00000000..68190c54 --- /dev/null +++ b/application/security/BanManager.php | |||
@@ -0,0 +1,213 @@ | |||
1 | <?php | ||
2 | |||
3 | |||
4 | namespace Shaarli\Security; | ||
5 | |||
6 | use Shaarli\FileUtils; | ||
7 | |||
8 | /** | ||
9 | * Class BanManager | ||
10 | * | ||
11 | * Failed login attempts will store the associated IP address. | ||
12 | * After N failed attempts, the IP will be prevented from log in for duration D. | ||
13 | * Both N and D can be set in the configuration file. | ||
14 | * | ||
15 | * @package Shaarli\Security | ||
16 | */ | ||
17 | class BanManager | ||
18 | { | ||
19 | /** @var array List of allowed proxies IP */ | ||
20 | protected $trustedProxies; | ||
21 | |||
22 | /** @var int Number of allowed failed attempt before the ban */ | ||
23 | protected $nbAttempts; | ||
24 | |||
25 | /** @var int Ban duration in seconds */ | ||
26 | protected $banDuration; | ||
27 | |||
28 | /** @var string Path to the file containing IP bans and failures */ | ||
29 | protected $banFile; | ||
30 | |||
31 | /** @var string Path to the log file, used to log bans */ | ||
32 | protected $logFile; | ||
33 | |||
34 | /** @var array List of IP with their associated number of failed attempts */ | ||
35 | protected $failures = []; | ||
36 | |||
37 | /** @var array List of banned IP with their associated unban timestamp */ | ||
38 | protected $bans = []; | ||
39 | |||
40 | /** | ||
41 | * BanManager constructor. | ||
42 | * | ||
43 | * @param array $trustedProxies List of allowed proxies IP | ||
44 | * @param int $nbAttempts Number of allowed failed attempt before the ban | ||
45 | * @param int $banDuration Ban duration in seconds | ||
46 | * @param string $banFile Path to the file containing IP bans and failures | ||
47 | * @param string $logFile Path to the log file, used to log bans | ||
48 | */ | ||
49 | public function __construct($trustedProxies, $nbAttempts, $banDuration, $banFile, $logFile) { | ||
50 | $this->trustedProxies = $trustedProxies; | ||
51 | $this->nbAttempts = $nbAttempts; | ||
52 | $this->banDuration = $banDuration; | ||
53 | $this->banFile = $banFile; | ||
54 | $this->logFile = $logFile; | ||
55 | $this->readBanFile(); | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * Handle a failed login and ban the IP after too many failed attempts | ||
60 | * | ||
61 | * @param array $server The $_SERVER array | ||
62 | */ | ||
63 | public function handleFailedAttempt($server) | ||
64 | { | ||
65 | $ip = $this->getIp($server); | ||
66 | // the IP is behind a trusted forward proxy, but is not forwarded | ||
67 | // in the HTTP headers, so we do nothing | ||
68 | if (empty($ip)) { | ||
69 | return; | ||
70 | } | ||
71 | |||
72 | // increment the fail count for this IP | ||
73 | if (isset($this->failures[$ip])) { | ||
74 | $this->failures[$ip]++; | ||
75 | } else { | ||
76 | $this->failures[$ip] = 1; | ||
77 | } | ||
78 | |||
79 | if ($this->failures[$ip] >= $this->nbAttempts) { | ||
80 | $this->bans[$ip] = time() + $this->banDuration; | ||
81 | logm( | ||
82 | $this->logFile, | ||
83 | $server['REMOTE_ADDR'], | ||
84 | 'IP address banned from login: '. $ip | ||
85 | ); | ||
86 | } | ||
87 | $this->writeBanFile(); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Remove failed attempts for the provided client. | ||
92 | * | ||
93 | * @param array $server $_SERVER | ||
94 | */ | ||
95 | public function clearFailures($server) | ||
96 | { | ||
97 | $ip = $this->getIp($server); | ||
98 | // the IP is behind a trusted forward proxy, but is not forwarded | ||
99 | // in the HTTP headers, so we do nothing | ||
100 | if (empty($ip)) { | ||
101 | return; | ||
102 | } | ||
103 | |||
104 | if (isset($this->failures[$ip])) { | ||
105 | unset($this->failures[$ip]); | ||
106 | } | ||
107 | $this->writeBanFile(); | ||
108 | } | ||
109 | |||
110 | /** | ||
111 | * Check whether the client IP is banned or not. | ||
112 | * | ||
113 | * @param array $server $_SERVER | ||
114 | * | ||
115 | * @return bool True if the IP is banned, false otherwise | ||
116 | */ | ||
117 | public function isBanned($server) | ||
118 | { | ||
119 | $ip = $this->getIp($server); | ||
120 | // the IP is behind a trusted forward proxy, but is not forwarded | ||
121 | // in the HTTP headers, so we allow the authentication attempt. | ||
122 | if (empty($ip)) { | ||
123 | return false; | ||
124 | } | ||
125 | |||
126 | // the user is not banned | ||
127 | if (! isset($this->bans[$ip])) { | ||
128 | return false; | ||
129 | } | ||
130 | |||
131 | // the user is still banned | ||
132 | if ($this->bans[$ip] > time()) { | ||
133 | return true; | ||
134 | } | ||
135 | |||
136 | // the ban has expired, the user can attempt to log in again | ||
137 | if (isset($this->failures[$ip])) { | ||
138 | unset($this->failures[$ip]); | ||
139 | } | ||
140 | unset($this->bans[$ip]); | ||
141 | logm($this->logFile, $server['REMOTE_ADDR'], 'Ban lifted for: '. $ip); | ||
142 | |||
143 | $this->writeBanFile(); | ||
144 | return false; | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * Retrieve the IP from $_SERVER. | ||
149 | * If the actual IP is behind an allowed reverse proxy, | ||
150 | * we try to extract the forwarded IP from HTTP headers. | ||
151 | * | ||
152 | * @param array $server $_SERVER | ||
153 | * | ||
154 | * @return string|bool The IP or false if none could be extracted | ||
155 | */ | ||
156 | protected function getIp($server) | ||
157 | { | ||
158 | $ip = $server['REMOTE_ADDR']; | ||
159 | if (! in_array($ip, $this->trustedProxies)) { | ||
160 | return $ip; | ||
161 | } | ||
162 | return getIpAddressFromProxy($server, $this->trustedProxies); | ||
163 | } | ||
164 | |||
165 | /** | ||
166 | * Read a file containing banned IPs | ||
167 | */ | ||
168 | protected function readBanFile() | ||
169 | { | ||
170 | $data = FileUtils::readFlatDB($this->banFile); | ||
171 | if (isset($data['failures']) && is_array($data['failures'])) { | ||
172 | $this->failures = $data['failures']; | ||
173 | } | ||
174 | |||
175 | if (isset($data['bans']) && is_array($data['bans'])) { | ||
176 | $this->bans = $data['bans']; | ||
177 | } | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | * Write the banned IPs to a file | ||
182 | */ | ||
183 | protected function writeBanFile() | ||
184 | { | ||
185 | return FileUtils::writeFlatDB( | ||
186 | $this->banFile, | ||
187 | [ | ||
188 | 'failures' => $this->failures, | ||
189 | 'bans' => $this->bans, | ||
190 | ] | ||
191 | ); | ||
192 | } | ||
193 | |||
194 | /** | ||
195 | * Get the Failures (for UT purpose). | ||
196 | * | ||
197 | * @return array | ||
198 | */ | ||
199 | public function getFailures() | ||
200 | { | ||
201 | return $this->failures; | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * Get the Bans (for UT purpose). | ||
206 | * | ||
207 | * @return array | ||
208 | */ | ||
209 | public function getBans() | ||
210 | { | ||
211 | return $this->bans; | ||
212 | } | ||
213 | } | ||
diff --git a/application/security/LoginManager.php b/application/security/LoginManager.php index 1ff3d0be..0b0ce0b1 100644 --- a/application/security/LoginManager.php +++ b/application/security/LoginManager.php | |||
@@ -20,8 +20,8 @@ class LoginManager | |||
20 | /** @var SessionManager Session Manager instance **/ | 20 | /** @var SessionManager Session Manager instance **/ |
21 | protected $sessionManager = null; | 21 | protected $sessionManager = null; |
22 | 22 | ||
23 | /** @var string Path to the file containing IP bans */ | 23 | /** @var BanManager Ban Manager instance **/ |
24 | protected $banFile = ''; | 24 | protected $banManager; |
25 | 25 | ||
26 | /** @var bool Whether the user is logged in **/ | 26 | /** @var bool Whether the user is logged in **/ |
27 | protected $isLoggedIn = false; | 27 | protected $isLoggedIn = false; |
@@ -35,17 +35,21 @@ class LoginManager | |||
35 | /** | 35 | /** |
36 | * Constructor | 36 | * Constructor |
37 | * | 37 | * |
38 | * @param array $globals The $GLOBALS array (reference) | ||
39 | * @param ConfigManager $configManager Configuration Manager instance | 38 | * @param ConfigManager $configManager Configuration Manager instance |
40 | * @param SessionManager $sessionManager SessionManager instance | 39 | * @param SessionManager $sessionManager SessionManager instance |
41 | */ | 40 | */ |
42 | public function __construct(& $globals, $configManager, $sessionManager) | 41 | public function __construct($configManager, $sessionManager) |
43 | { | 42 | { |
44 | $this->globals = &$globals; | ||
45 | $this->configManager = $configManager; | 43 | $this->configManager = $configManager; |
46 | $this->sessionManager = $sessionManager; | 44 | $this->sessionManager = $sessionManager; |
47 | $this->banFile = $this->configManager->get('resource.ban_file', 'data/ipbans.php'); | 45 | $this->banManager = new BanManager( |
48 | $this->readBanFile(); | 46 | $this->configManager->get('security.trusted_proxies', []), |
47 | $this->configManager->get('security.ban_after'), | ||
48 | $this->configManager->get('security.ban_duration'), | ||
49 | $this->configManager->get('resource.ban_file', 'data/ipbans.php'), | ||
50 | $this->configManager->get('resource.log') | ||
51 | ); | ||
52 | |||
49 | if ($this->configManager->get('security.open_shaarli') === true) { | 53 | if ($this->configManager->get('security.open_shaarli') === true) { |
50 | $this->openShaarli = true; | 54 | $this->openShaarli = true; |
51 | } | 55 | } |
@@ -158,65 +162,13 @@ class LoginManager | |||
158 | } | 162 | } |
159 | 163 | ||
160 | /** | 164 | /** |
161 | * Read a file containing banned IPs | ||
162 | */ | ||
163 | protected function readBanFile() | ||
164 | { | ||
165 | if (! file_exists($this->banFile)) { | ||
166 | return; | ||
167 | } | ||
168 | include $this->banFile; | ||
169 | } | ||
170 | |||
171 | /** | ||
172 | * Write the banned IPs to a file | ||
173 | */ | ||
174 | protected function writeBanFile() | ||
175 | { | ||
176 | if (! array_key_exists('IPBANS', $this->globals)) { | ||
177 | return; | ||
178 | } | ||
179 | file_put_contents( | ||
180 | $this->banFile, | ||
181 | "<?php\n\$GLOBALS['IPBANS']=" . var_export($this->globals['IPBANS'], true) . ";\n?>" | ||
182 | ); | ||
183 | } | ||
184 | |||
185 | /** | ||
186 | * Handle a failed login and ban the IP after too many failed attempts | 165 | * Handle a failed login and ban the IP after too many failed attempts |
187 | * | 166 | * |
188 | * @param array $server The $_SERVER array | 167 | * @param array $server The $_SERVER array |
189 | */ | 168 | */ |
190 | public function handleFailedLogin($server) | 169 | public function handleFailedLogin($server) |
191 | { | 170 | { |
192 | $ip = $server['REMOTE_ADDR']; | 171 | $this->banManager->handleFailedAttempt($server); |
193 | $trusted = $this->configManager->get('security.trusted_proxies', []); | ||
194 | |||
195 | if (in_array($ip, $trusted)) { | ||
196 | $ip = getIpAddressFromProxy($server, $trusted); | ||
197 | if (! $ip) { | ||
198 | // the IP is behind a trusted forward proxy, but is not forwarded | ||
199 | // in the HTTP headers, so we do nothing | ||
200 | return; | ||
201 | } | ||
202 | } | ||
203 | |||
204 | // increment the fail count for this IP | ||
205 | if (isset($this->globals['IPBANS']['FAILURES'][$ip])) { | ||
206 | $this->globals['IPBANS']['FAILURES'][$ip]++; | ||
207 | } else { | ||
208 | $this->globals['IPBANS']['FAILURES'][$ip] = 1; | ||
209 | } | ||
210 | |||
211 | if ($this->globals['IPBANS']['FAILURES'][$ip] >= $this->configManager->get('security.ban_after')) { | ||
212 | $this->globals['IPBANS']['BANS'][$ip] = time() + $this->configManager->get('security.ban_duration', 1800); | ||
213 | logm( | ||
214 | $this->configManager->get('resource.log'), | ||
215 | $server['REMOTE_ADDR'], | ||
216 | 'IP address banned from login' | ||
217 | ); | ||
218 | } | ||
219 | $this->writeBanFile(); | ||
220 | } | 172 | } |
221 | 173 | ||
222 | /** | 174 | /** |
@@ -226,13 +178,7 @@ class LoginManager | |||
226 | */ | 178 | */ |
227 | public function handleSuccessfulLogin($server) | 179 | public function handleSuccessfulLogin($server) |
228 | { | 180 | { |
229 | $ip = $server['REMOTE_ADDR']; | 181 | $this->banManager->clearFailures($server); |
230 | // FIXME unban when behind a trusted proxy? | ||
231 | |||
232 | unset($this->globals['IPBANS']['FAILURES'][$ip]); | ||
233 | unset($this->globals['IPBANS']['BANS'][$ip]); | ||
234 | |||
235 | $this->writeBanFile(); | ||
236 | } | 182 | } |
237 | 183 | ||
238 | /** | 184 | /** |
@@ -244,24 +190,6 @@ class LoginManager | |||
244 | */ | 190 | */ |
245 | public function canLogin($server) | 191 | public function canLogin($server) |
246 | { | 192 | { |
247 | $ip = $server['REMOTE_ADDR']; | 193 | return ! $this->banManager->isBanned($server); |
248 | |||
249 | if (! isset($this->globals['IPBANS']['BANS'][$ip])) { | ||
250 | // the user is not banned | ||
251 | return true; | ||
252 | } | ||
253 | |||
254 | if ($this->globals['IPBANS']['BANS'][$ip] > time()) { | ||
255 | // the user is still banned | ||
256 | return false; | ||
257 | } | ||
258 | |||
259 | // the ban has expired, the user can attempt to log in again | ||
260 | logm($this->configManager->get('resource.log'), $server['REMOTE_ADDR'], 'Ban lifted.'); | ||
261 | unset($this->globals['IPBANS']['FAILURES'][$ip]); | ||
262 | unset($this->globals['IPBANS']['BANS'][$ip]); | ||
263 | |||
264 | $this->writeBanFile(); | ||
265 | return true; | ||
266 | } | 194 | } |
267 | } | 195 | } |
diff --git a/doc/md/Server-configuration.md b/doc/md/Server-configuration.md index 4753df1d..88eed8e6 100644 --- a/doc/md/Server-configuration.md +++ b/doc/md/Server-configuration.md | |||
@@ -404,6 +404,8 @@ If Shaarli is served behind a proxy (i.e. there is a proxy server between client | |||
404 | - `X-Forwarded-Host` | 404 | - `X-Forwarded-Host` |
405 | - `X-Forwarded-For` | 405 | - `X-Forwarded-For` |
406 | 406 | ||
407 | In you [Shaarli configuration](Shaarli-configuration) `data/config.json.php`, add the public IP of your proxy under `security.trusted_proxies`. | ||
408 | |||
407 | See also [proxy-related](https://github.com/shaarli/Shaarli/issues?utf8=%E2%9C%93&q=label%3Aproxy+) issues. | 409 | See also [proxy-related](https://github.com/shaarli/Shaarli/issues?utf8=%E2%9C%93&q=label%3Aproxy+) issues. |
408 | 410 | ||
409 | ## Robots and crawlers | 411 | ## Robots and crawlers |
@@ -125,7 +125,7 @@ if (isset($_COOKIE['shaarli']) && !SessionManager::checkId($_COOKIE['shaarli'])) | |||
125 | 125 | ||
126 | $conf = new ConfigManager(); | 126 | $conf = new ConfigManager(); |
127 | $sessionManager = new SessionManager($_SESSION, $conf); | 127 | $sessionManager = new SessionManager($_SESSION, $conf); |
128 | $loginManager = new LoginManager($GLOBALS, $conf, $sessionManager); | 128 | $loginManager = new LoginManager($conf, $sessionManager); |
129 | $loginManager->generateStaySignedInToken($_SERVER['REMOTE_ADDR']); | 129 | $loginManager->generateStaySignedInToken($_SERVER['REMOTE_ADDR']); |
130 | $clientIpId = client_ip_id($_SERVER); | 130 | $clientIpId = client_ip_id($_SERVER); |
131 | 131 | ||
diff --git a/tests/security/BanManagerTest.php b/tests/security/BanManagerTest.php new file mode 100644 index 00000000..bba7c8ad --- /dev/null +++ b/tests/security/BanManagerTest.php | |||
@@ -0,0 +1,393 @@ | |||
1 | <?php | ||
2 | |||
3 | |||
4 | namespace Shaarli\Security; | ||
5 | |||
6 | use PHPUnit\Framework\TestCase; | ||
7 | use Shaarli\FileUtils; | ||
8 | |||
9 | /** | ||
10 | * Test coverage for BanManager | ||
11 | */ | ||
12 | class BanManagerTest extends TestCase | ||
13 | { | ||
14 | /** @var BanManager Ban Manager instance */ | ||
15 | protected $banManager; | ||
16 | |||
17 | /** @var string Banned IP filename */ | ||
18 | protected $banFile = 'sandbox/ipbans.php'; | ||
19 | |||
20 | /** @var string Log filename */ | ||
21 | protected $logFile = 'sandbox/shaarli.log'; | ||
22 | |||
23 | /** @var string Local client IP address */ | ||
24 | protected $ipAddr = '127.0.0.1'; | ||
25 | |||
26 | /** @var string Trusted proxy IP address */ | ||
27 | protected $trustedProxy = '10.1.1.100'; | ||
28 | |||
29 | /** @var array Simulates the $_SERVER array */ | ||
30 | protected $server = []; | ||
31 | |||
32 | /** | ||
33 | * Prepare or reset test resources | ||
34 | */ | ||
35 | public function setUp() | ||
36 | { | ||
37 | if (file_exists($this->banFile)) { | ||
38 | unlink($this->banFile); | ||
39 | } | ||
40 | |||
41 | $this->banManager = $this->getNewBanManagerInstance(); | ||
42 | $this->server['REMOTE_ADDR'] = $this->ipAddr; | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * Test constructor with initial file. | ||
47 | */ | ||
48 | public function testInstantiateFromFile() | ||
49 | { | ||
50 | $time = time() + 10; | ||
51 | FileUtils::writeFlatDB( | ||
52 | $this->banFile, | ||
53 | [ | ||
54 | 'failures' => [ | ||
55 | $this->ipAddr => 2, | ||
56 | $ip = '1.2.3.4' => 1, | ||
57 | ], | ||
58 | 'bans' => [ | ||
59 | $ip2 = '8.8.8.8' => $time, | ||
60 | $ip3 = '1.1.1.1' => $time + 1, | ||
61 | ], | ||
62 | ] | ||
63 | ); | ||
64 | $this->banManager = $this->getNewBanManagerInstance(); | ||
65 | |||
66 | $this->assertCount(2, $this->banManager->getFailures()); | ||
67 | $this->assertEquals(2, $this->banManager->getFailures()[$this->ipAddr]); | ||
68 | $this->assertEquals(1, $this->banManager->getFailures()[$ip]); | ||
69 | $this->assertCount(2, $this->banManager->getBans()); | ||
70 | $this->assertEquals($time, $this->banManager->getBans()[$ip2]); | ||
71 | $this->assertEquals($time + 1, $this->banManager->getBans()[$ip3]); | ||
72 | } | ||
73 | |||
74 | /** | ||
75 | * Test constructor with initial file with invalid values | ||
76 | */ | ||
77 | public function testInstantiateFromCrappyFile() | ||
78 | { | ||
79 | FileUtils::writeFlatDB($this->banFile, 'plop'); | ||
80 | $this->banManager = $this->getNewBanManagerInstance(); | ||
81 | |||
82 | $this->assertEquals([], $this->banManager->getFailures()); | ||
83 | $this->assertEquals([], $this->banManager->getBans()); | ||
84 | } | ||
85 | |||
86 | /** | ||
87 | * Test failed attempt with a direct IP. | ||
88 | */ | ||
89 | public function testHandleFailedAttempt() | ||
90 | { | ||
91 | $this->assertCount(0, $this->banManager->getFailures()); | ||
92 | |||
93 | $this->banManager->handleFailedAttempt($this->server); | ||
94 | $this->assertCount(1, $this->banManager->getFailures()); | ||
95 | $this->assertEquals(1, $this->banManager->getFailures()[$this->ipAddr]); | ||
96 | |||
97 | $this->banManager->handleFailedAttempt($this->server); | ||
98 | $this->assertCount(1, $this->banManager->getFailures()); | ||
99 | $this->assertEquals(2, $this->banManager->getFailures()[$this->ipAddr]); | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * Test failed attempt behind a trusted proxy IP (with proper IP forwarding). | ||
104 | */ | ||
105 | public function testHandleFailedAttemptBehingProxy() | ||
106 | { | ||
107 | $server = [ | ||
108 | 'REMOTE_ADDR' => $this->trustedProxy, | ||
109 | 'HTTP_X_FORWARDED_FOR' => $this->ipAddr, | ||
110 | ]; | ||
111 | $this->assertCount(0, $this->banManager->getFailures()); | ||
112 | |||
113 | $this->banManager->handleFailedAttempt($server); | ||
114 | $this->assertCount(1, $this->banManager->getFailures()); | ||
115 | $this->assertEquals(1, $this->banManager->getFailures()[$this->ipAddr]); | ||
116 | |||
117 | $this->banManager->handleFailedAttempt($server); | ||
118 | $this->assertCount(1, $this->banManager->getFailures()); | ||
119 | $this->assertEquals(2, $this->banManager->getFailures()[$this->ipAddr]); | ||
120 | } | ||
121 | |||
122 | /** | ||
123 | * Test failed attempt behind a trusted proxy IP but without IP forwarding. | ||
124 | */ | ||
125 | public function testHandleFailedAttemptBehindNotConfiguredProxy() | ||
126 | { | ||
127 | $server = [ | ||
128 | 'REMOTE_ADDR' => $this->trustedProxy, | ||
129 | ]; | ||
130 | $this->assertCount(0, $this->banManager->getFailures()); | ||
131 | |||
132 | $this->banManager->handleFailedAttempt($server); | ||
133 | $this->assertCount(0, $this->banManager->getFailures()); | ||
134 | |||
135 | $this->banManager->handleFailedAttempt($server); | ||
136 | $this->assertCount(0, $this->banManager->getFailures()); | ||
137 | } | ||
138 | |||
139 | /** | ||
140 | * Test failed attempts with multiple direct IP. | ||
141 | */ | ||
142 | public function testHandleFailedAttemptMultipleIp() | ||
143 | { | ||
144 | $this->assertCount(0, $this->banManager->getFailures()); | ||
145 | $this->banManager->handleFailedAttempt($this->server); | ||
146 | $this->server['REMOTE_ADDR'] = '1.2.3.4'; | ||
147 | $this->banManager->handleFailedAttempt($this->server); | ||
148 | $this->banManager->handleFailedAttempt($this->server); | ||
149 | $this->assertCount(2, $this->banManager->getFailures()); | ||
150 | $this->assertEquals(1, $this->banManager->getFailures()[$this->ipAddr]); | ||
151 | $this->assertEquals(2, $this->banManager->getFailures()[$this->server['REMOTE_ADDR']]); | ||
152 | } | ||
153 | |||
154 | /** | ||
155 | * Test clear failure for provided IP without any additional data. | ||
156 | */ | ||
157 | public function testClearFailuresEmpty() | ||
158 | { | ||
159 | $this->assertCount(0, $this->banManager->getFailures()); | ||
160 | $this->banManager->clearFailures($this->server); | ||
161 | $this->assertCount(0, $this->banManager->getFailures()); | ||
162 | } | ||
163 | |||
164 | /** | ||
165 | * Test clear failure for provided IP with failed attempts. | ||
166 | */ | ||
167 | public function testClearFailuresFromFile() | ||
168 | { | ||
169 | FileUtils::writeFlatDB( | ||
170 | $this->banFile, | ||
171 | [ | ||
172 | 'failures' => [ | ||
173 | $this->ipAddr => 2, | ||
174 | $ip = '1.2.3.4' => 1, | ||
175 | ] | ||
176 | ] | ||
177 | ); | ||
178 | $this->banManager = $this->getNewBanManagerInstance(); | ||
179 | |||
180 | $this->assertCount(2, $this->banManager->getFailures()); | ||
181 | $this->banManager->clearFailures($this->server); | ||
182 | $this->assertCount(1, $this->banManager->getFailures()); | ||
183 | $this->assertEquals(1, $this->banManager->getFailures()[$ip]); | ||
184 | } | ||
185 | |||
186 | /** | ||
187 | * Test clear failure for provided IP with failed attempts, behind a reverse proxy. | ||
188 | */ | ||
189 | public function testClearFailuresFromFileBehindProxy() | ||
190 | { | ||
191 | $server = [ | ||
192 | 'REMOTE_ADDR' => $this->trustedProxy, | ||
193 | 'HTTP_X_FORWARDED_FOR' => $this->ipAddr, | ||
194 | ]; | ||
195 | |||
196 | FileUtils::writeFlatDB( | ||
197 | $this->banFile, | ||
198 | [ | ||
199 | 'failures' => [ | ||
200 | $this->ipAddr => 2, | ||
201 | $ip = '1.2.3.4' => 1, | ||
202 | ] | ||
203 | ] | ||
204 | ); | ||
205 | $this->banManager = $this->getNewBanManagerInstance(); | ||
206 | |||
207 | $this->assertCount(2, $this->banManager->getFailures()); | ||
208 | $this->banManager->clearFailures($server); | ||
209 | $this->assertCount(1, $this->banManager->getFailures()); | ||
210 | $this->assertEquals(1, $this->banManager->getFailures()[$ip]); | ||
211 | } | ||
212 | |||
213 | /** | ||
214 | * Test clear failure for provided IP with failed attempts, | ||
215 | * behind a reverse proxy without forwarding. | ||
216 | */ | ||
217 | public function testClearFailuresFromFileBehindNotConfiguredProxy() | ||
218 | { | ||
219 | $server = [ | ||
220 | 'REMOTE_ADDR' => $this->trustedProxy, | ||
221 | ]; | ||
222 | |||
223 | FileUtils::writeFlatDB( | ||
224 | $this->banFile, | ||
225 | [ | ||
226 | 'failures' => [ | ||
227 | $this->ipAddr => 2, | ||
228 | $ip = '1.2.3.4' => 1, | ||
229 | ] | ||
230 | ] | ||
231 | ); | ||
232 | $this->banManager = $this->getNewBanManagerInstance(); | ||
233 | |||
234 | $this->assertCount(2, $this->banManager->getFailures()); | ||
235 | $this->banManager->clearFailures($server); | ||
236 | $this->assertCount(2, $this->banManager->getFailures()); | ||
237 | } | ||
238 | |||
239 | /** | ||
240 | * Test isBanned without any data | ||
241 | */ | ||
242 | public function testIsBannedEmpty() | ||
243 | { | ||
244 | $this->assertFalse($this->banManager->isBanned($this->server)); | ||
245 | } | ||
246 | |||
247 | /** | ||
248 | * Test isBanned with banned IP from file data | ||
249 | */ | ||
250 | public function testBannedFromFile() | ||
251 | { | ||
252 | FileUtils::writeFlatDB( | ||
253 | $this->banFile, | ||
254 | [ | ||
255 | 'bans' => [ | ||
256 | $this->ipAddr => time() + 10, | ||
257 | ] | ||
258 | ] | ||
259 | ); | ||
260 | $this->banManager = $this->getNewBanManagerInstance(); | ||
261 | |||
262 | $this->assertCount(1, $this->banManager->getBans()); | ||
263 | $this->assertTrue($this->banManager->isBanned($this->server)); | ||
264 | } | ||
265 | |||
266 | /** | ||
267 | * Test isBanned with banned IP from file data behind a reverse proxy | ||
268 | */ | ||
269 | public function testBannedFromFileBehindProxy() | ||
270 | { | ||
271 | $server = [ | ||
272 | 'REMOTE_ADDR' => $this->trustedProxy, | ||
273 | 'HTTP_X_FORWARDED_FOR' => $this->ipAddr, | ||
274 | ]; | ||
275 | FileUtils::writeFlatDB( | ||
276 | $this->banFile, | ||
277 | [ | ||
278 | 'bans' => [ | ||
279 | $this->ipAddr => time() + 10, | ||
280 | ] | ||
281 | ] | ||
282 | ); | ||
283 | $this->banManager = $this->getNewBanManagerInstance(); | ||
284 | |||
285 | $this->assertCount(1, $this->banManager->getBans()); | ||
286 | $this->assertTrue($this->banManager->isBanned($server)); | ||
287 | } | ||
288 | |||
289 | /** | ||
290 | * Test isBanned with banned IP from file data behind a reverse proxy, | ||
291 | * without IP forwarding | ||
292 | */ | ||
293 | public function testBannedFromFileBehindNotConfiguredProxy() | ||
294 | { | ||
295 | $server = [ | ||
296 | 'REMOTE_ADDR' => $this->trustedProxy, | ||
297 | ]; | ||
298 | FileUtils::writeFlatDB( | ||
299 | $this->banFile, | ||
300 | [ | ||
301 | 'bans' => [ | ||
302 | $this->ipAddr => time() + 10, | ||
303 | ] | ||
304 | ] | ||
305 | ); | ||
306 | $this->banManager = $this->getNewBanManagerInstance(); | ||
307 | |||
308 | $this->assertCount(1, $this->banManager->getBans()); | ||
309 | $this->assertFalse($this->banManager->isBanned($server)); | ||
310 | } | ||
311 | |||
312 | /** | ||
313 | * Test isBanned with an expired ban | ||
314 | */ | ||
315 | public function testLiftBan() | ||
316 | { | ||
317 | FileUtils::writeFlatDB( | ||
318 | $this->banFile, | ||
319 | [ | ||
320 | 'bans' => [ | ||
321 | $this->ipAddr => time() - 10, | ||
322 | ] | ||
323 | ] | ||
324 | ); | ||
325 | $this->banManager = $this->getNewBanManagerInstance(); | ||
326 | |||
327 | $this->assertCount(1, $this->banManager->getBans()); | ||
328 | $this->assertFalse($this->banManager->isBanned($this->server)); | ||
329 | } | ||
330 | |||
331 | /** | ||
332 | * Test isBanned with an expired ban behind a reverse proxy | ||
333 | */ | ||
334 | public function testLiftBanBehindProxy() | ||
335 | { | ||
336 | $server = [ | ||
337 | 'REMOTE_ADDR' => $this->trustedProxy, | ||
338 | 'HTTP_X_FORWARDED_FOR' => $this->ipAddr, | ||
339 | ]; | ||
340 | |||
341 | FileUtils::writeFlatDB( | ||
342 | $this->banFile, | ||
343 | [ | ||
344 | 'bans' => [ | ||
345 | $this->ipAddr => time() - 10, | ||
346 | ] | ||
347 | ] | ||
348 | ); | ||
349 | $this->banManager = $this->getNewBanManagerInstance(); | ||
350 | |||
351 | $this->assertCount(1, $this->banManager->getBans()); | ||
352 | $this->assertFalse($this->banManager->isBanned($server)); | ||
353 | } | ||
354 | |||
355 | /** | ||
356 | * Test isBanned with an expired ban behind a reverse proxy | ||
357 | */ | ||
358 | public function testLiftBanBehindNotConfiguredProxy() | ||
359 | { | ||
360 | $server = [ | ||
361 | 'REMOTE_ADDR' => $this->trustedProxy, | ||
362 | ]; | ||
363 | |||
364 | FileUtils::writeFlatDB( | ||
365 | $this->banFile, | ||
366 | [ | ||
367 | 'bans' => [ | ||
368 | $this->ipAddr => time() - 10, | ||
369 | ] | ||
370 | ] | ||
371 | ); | ||
372 | $this->banManager = $this->getNewBanManagerInstance(); | ||
373 | |||
374 | $this->assertCount(1, $this->banManager->getBans()); | ||
375 | $this->assertFalse($this->banManager->isBanned($server)); | ||
376 | } | ||
377 | |||
378 | /** | ||
379 | * Build a new instance of BanManager, which will reread the ban file. | ||
380 | * | ||
381 | * @return BanManager instance | ||
382 | */ | ||
383 | protected function getNewBanManagerInstance() | ||
384 | { | ||
385 | return new BanManager( | ||
386 | [$this->trustedProxy], | ||
387 | 3, | ||
388 | 1800, | ||
389 | $this->banFile, | ||
390 | $this->logFile | ||
391 | ); | ||
392 | } | ||
393 | } | ||
diff --git a/tests/security/LoginManagerTest.php b/tests/security/LoginManagerTest.php index 7b0262b3..eef0f22a 100644 --- a/tests/security/LoginManagerTest.php +++ b/tests/security/LoginManagerTest.php | |||
@@ -75,54 +75,27 @@ class LoginManagerTest extends TestCase | |||
75 | 'credentials.salt' => $this->salt, | 75 | 'credentials.salt' => $this->salt, |
76 | 'resource.ban_file' => $this->banFile, | 76 | 'resource.ban_file' => $this->banFile, |
77 | 'resource.log' => $this->logFile, | 77 | 'resource.log' => $this->logFile, |
78 | 'security.ban_after' => 4, | 78 | 'security.ban_after' => 2, |
79 | 'security.ban_duration' => 3600, | 79 | 'security.ban_duration' => 3600, |
80 | 'security.trusted_proxies' => [$this->trustedProxy], | 80 | 'security.trusted_proxies' => [$this->trustedProxy], |
81 | ]); | 81 | ]); |
82 | 82 | ||
83 | $this->cookie = []; | 83 | $this->cookie = []; |
84 | |||
85 | $this->globals = &$GLOBALS; | ||
86 | unset($this->globals['IPBANS']); | ||
87 | |||
88 | $this->session = []; | 84 | $this->session = []; |
89 | 85 | ||
90 | $this->sessionManager = new SessionManager($this->session, $this->configManager); | 86 | $this->sessionManager = new SessionManager($this->session, $this->configManager); |
91 | $this->loginManager = new LoginManager($this->globals, $this->configManager, $this->sessionManager); | 87 | $this->loginManager = new LoginManager($this->configManager, $this->sessionManager); |
92 | $this->server['REMOTE_ADDR'] = $this->ipAddr; | 88 | $this->server['REMOTE_ADDR'] = $this->ipAddr; |
93 | } | 89 | } |
94 | 90 | ||
95 | /** | 91 | /** |
96 | * Wipe test resources | ||
97 | */ | ||
98 | public function tearDown() | ||
99 | { | ||
100 | unset($this->globals['IPBANS']); | ||
101 | } | ||
102 | |||
103 | /** | ||
104 | * Instantiate a LoginManager and load ban records | ||
105 | */ | ||
106 | public function testReadBanFile() | ||
107 | { | ||
108 | file_put_contents( | ||
109 | $this->banFile, | ||
110 | "<?php\n\$GLOBALS['IPBANS']=array('FAILURES' => array('127.0.0.1' => 99));\n?>" | ||
111 | ); | ||
112 | new LoginManager($this->globals, $this->configManager, null); | ||
113 | $this->assertEquals(99, $this->globals['IPBANS']['FAILURES']['127.0.0.1']); | ||
114 | } | ||
115 | |||
116 | /** | ||
117 | * Record a failed login attempt | 92 | * Record a failed login attempt |
118 | */ | 93 | */ |
119 | public function testHandleFailedLogin() | 94 | public function testHandleFailedLogin() |
120 | { | 95 | { |
121 | $this->loginManager->handleFailedLogin($this->server); | 96 | $this->loginManager->handleFailedLogin($this->server); |
122 | $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
123 | |||
124 | $this->loginManager->handleFailedLogin($this->server); | 97 | $this->loginManager->handleFailedLogin($this->server); |
125 | $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | 98 | $this->assertFalse($this->loginManager->canLogin($this->server)); |
126 | } | 99 | } |
127 | 100 | ||
128 | /** | 101 | /** |
@@ -135,10 +108,8 @@ class LoginManagerTest extends TestCase | |||
135 | 'HTTP_X_FORWARDED_FOR' => $this->ipAddr, | 108 | 'HTTP_X_FORWARDED_FOR' => $this->ipAddr, |
136 | ]; | 109 | ]; |
137 | $this->loginManager->handleFailedLogin($server); | 110 | $this->loginManager->handleFailedLogin($server); |
138 | $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
139 | |||
140 | $this->loginManager->handleFailedLogin($server); | 111 | $this->loginManager->handleFailedLogin($server); |
141 | $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | 112 | $this->assertFalse($this->loginManager->canLogin($server)); |
142 | } | 113 | } |
143 | 114 | ||
144 | /** | 115 | /** |
@@ -150,39 +121,8 @@ class LoginManagerTest extends TestCase | |||
150 | 'REMOTE_ADDR' => $this->trustedProxy, | 121 | 'REMOTE_ADDR' => $this->trustedProxy, |
151 | ]; | 122 | ]; |
152 | $this->loginManager->handleFailedLogin($server); | 123 | $this->loginManager->handleFailedLogin($server); |
153 | $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr])); | ||
154 | |||
155 | $this->loginManager->handleFailedLogin($server); | 124 | $this->loginManager->handleFailedLogin($server); |
156 | $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr])); | 125 | $this->assertTrue($this->loginManager->canLogin($server)); |
157 | } | ||
158 | |||
159 | /** | ||
160 | * Record a failed login attempt and ban the IP after too many failures | ||
161 | */ | ||
162 | public function testHandleFailedLoginBanIp() | ||
163 | { | ||
164 | $this->loginManager->handleFailedLogin($this->server); | ||
165 | $this->assertEquals(1, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
166 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
167 | |||
168 | $this->loginManager->handleFailedLogin($this->server); | ||
169 | $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
170 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
171 | |||
172 | $this->loginManager->handleFailedLogin($this->server); | ||
173 | $this->assertEquals(3, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
174 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
175 | |||
176 | $this->loginManager->handleFailedLogin($this->server); | ||
177 | $this->assertEquals(4, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
178 | $this->assertFalse($this->loginManager->canLogin($this->server)); | ||
179 | |||
180 | // handleFailedLogin is not supposed to be called at this point: | ||
181 | // - no login form should be displayed once an IP has been banned | ||
182 | // - yet this could happen when using custom templates / scripts | ||
183 | $this->loginManager->handleFailedLogin($this->server); | ||
184 | $this->assertEquals(5, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
185 | $this->assertFalse($this->loginManager->canLogin($this->server)); | ||
186 | } | 126 | } |
187 | 127 | ||
188 | /** | 128 | /** |
@@ -202,14 +142,11 @@ class LoginManagerTest extends TestCase | |||
202 | public function testHandleSuccessfulLoginAfterFailure() | 142 | public function testHandleSuccessfulLoginAfterFailure() |
203 | { | 143 | { |
204 | $this->loginManager->handleFailedLogin($this->server); | 144 | $this->loginManager->handleFailedLogin($this->server); |
205 | $this->loginManager->handleFailedLogin($this->server); | ||
206 | $this->assertEquals(2, $this->globals['IPBANS']['FAILURES'][$this->ipAddr]); | ||
207 | $this->assertTrue($this->loginManager->canLogin($this->server)); | 145 | $this->assertTrue($this->loginManager->canLogin($this->server)); |
208 | 146 | ||
209 | $this->loginManager->handleSuccessfulLogin($this->server); | 147 | $this->loginManager->handleSuccessfulLogin($this->server); |
148 | $this->loginManager->handleFailedLogin($this->server); | ||
210 | $this->assertTrue($this->loginManager->canLogin($this->server)); | 149 | $this->assertTrue($this->loginManager->canLogin($this->server)); |
211 | $this->assertFalse(isset($this->globals['IPBANS']['FAILURES'][$this->ipAddr])); | ||
212 | $this->assertFalse(isset($this->globals['IPBANS']['BANS'][$this->ipAddr])); | ||
213 | } | 150 | } |
214 | 151 | ||
215 | /** | 152 | /** |
@@ -221,33 +158,6 @@ class LoginManagerTest extends TestCase | |||
221 | } | 158 | } |
222 | 159 | ||
223 | /** | 160 | /** |
224 | * The IP is banned | ||
225 | */ | ||
226 | public function testCanLoginIpBanned() | ||
227 | { | ||
228 | // ban the IP for an hour | ||
229 | $this->globals['IPBANS']['FAILURES'][$this->ipAddr] = 10; | ||
230 | $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() + 3600; | ||
231 | |||
232 | $this->assertFalse($this->loginManager->canLogin($this->server)); | ||
233 | } | ||
234 | |||
235 | /** | ||
236 | * The IP is banned, and the ban duration is over | ||
237 | */ | ||
238 | public function testCanLoginIpBanExpired() | ||
239 | { | ||
240 | // ban the IP for an hour | ||
241 | $this->globals['IPBANS']['FAILURES'][$this->ipAddr] = 10; | ||
242 | $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() + 3600; | ||
243 | $this->assertFalse($this->loginManager->canLogin($this->server)); | ||
244 | |||
245 | // lift the ban | ||
246 | $this->globals['IPBANS']['BANS'][$this->ipAddr] = time() - 3600; | ||
247 | $this->assertTrue($this->loginManager->canLogin($this->server)); | ||
248 | } | ||
249 | |||
250 | /** | ||
251 | * Generate a token depending on the user credentials and client IP | 161 | * Generate a token depending on the user credentials and client IP |
252 | */ | 162 | */ |
253 | public function testGenerateStaySignedInToken() | 163 | public function testGenerateStaySignedInToken() |
@@ -282,7 +192,7 @@ class LoginManagerTest extends TestCase | |||
282 | $configManager = new \FakeConfigManager([ | 192 | $configManager = new \FakeConfigManager([ |
283 | 'resource.ban_file' => $this->banFile, | 193 | 'resource.ban_file' => $this->banFile, |
284 | ]); | 194 | ]); |
285 | $loginManager = new LoginManager($this->globals, $configManager, null); | 195 | $loginManager = new LoginManager($configManager, null); |
286 | $loginManager->checkLoginState([], ''); | 196 | $loginManager->checkLoginState([], ''); |
287 | 197 | ||
288 | $this->assertFalse($loginManager->isLoggedIn()); | 198 | $this->assertFalse($loginManager->isLoggedIn()); |