Skip to content

Commit

Permalink
Fixed issue: [security] Unsafe way to detect IP address against brute…
Browse files Browse the repository at this point in the history
…-force attacks
  • Loading branch information
c-schmitz committed May 30, 2023
1 parent 45c166e commit 6575099
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion application/extensions/LimeDebug/LimeDebug.php
Expand Up @@ -7,7 +7,7 @@ class LimeDebug extends CWidget
{
public function run()
{
if (YII_DEBUG && in_array(getIPAddress(), array("127.0.0.1","::1")))
if (YII_DEBUG && in_array(getRealIPAddress(), array("127.0.0.1","::1")))
{
$data = array(
'session' => $_SESSION,
Expand Down
16 changes: 16 additions & 0 deletions application/helpers/common_helper.php
Expand Up @@ -4610,6 +4610,22 @@ function getIPAddress()
return $sIPAddress;
}


/**
* This function returns the real IP address and should mainly be used for security sensitive purposes
* If you want to use the IP address for language detection or similar, use getIPAddress() instead
*
* @return string Client IP Address
*/
function getRealIPAddress()
{
$sIPAddress = '127.0.0.1';
if (!empty($_SERVER['REMOTE_ADDR']) && filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP) !== false) {
$sIPAddress = $_SERVER['REMOTE_ADDR'];
}
return $sIPAddress;
}

/**
* This function tries to find out a valid language code for the language of the browser used
* If it cannot find it it will return the default language from global settings
Expand Down
6 changes: 3 additions & 3 deletions application/models/FailedLoginAttempt.php
Expand Up @@ -63,7 +63,7 @@ public function tableName()
*/
public function deleteAttempts(string $attemptType = 'login')
{
$ip = substr(getIPAddress(), 0, 40);
$ip = substr(getRealIPAddress(), 0, 40);

if (Yii::app()->getConfig('DBVersion') <= 480) {
$this->deleteAllByAttributes(array('ip' => $ip));
Expand All @@ -82,7 +82,7 @@ public function deleteAttempts(string $attemptType = 'login')
public function isLockedOut(string $attemptType): bool
{
$isLockedOut = false;
$ip = substr(getIPAddress(), 0, 40);
$ip = substr(getRealIPAddress(), 0, 40);

// Return false if IP is whitelisted
if ($this->isWhitelisted($ip, $attemptType)) {
Expand Down Expand Up @@ -144,7 +144,7 @@ public function addAttempt($attemptType = self::TYPE_LOGIN)
{
if (!$this->isLockedOut($attemptType)) {
$timestamp = date("Y-m-d H:i:s");
$ip = substr(getIPAddress(), 0, 40);
$ip = substr(getRealIPAddress(), 0, 40);

if (Yii::app()->getConfig('DBVersion') <= 480) {
$row = $this->findByAttributes(array('ip' => $ip));
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/models/FailedLoginAttemptTest.php
Expand Up @@ -9,7 +9,7 @@ class FailedLoginAttempTest extends TestBaseClass
public function testAddDeleteAttemp()
{
// Save Ip
$ip = substr(getIPAddress(), 0, 40);
$ip = substr(getRealIPAddress(), 0, 40);

// Make sure there are no records for the ip
FailedLoginAttempt::model()->deleteAttempts(FailedLoginAttempt::TYPE_LOGIN);
Expand Down

0 comments on commit 6575099

Please sign in to comment.