ZF-8253: Zend_Validate_IP incorrectly invalidates valid IP addresses

Issue Type: Bug Created: 2009-11-08T17:49:37.000+0000 Last Updated: 2009-11-09T12:33:41.000+0000 Status: Resolved Fix version(s): - 1.10.0 (27/Jan/10)

Reporter: Dolf Schimmel (Freeaqingme) (freak) Assignee: Thomas Weidner (thomas) Tags: - Zend_Validate

Related issues: Attachments: - tmp3.php


These are some valid IPs which are said to be incorrect by Zend_Validate_Ip: 2001:0db8:0000:0000:0000:0000:1428:57ab 2001:0DB8:0000:0000:0000:0000:1428:57AB a:b:c:: a:b:c:d:: a:b:c:d:e:: a:b:c:d:e:f: a::c:d:e:f:0:1 a::0:1 ::e:f a:b:c:d:e:f::0

Found here (… ) via (… and…-(and-caveats).html )


Posted by Dolf Schimmel (Freeaqingme) (freak) on 2009-11-08T17:50:47.000+0000

Attached tiny reproduction script using Zend_Validate

Posted by Thomas Weidner (thomas) on 2009-11-08T23:09:51.000+0000

Tests are partitially not reproducable:

Example: is a valid IP4 address. According to your testbed it is invalid.

Do you have further informations ?

So basically you are saying that PHP's native functions do not work and ZF should fix a PHP bug ?

Posted by Thomas Weidner (thomas) on 2009-11-08T23:34:48.000+0000

See this bug report within PHP:

I'm not sure if ZF should try to fix PHP bugs. Note that changing this implementation from PHP to ZF will have a massive performance impact.

Note additionally that this bug report was reported "9.Nov."... at the same day as this issue was reported to ZF.

Posted by Dolf Schimmel (Freeaqingme) (freak) on 2009-11-09T05:44:25.000+0000

I removed the ipv4 address, that was obviously my bad (hey, it was late!).

Furthermore I do think we should fix this, no matter if it's PHP's behaviour that is wrong here, or ZF's. ZF claims to validate ipv6 addresses, and currently it does not do that entirely right. If you look for example at Zend_Validate_Int, we could just have gone for return is_int(), but because we wanted to do more than php itself can, we implemented the extra functionality ourselves. Same goes for NotEmpty, and probably more.

As to what the performance impact is concerned; I don't think there would be much. We would only have to think of a regex which takes care of the abovementioned ipv6 addresses and check them only if the other 2 IP-checks fail.

Fwiw; I did not have any contact with the author of the url's that are in the original report, so it's kind of coincidence that we report the same issue within hours from each other.

Posted by Dolf Schimmel (Freeaqingme) (freak) on 2009-11-09T06:40:19.000+0000

I attached a patch that does most of the abovementioned ips correctly. It also contains a (commented out) testcase with ip addresses php currently does do wrong, which we could later use when php bug #50117 is fixed.

EDIT: Patch redrawn. I have got a way better patch that should fix all problems. However, because not all code is mine, I am waiting for approval first.

Posted by Dolf Schimmel (Freeaqingme) (freak) on 2009-11-09T07:21:00.000+0000

Fixed with r18906.

Special thanks to Tino Zijdel ( ) whose code we were allowed to borrow.

Posted by Thomas Weidner (thomas) on 2009-11-09T08:28:07.000+0000

A question of mine does not mean to take over the issue yourself and implement this without any further notice.

Next time, before taking over an issue, please ask the maintainer even if he asked you a question.

Please forward me the mail from Tino related to "allowed to borrow" according to our NDA rules.

Additionally you stated in your comment to use old and new implementation as fallback for performance but it was not implemented this way. Have you made any performance checks related to your reply ? How are your results ?

Posted by Thomas Weidner (thomas) on 2009-11-09T08:29:23.000+0000

Need to check code and it's rights

Posted by Matthew Weier O'Phinney (matthew) on 2009-11-09T08:54:11.000+0000

Thomas -- Dolf has acquired rights to contribute the code (and I have seen the emails). As such, the onus of responsibility lies on him as he committed the changes. Focus on the other questions instead.

Posted by Dolf Schimmel (Freeaqingme) (freak) on 2009-11-09T09:30:28.000+0000

Thomas, my original patch relied on filter_var() which may not be installed on each system, therefore I used the original implementation with inet_ntop() as a fallback. However, because my new fix (which I committed to trunk) does not rely on any extensions that may, or may not be installed, there's no need for any such fallback.

Then there's the issue of performance; I did not benchmark my fix against the old implementation. The reason why I neglected to do so is because the old implementation is flawed (hence this issue) and is never going to work for each ipv6 address. The main problem basically is that inet_ntop() only returns IP's in their standard notation (which is compressed). However, when validating a non-compressed IP, it will be be outputted as compressed (if possible), and therefore always differ from the original input (invalidating it where it actually is valid). Though noncompressed IP's are no standard notation, they are entirely valid acc. to the relevant specs. Besides that, with the new implementation only one (tiny) regex is used max, so there can never be a noticeable slowdown.

The reason I committed it to trunk was because I am pretty certain of my fix being right since all unittests pass, my test actually adds 15 assertions. {quote}<|thomas_1> Problem with I18n is that most people only think of THEIR environment or server settings and not is I18n as complete API with effect to about 20 components <|thomas_1> He attached an issue from me to himself, ... without a solution for 4 days... no good behaviour in my eyes..{quote}

This obviously is not an environment/server setting, and I did introduce a solution within minutes after assigning it to myself. Besides that I made sure all unittests passed (to make sure trunk was still 'stable'), and I did not commit it to any release branch so you would have the option of reviewing/changing stuff.

Fwiw; I never signed an NDA for ZF related stuff, only a CLA.

p.s. I could forward you that conversation, but I am not convinced of any such need, and like to keep emails as confidential as possible (which is why I forwarded them to Matthew only).

Posted by Thomas Weidner (thomas) on 2009-11-09T12:32:02.000+0000

I just verified the performance of the new implementation.

I created a testbed which tested 6500 IP addresses. These tests were run 5 times to have a good overview.

The old implementation needed a average time of 3.2 seconds per run.

The same tests with the new implementation needed a average time of 2.1 seconds per run.

This means that the new implementation is not slower but even faster as the native PHP implemenation by about 30-35%.

Note that I did this tests within a Win* development environment. On *nix this should even perform better.

Therefor closing this issue as fixed. Great work :-)

Posted by Thomas Weidner (thomas) on 2009-11-09T12:33:41.000+0000

Tested by using the adresses from the patch, and additional ones, multiple times.

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.