cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: Local IP may be returned by request->getClientAddress(true);

Open elcreator opened this issue 1 year ago • 5 comments

Describe the bug According to the standard the first non-local address should be selected https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Forwarded-For#selecting_an_ip_address

According to the implementation the first address is selected https://github.com/phalcon/cphalcon/blob/7a3b54d8010d7625cefd5adf90fe5546011039d1/phalcon/Http/Request.zep#L237

To Reproduce Steps to reproduce the behavior: Open the website with Phalcon through any proxy

echo $this->request->getClientAddress(true);

Actual behavior Local IP may be returned

Expected behavior

Non-local IP should be returned

elcreator avatar Jun 25 '25 16:06 elcreator

This has been resolved in https://github.com/phalcon/cphalcon/pull/16784

Thank you @elcreator and @raicabogdan

niden avatar Jul 06 '25 13:07 niden

@niden

Is this issue fixed correctly?

Previously, I was able to retrieve the IP address, but now the method always returns false on both the local and server environments.

It appears that REMOTE_ADDR is not being used when the trustForwardedHeader parameter is true. I can see that the correct REMOTE_ADDR value exists in the $_SERVER global variable, but it is not being returned by getClientAddress(true).

satorumoriwaki avatar Feb 16 '26 03:02 satorumoriwaki

@satorumoriwaki Do you mind providing the x-forwarded-for ips you have? I think it would cause a false return if you only have private or reserved network ips in the proxy ips. Thinking at this more, do you mean we should 'optionally' return the first non-private or non-reserved IP address from x-forwarded-for and if nothing valid found to fallback to REMOTE_ADDR?

raicabogdan avatar Feb 16 '26 11:02 raicabogdan

@raicabogdan

Hi,

With this recent change, the behavior is no longer the same as before. Previously, even when calling getClientAddress(true), it would return REMOTE_ADDR.

However, after this update, getClientAddress(true) no longer uses REMOTE_ADDR, which differs from the original behavior. I am concerned that this change may affect many users.

satorumoriwaki avatar Feb 23 '26 10:02 satorumoriwaki

@satorumoriwaki This should be fixed in the last update from last week. Have you tested that?

raicabogdan avatar Feb 23 '26 10:02 raicabogdan