Fork me on GitHub

good coding practice to prevent sql injection?  Bottom

  • Hi, I'm working on my first zikula module and I'd appreciate it if somebody can educate me on the best coding practice I should follow to prevent SQL injection. Reading codes of existing zikula modules I notice that when constructing SQL clauses the function DataUtil::formatForStore is often used to clean the user input which just addslashes to the user input. Is this sufficient, or is there sth I don't know about in zikula that addresses this issue? Many use bound parameters (the prepare statement) to prevent SQL injection, I'm guessing that's not what zikula uses, right?

    Thanks
  • Right, for the moment one uses FormUtil::getPassedValue and DataUtil::formatForStore. Beginning with Zikula 1.3.0 there will be Doctrine included, so then prepare statements will become more frequently used, whereby Doctrine uses DQL instead of SQL.

    --
    Guite | ModuleStudio
  • Hi,

    if you use DBUtil (other than just passing manually constructed SQL to DBUtil::executeSQL()), the architecture will automatically prepare your input for safe storage using DataUtil::formatForStore. This is the first step in preventing SQL injection. Another good practice I use (where possible) is that when/where you retrieve numerical input from the user is to cast it to an int/float/etc to ensure that if the user entered a string, the string does not survive intact.

    On the output side, you want to enable the SafeHTML output filter in your site config in order to prevent XSS code being included in your generated HTML.

    Together, those 2 techniques should allow you to sleep safely at night.

    Greetings
    R

This list is based on users active over the last 60 minutes.