Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added isset check for REMOTE_ADDR #5418

Closed
wants to merge 4 commits into from

3 participants

Michel Maas Maks3w Matthew Weier O'Phinney
Michel Maas

getIpAddressFromProxy failed while unit-testing, because there is no REMOTE_ADDR set. Now there is a check to see if it is set before trying to check if it is in the trustedProxies array.

Michel Maas AxaliaN Added isset check for REMOTE_ADDR
getIpAddressFromProxy failed while unit-testing, because there is no REMOTE_ADDR set. Now there is a check to see if it is set before trying to check if it is in the trustedProxies array.
f3b0e99
Maks3w
Collaborator

Can you provide a unit test?

I don't see any unit test broken in Travis-CI due this thing.

Michel Maas

I will provide a unit test shortly, however, the reason that Travis-CI probably didn't break, is because they (probably) supply a REMOTE_ADDR in the $_SERVER global.

Matthew Weier O'Phinney

@AxaliaN Um... I know I do not set REMOTE_ADDR or any other $_SERVER globals when running unit tests, and the tests run fine for me. I can't say I'm convinced we need this...

Michel Maas

Try running it on Windows, our entire build failed because there was no check for this ;) I can reproduce it any time, with a simple call to getIpAddress.

The code checks if $_SERVER['REMOTE_ADDR'] is in an array, but just assumes this variable is set. Apparently, this isn't the case all of the time, so a check is in order imho.

Michel Maas

Just to make my case a little stronger: I just pushed a commit to remove trailing spaces from the unit test, and Travis fails with this error:

There was 1 error:

1) ZendTest\Http\PhpEnvironment\RemoteAddressTest::testGetIpAddressReturnsEmptyStringOnNoRemoteAddr
Undefined index: REMOTE_ADDR

/home/travis/build/zendframework/zf2/tests/ZendTest/Http/PhpEnvironment/RemoteAddressTest.php:140

Which is the exact same error RemoveAddress.php gives.

I will fix my unit test for this, but I hope you understand now how there needs to be a check to see if the REMOTE_ADDR key is available.

Michel Maas AxaliaN Fixed unit test
Added a check to see if REMOTE_ADDR has been set.
5e8ad0f
Matthew Weier O'Phinney

@AxaliaN To be honest, I'm not terribly concerned about tests not running on Windows; Windows is a very rare platform when it comes to deployment. I'll merge anyways, but I don't see this as critical by any means.

Matthew Weier O'Phinney weierophinney referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/5418' into develop
Forward port #5418
515063f
Michel Maas

Thanks. It didn't just failf or us on windows, but while building using Jenkins as well. That's why it was critical, at least in our case.

Matthew Weier O'Phinney weierophinney was assigned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 5, 2013
  1. Michel Maas

    Added isset check for REMOTE_ADDR

    AxaliaN authored
    getIpAddressFromProxy failed while unit-testing, because there is no REMOTE_ADDR set. Now there is a check to see if it is set before trying to check if it is in the trustedProxies array.
  2. Michel Maas
Commits on Nov 6, 2013
  1. Michel Maas

    Removed trailing spaces

    AxaliaN authored
  2. Michel Maas

    Fixed unit test

    AxaliaN authored
    Added a check to see if REMOTE_ADDR has been set.
This page is out of date. Refresh to see the latest.
2  library/Zend/Http/PhpEnvironment/RemoteAddress.php
View
@@ -119,7 +119,7 @@ public function getIpAddress()
protected function getIpAddressFromProxy()
{
if (!$this->useProxy
- || !in_array($_SERVER['REMOTE_ADDR'], $this->trustedProxies)
+ || (isset($_SERVER['REMOTE_ADDR']) && !in_array($_SERVER['REMOTE_ADDR'], $this->trustedProxies))
) {
return false;
}
23 tests/ZendTest/Http/PhpEnvironment/RemoteAddressTest.php
View
@@ -127,4 +127,27 @@ public function testGetIpAddressFromProxyFakeData()
$_SERVER['HTTP_X_FORWARDED_FOR'] = '8.8.8.8, 10.0.0.2, 1.1.1.1, 10.0.0.1';
$this->assertEquals('1.1.1.1', $this->remoteAddress->getIpAddress());
}
+
+ /**
+ * Tests if an empty string is returned if the server variable
+ * REMOTE_ADDR is not set.
+ *
+ * This happens when you run a local unit test, or a PHP script with
+ * PHP from the command line.
+ */
+ public function testGetIpAddressReturnsEmptyStringOnNoRemoteAddr()
+ {
+ // Store the set IP address for later use
+ if (isset($_SERVER['REMOTE_ADDR'])) {
+ $ipAddress = $_SERVER['REMOTE_ADDR'];
+ unset($_SERVER['REMOTE_ADDR']);
+ }
+
+ $this->remoteAddress->setUseProxy(true);
+ $this->assertEquals('', $this->remoteAddress->getIpAddress());
+
+ if (isset($ipAddress)) {
+ $_SERVER['REMOTE_ADDR'] = $ipAddress;
+ }
+ }
}
Something went wrong with that request. Please try again.