Vectorface / whip

A PHP class for retrieving accurate IP address information for the client.
MIT License
375 stars 19 forks source link

extractAddressFromHeaders always returns false #6

Closed tixastronauta closed 8 years ago

tixastronauta commented 8 years ago

Since the last update, the extractAddressFromHeaders method is always returning false. I'm working on a fix an will open a PR soon.

tixastronauta commented 8 years ago

Actually, since tests are currently failing and I don't have the time to fix it. Here's the patch for this issue in case someone wants to apply it:

index 6fa11bb..8307ab8 100644
--- a/src/VectorFace/Whip/Whip.php
+++ b/src/VectorFace/Whip/Whip.php
@@ -144,7 +144,11 @@ class Whip
                     && ! $this->isIpWhitelisted($this->whitelist[$key], $localAddress))) {
                 continue;
             }
-            return $this->extractAddressFromHeaders($headers);
+            $address = $this->extractAddressFromHeaders($headers);
+            if (false !== $address)
+            {
+                return $address;
+            }
         }
         return false;
     }

Tests failing:

PHPUnit 4.2.6 by Sebastian Bergmann.

Configuration read from /Users/tixastronauta/Sites/opensource/whip/phpunit.xml

...F............

Time: 139 ms, Memory: 3.50Mb

There was 1 failure:

1) VectorFace\WhipTests\WhipTest::testInvalidIPv4Address
Failed asserting that '127.0.0.01' is false.

/Users/tixastronauta/Sites/opensource/whip/tests/VectorFace/WhipTests/WhipTest.php:77

FAILURES!
Tests: 16, Assertions: 16, Failures: 1.
jdpanderson commented 8 years ago

Are you using a Mac or maybe a BSD?

It seems the implementation of inet_pton on Mac OS (and probably other platforms) considers "127.0.0.01" to be a valid IP address. There's not actually anything wrong with the Whip code - just the test's use of an ambiguously (in)valid IP. The fix was to use an IP address that supported platforms consider invalid.

To demonstrate, try the following on a linux box and on a Mac. Mac should give you valid, Linux invalid.

php -r 'echo (inet_pton("127.0.0.1") !== false) ? "Valid\n" : "Invalid\n";'
jdpanderson commented 8 years ago

I've pushed some changes, one of which addresses this issue I'm closing this issue. Please re-open if you still see any brokenness

Thanks for reporting the issue and helping to make our software better.