Simple Machines Community Forum

SMF Development => Bug Reports => Fixed or Bogus Bugs => Topic started by: joepie91 on October 27, 2011, 03:50:57 PM

Title: IP checking breaks reCAPTCHA plugin on IPv6 + general bad code practices
Post by: joepie91 on October 27, 2011, 03:50:57 PM
Hello,

First off I would like to make clear that I have used SMF myself for a long time, and my main reason for using it over other forum platforms is how light and simple it is, and, until now, reliable. I am not just here to bash, I am here to point out a very serious problem.

Recently a friend of mine tried to install a reCAPTCHA plugin on his SMF 2.0.1 board. Every time you would actually try to register or make a guest post, an error would be encountered saying that reCAPTCHA required the user IP to be passed on. After testing in the Subs-Editor.php file I found out that the $_SERVER['REMOTE_ADDR'] variable simply did not hold a value. After creating an independent file that printed the same variable, and finding that it worked just fine, I started suspecting something was wrong with the core SMF code. After a grep through the source I found the following few lines:

./Sources/QueryString.php: if (!isset($_SERVER['REMOTE_ADDR']))
./Sources/QueryString.php: $_SERVER['REMOTE_ADDR'] = '';
./Sources/QueryString.php: elseif (preg_match('~^((([1]?\d)?\d|2[0-4]\d|25[0-5])\.){3}(([1]?\d)?\d|2[0-4]\d|25[0-5])$~', $_SERVER['REMOTE_ADDR']) === 0)
./Sources/QueryString.php: $_SERVER['REMOTE_ADDR'] = 'unknown';
[...]
./Sources/QueryString.php: if ($_SERVER['REMOTE_ADDR'] == 'unknown')
./Sources/QueryString.php: $_SERVER['REMOTE_ADDR'] = '';


... which brings me to several points regarding the code structure of SMF.

There are several very big and important mistakes that are being made here:

Now if this were simply a misformatted regular expression, I would have submitted a corrected version that also did IPv6 and be done with it. However, the problems here - with the programming style in general, not just with the regular expression - are so severe, that it requires more than that. This kind of programming has so much potential to cause future problems that it's very likely that, continuing this style, similar problems will be found in the future.

The solution to the issue we were having? Comment out the two lines for the format checking entirely.
Title: Re: IP checking breaks reCAPTCHA plugin on IPv6 + general bad code practices
Post by: Antechinus on October 27, 2011, 05:04:29 PM
Just noting that we are aware of this problem:

"An IPv4-specific regex is used that cannot deal with IPv6 addresses, nor with IPv4 addresses in IPv6 format."

Sleepy has already written a mod to add IPv6 support to 2.0.x (http://custom.simplemachines.org/mods/index.php?mod=3051) and we are going to have IPv6 support in the next version, of course.

I wont comment on the other points as I'm mainly front end, so am not really up to speed on those points. I expect Norv will take a look at this as soon as he logs in.

Oh and just an opinion here: but I never use any sort of CAPTCHA at all these days, and have not noticed any disadvantage in running an active site like that. I'd suggest not bothering with RECAPTCHA if it's giving you trouble.
Title: Re: IP checking breaks reCAPTCHA plugin on IPv6 + general bad code practices
Post by: Norv on October 28, 2011, 02:30:00 PM
Thank you for taking the time for this, joepie.

Quote from: joepie91 on October 27, 2011, 03:50:57 PM
However, the problems here - with the programming style in general, not just with the regular expression - are so severe, that it requires more than that. This kind of programming has so much potential to cause future problems that it's very likely

I agree. There are bad practices embedded throughout the code. I know that very well, and none of the writing of superglobals and such practices will be in the next version. The SMF codebase is getting a serious refactoring and cleaning for the next versions. I have already started the work with this purpose, and I'm not satisfied until any of such (and similar) will not be there anymore.
We don't need the earth and sky in SMF, but THIS, we do need to do.
Title: Re: IP checking breaks reCAPTCHA plugin on IPv6 + general bad code practices
Post by: Illori on November 18, 2011, 10:39:48 AM
if this is still an issue should it be tracked?
Title: Re: IP checking breaks reCAPTCHA plugin on IPv6 + general bad code practices
Post by: emanuele on March 08, 2012, 09:48:36 AM
The problem with IPv6 should be fixed with SleePy changes to support IPv6, the general bad practice...well it's still there. Don't know if it will be changed for 2.1.
Title: Re: IP checking breaks reCAPTCHA plugin on IPv6 + general bad code practices
Post by: Arantor on December 12, 2013, 12:48:46 AM
The bad practice would require a complete overhaul of everything end to end to achieve and certainly won't happen any time soon though it does need doing.