Problem/Motivation

Currently the below code displays the ip address as 127.0.0.1.
$this->logger('user')->notice('Login attempt failed from %ip.', ['%ip' => $this->getRequest()->getClientIp()]);

How to find the exact ip address, tried out to get the ip address from the $_SERVER which provides the ip address as 127.0.0.1

Steps to reproduce

When tried to login from user/login with invalid credentials, then on recent logs able to see the below message.

Login attempt failed from 127.0.0.1.

Proposed resolution

Need to get the exact ip address like the value we get from https://whatismyipaddress.com/

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Comments

sarikak created an issue. See original summary.

cilefen’s picture

Category: Bug report » Support request
Issue tags: -client ip address find ipaddress

Is there a load balancer or forward cache? What is the PHP and web server integration? You have probably missed a setup step documented in the default settings.php.

This is not a bug.

sarikak’s picture

@cilefen I configured new drupal 10 set up in localhost in xampp.

There is no load balancer or forward cache.

Please find the details:

Drupal version : 10.3.5
DB driver : mysql
DB hostname : localhost
DB port : 3306
DB username : root
DB name : d10
Database : Connected
Drupal bootstrap : Successful
Default theme : olivero
Admin theme : claro
PHP binary : C:/xampp/php/php.exe
PHP config : C:/xampp/php/php.ini
PHP OS : WINNT
PHP version : 8.1.6
Drush script : C:/xampp/htdocs/d10/vendor/drush/drush/drush
Drush version : 12.5.3.0
Drush temp : C:/Users/admin/AppData/Local/Temp
Drush configs : C:/xampp/htdocs/d10/vendor/drush/drush/drush.yml
Install profile : standard
Drupal root : C:/xampp/htdocs/d10/web
Site path : sites/default
Files, Public : sites/default/files
Files, Temp : C:\xampp\tmp

The settings.php code:

// Enable reverse proxy handling in Drupal.
$settings['reverse_proxy'] = TRUE;

// Specify the IP addresses of all your trusted reverse proxies.
// If you're unsure about the IPs, you may need to ask your hosting provider
// or use the CIDR range for your load balancer or CDN (e.g., Cloudflare, AWS).
$settings['reverse_proxy_addresses'] = [
  '127.0.0.1',      // For local development environments
  '***.**.*.*',  // Replace with your reverse proxy IP
  // Add more IP addresses if necessary
];

// Use the 'X-Forwarded-For' header to get the real client IP.
$settings['reverse_proxy_header'] = 'X-Forwarded-For';
sarikak’s picture

The PHP code snippet to get the current user's IP address also provides the value of 127.0.0.1
Added the below code in hook_form_alter function of user_login_form.

 // Check for shared internet or proxy server
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        // Check for IP address forwarded from a proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        // Use REMOTE_ADDR if no proxy or client IP is available
        $ip = $_SERVER['REMOTE_ADDR'];
    }

Tried with Login Event subscriber as well, but could not find any solution.

The below code from the Drupal core module, UserLoginForm.phpalso unaable to give the exact ip address but shows the 127.0.0.1.

// If the username entered is not a valid user,
          // only store the IP address.
          $this->logger('user')->notice('Login attempt failed from %ip.', ['%ip' => $this->getRequest()->getClientIp()]);

@cilefen Can you please suggest a way to get the ip address.

Appreciate any inputs.

Thanks

cilefen’s picture

If this is local development your IP address is 127.0.0.1 and this working as expected. Which IP address were you expecting?

My advice is to use DDEV instead of XAMPP.

mdsohaib4242’s picture

You can check the $_SERVER headers to see if the real IP address is passed via headers like X-Forwarded-For or HTTP_X_FORWARDED_FOR.
At last check for the REMOTE_ADDR if no valid IP was found

sarikak’s picture

I want this ip address rather than 127.0.0.1

ifconfig.co — What is my IP address?
165.225.***.***

This issue is raised from one of our client security team. Please find below are the issue details:

IP address-based blocking bypass via XForwarded-For

The vulnerability involves improper IP address handling in Drupal's brute force protection mechanism, leading to an IP address-based blocking bypass. This misconfiguration occurs because the X-Forwarded-For header is not correctly processed, allowing attackers to evade brute force protections.
Properly configure IP detection so that the real IP address is correctly parsed and used by the brute force protection mechanism

Note:- Currently this issue we are checking on local , once issue fixed we will check on the server.
We have checked with the $_server variable also we are getting same 127.0.0.1.

Is there anything we need to enable in server/apache level?

cilefen’s picture

Drupal is ready to be set up properly to parse forwarded-for headers. At this stage you may wish to consult XAMPP support or with a systems administrator to debug your specific setup.

Again, this is not a bug in Drupal.

quietone’s picture

@sarikak, The Drupal Core issue queue is not the ideal place for support requests. The 'support request' option is there for filing support issues for contributed modules and themes. There are several support options listed on our support page (Community > Support at the top of Drupal.org) and there is Drupal Slack. You may get better replies in one of those places.

quietone’s picture

Status: Active » Closed (works as designed)

Closing this based on #8