6
I have to agree with Dave_L on this one. I think those lines were added to XOOPS code in a hurry at some point to fix complaints about not working with register_globals off.
It is true if you use the "loop" method, if you forget to initialize a variable in the module, then you can potentially be open to a variety of attacks depending on how that variable is used. i.e. The attacker can feed a value into that variable by adding an argument to the URL.
As a *preferred* method, I would choose Dave_L's approach, or simply use the full $HTTP_POST_VARS['var1'] in the places where you actually need it.
So far this really doesn't relate specifically to SQL injection. The idea of doing 'addslashes' on all non-strings is not good as it leads to unpredictable state of your variables. i.e. You should only 'addslashes' if you really need to, and only the rest of your module code will really know this. A loop at the beginning of your code doesn't know this. Also, 'addslashes' is not really a good way to make things database-safe -- it works for MySQL but not for many other databases which may be supported in the future.
The way to avoid SQL injection is to not use HTTP_POST_VARS or HTTP_GET_VARS etc. **directly** in SQL statements.
Use quoteString:
$xoopsDB->query('SELECT * FROM sometable WHERE user=' . $xoopsDB->quoteString($HTTP_GET_VARS['username']));
but DO NOT use:
$xoopsDB->query('SELECT * FROM sometable WHERE user=' . $HTTP_GET_VARS['username']);
Anyways, there are other issues related to HTTP_VARS and SQL injection is just one of them. The other big thing to worry about is if you directly echo an $HTTP_VAR to the page you are creating. If someone put malicious HTML or javascript into that value, then that malicous code appears on your webpages where it could do anything from disrupting the page layout to stealing a user's cookies (which can allow password stealing and session hijacking). I only know a little about this stuff but the 'net has a huge amount of info if you're interested.
EDIT: by the way, the usual fix for making things display-safe is to use htmlspecialchars($HTTP_GET_VARS['somevar']), but depending exactly what the variable is used for this may or may not work.