I’m building a Codeigniter application and I’m trying my hardest to prevent SQL injections. I’m using the Active Record method to construct all my queries. I know Active Record automatically sanitizes the input, but I’m wondering exactly to what extent? Does it simply escape all the quotes, or does it do more? What about preventing obfuscated SQL injections, or other more advanced kinds?
Basically, I’m looking for an in-depth explanation of how CI sanitizes data. Anyone know?
Exactly like this (for the MySQL driver):
mysql_real_escape_string()(this will be the case 99% of the time)mysql_escape_string()addslashes()%and_inLIKEconditions viastr_replace()https://github.com/EllisLab/CodeIgniter/blob/develop/system/database/drivers/mysql/mysql_driver.php#L294
Note that this is merely escaping characters so MySQL queries will not break or do something unexpected, and is used only in the context of a database query to ensure correct syntax based on what you pass to it.
There is no magic that makes all data safe for any context (like HTML, CSV, or XML output), and just in case you were thinking about it:
xss_clean()is not a one-size-fits-all solution nor is it 100% bulletproof, sometimes it’s actually quite inappropriate. The Active Record class does the query escaping automatically, but for everything else you should be escaping/sanitizing data manually in the correct way for the given context, with your output, not your input.