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

Return local IP when proxy IP detection fails #4

Merged
merged 1 commit into from
Jul 13, 2017

Conversation

shadowhand
Copy link
Member

Fixes #3

src/ClientIp.php Outdated
if ($ip = $this->getLocalIp($request)) {
if (!in_array($ip, $this->proxyIps)) {
return $ip;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored this because it was really hard for me to follow as written. Functionality remains the same, with better separation of concerns.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the functionality does not remain the same. The logic should be:

  • Get the ip using the default way (REMOTE_ADDR). Let's call it $localIp
  • If proxies hasn't been configured returns $localIp
  • If there's an array of allowed proxy ips, check if $localIp is in this array. If it's not, returns $localIp.
  • Get the new ip using the proxy headers and return it if it's found, or $localIp if doesn't.

In this refactoring $this->proxyIps is used to check the ip returned by $this->getProxyIp(), it should used to check the ip returned by $this->getLocalIp()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it makes sense now. The purpose of proxyIps was a bit confusing. Will update.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to improve readme for a better understanding of proxyIps functionality.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, still a bit confused. Shouldn't the choices be:

  1. If remote IP detection is enabled, attempt to return remote IP address.
  2. If proxy headers are enabled, attempt to return proxied IP address.
  3. If local IP address is found:
    • If proxy IPs are defined, and the local IP is in the proxy list, void the local IP.
    • Otherwise return the local IP.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to configure the proxies with or without ips. No ips means you're trusting in all proxies, so just try to get the ip from headers and use the remote_addr as fallback. Obviously, this less secure. Setting an array of ips of well known proxies, get the ip only from any of these proxies, and use remote_addr as fallback too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the current changes follow the correct order.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, there's a subtle difference that I missed that the tests are not accounting for:

The local IP has to be checked against proxy IPs if the proxy IP list is defined. Otherwise something weird is happening. Got it.

@shadowhand
Copy link
Member Author

Scrutinizer liked my changes. 😎

src/ClientIp.php Outdated
if ($ip = $this->getLocalIp($request)) {
if (!in_array($ip, $this->proxyIps)) {
return $ip;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the functionality does not remain the same. The logic should be:

  • Get the ip using the default way (REMOTE_ADDR). Let's call it $localIp
  • If proxies hasn't been configured returns $localIp
  • If there's an array of allowed proxy ips, check if $localIp is in this array. If it's not, returns $localIp.
  • Get the new ip using the proxy headers and return it if it's found, or $localIp if doesn't.

In this refactoring $this->proxyIps is used to check the ip returned by $this->getProxyIp(), it should used to check the ip returned by $this->getLocalIp()

@shadowhand shadowhand force-pushed the fix/proxy-ip-detect branch from edf1435 to 17fc737 Compare July 13, 2017 13:52
@shadowhand shadowhand force-pushed the fix/proxy-ip-detect branch from 17fc737 to 013777f Compare July 13, 2017 14:58
}

foreach ($this->proxyHeaders as $name) {
if ($request->hasHeader($name) && ($ip = self::getHeaderIp($request->getHeaderLine($name))) !== null) {
return $localIp;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oscarotero okay, functionality should be a match with existing code. Verify for me?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems good to me

}

foreach ($this->proxyHeaders as $name) {
if ($request->hasHeader($name) && ($ip = self::getHeaderIp($request->getHeaderLine($name))) !== null) {
return $localIp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems good to me

@oscarotero oscarotero merged commit 907852d into master Jul 13, 2017
@shadowhand shadowhand deleted the fix/proxy-ip-detect branch July 13, 2017 16:26
@shadowhand
Copy link
Member Author

Released in v0.5.0.

@oscarotero
Copy link
Member

Well, this is a bugfix, so it could be released as 0.4.1. And the link to compare this version with the previous in changelog is missing.
anyway, now it's done.

@shadowhand
Copy link
Member Author

@oscarotero 🤦‍♂️ good point on the version. I updated the changelog to include the compare link.

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

Successfully merging this pull request may close these issues.

2 participants