Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getClientIp() gives server IP or Random IP rather than visitor IP #37023

Closed
a21ns1g4ts opened this issue May 31, 2020 · 3 comments
Closed

getClientIp() gives server IP or Random IP rather than visitor IP #37023

a21ns1g4ts opened this issue May 31, 2020 · 3 comments

Comments

@a21ns1g4ts
Copy link

a21ns1g4ts commented May 31, 2020

Symfony version(s) affected: 5.0

Description
I'm trying to track IP addresses of visitors. When using getClientIp(), I get the server's IP address rather than the visitor's. I tried this on multiple machines at multiple locations and they all resulted in the exact same IP.
I have look at the follow line:
https://github.com/symfony/http-foundation/blob/0553e8b8cb3a48d6b84a91968871e6e14d5602a4/Request.php#L798

public function getClientIps()
{
   $ip = $this->server->get('REMOTE_ADDR');

   if (!$this->isFromTrustedProxy()) {
       return [$ip];
  }

  return $this->getTrustedValues(self::HEADER_X_FORWARDED_FOR, $ip) ?: [$ip];
}

How to reproduce
I have noted this behavior in my network. But i have the problems with this method in heroku. The results ip's are random in some cases. I have used https://ngrok.com/ for local tests. I have a visitor tracker made with on rails and it works for all network and all cases.
I have tested in my heroku server and i have catch the results:
Different ips for same client.
image

In my ruby application running in heroku always it works:

image

Possible Solution

👀
Additional context
Test locally with ngrock

public function getClientIps()
{
  $ip = $this->server->get('REMOTE_ADDR');

   // if (!$this->isFromTrustedProxy()) {
   //   return [$ip];
   // }
      var_dump($this->getTrustedValues(self::HEADER_X_FORWARDED_FOR, $ip))
      return $this->getTrustedValues(self::HEADER_X_FORWARDED_FOR, $ip) ?: [$ip];
}

Output

^ array:2 [▼
  0 => "127.0.0.1"
  1 => "177.66.107.164"
]
@xabbuh
Copy link
Member

xabbuh commented May 31, 2020

Well, if your web server is behind a proxy server, you will indeed have to configure the trusted proxies to retrieve the real client IP address.

@xabbuh
Copy link
Member

xabbuh commented May 31, 2020

@a21ns1g4ts
Copy link
Author

a21ns1g4ts commented May 31, 2020

yep, but a lot of symphony dependents don't know this behavior. I have used several http kernels and only symphony has this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants