Skip to content

Commit

Permalink
Fixed issue #14043: Improvement in IP blocking after failed login att…
Browse files Browse the repository at this point in the history
…empts

* Improved blocking of failed login-attempts

Record new attempts only when IP is not already blocked. This prevents endless blocking if user occasionally tries to login again.

* Reset failed login counter after sucessful login

and remove a line of dead code
  • Loading branch information
weberhofer authored and c-schmitz committed Nov 19, 2019
1 parent c6e53e1 commit 1bd2f1b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
3 changes: 2 additions & 1 deletion application/core/LSUserIdentity.php
Expand Up @@ -82,9 +82,10 @@ public function authenticate()
// Perform postlogin
regenerateCSRFToken();
$this->postLogin();
// Reset counter after successful login
FailedLoginAttempt::model()->deleteAttempts();
} else {
// Log a failed attempt
$userHostAddress = getIPAddress();
FailedLoginAttempt::model()->addAttempt();
regenerateCSRFToken();
App()->session->regenerateID(); // Handled on login by Yii
Expand Down
33 changes: 17 additions & 16 deletions application/models/FailedLoginAttempt.php
Expand Up @@ -97,29 +97,30 @@ public function cleanOutOldAttempts()
}

/**
* Creates an attempt
* Records an failed login-attempt if IP is not already locked out
*
* @access public
* @return true
*/
public function addAttempt()
{
$timestamp = date("Y-m-d H:i:s");
$ip = substr(getIPAddress(), 0, 40);
$row = $this->findByAttributes(array('ip' => $ip));

if ($row !== null) {
$row->number_attempts = $row->number_attempts + 1;
$row->last_attempt = $timestamp;
$row->save();
} else {
$record = new FailedLoginAttempt;
$record->ip = $ip;
$record->number_attempts = 1;
$record->last_attempt = $timestamp;
$record->save();
if (!$this->isLockedOut()) {
$timestamp = date("Y-m-d H:i:s");
$ip = substr(getIPAddress(), 0, 40);
$row = $this->findByAttributes(array('ip' => $ip));

if ($row !== null) {
$row->number_attempts = $row->number_attempts + 1;
$row->last_attempt = $timestamp;
$row->save();
} else {
$record = new FailedLoginAttempt;
$record->ip = $ip;
$record->number_attempts = 1;
$record->last_attempt = $timestamp;
$record->save();
}
}

return true;
}
}

0 comments on commit 1bd2f1b

Please sign in to comment.