Preventing SQL Injection/ Cross Site Scripting

From: Date: Sat, 21 Apr 2007 01:08:20 +0000
Subject: Preventing SQL Injection/ Cross Site Scripting
Groups: php.general 
Request: Send a blank email to php-general+get-253400@lists.php.net to get a copy of this message
I've got a comments form that I'd like to harden against SQL Injection / XSS attacks. The data is stored in UTF-8 in a mysql database. I currently parse the data as such: $_POST["commentform"]=str_replace ("'", "''", $_POST["commentform"]);
 //    q->qq
$_POST["commentform"]=str_replace ("--", "", $_POST["commentform"]);
//    -- -> x
$_POST["commentform"]=str_replace (";", "", $_POST["commentform"]);
//    ; -> x
$_POST["commentform"]=str_replace ("=", "''", $_POST["commentform"]);
 //    = -> x
$_POST["commentform"]=preg_replace ("/java/i", "''", $_POST["commentform"]); $_POST["commentform"]=preg_replace ("/script/i", "''", $_POST["commentform"]); $_POST["commentform"]=preg_replace ("/src=/i", "''", $_POST["commentform"]); $_POST["commentform"]=preg_replace ("/src =/i", "''", $_POST["commentform"]); $_POST["commentform"]=preg_replace ("/iframe/i", "''", $_POST["commentform"]); $_POST["commentform"]=preg_replace ("/rel=/i", "''", $_POST["commentform"]); $_POST["commentform"]=preg_replace ("/rel =/i", "''", $_POST["commentform"]); $_POST["commentform"]=preg_replace ("/href=/i", "''", $_POST["commentform"]); $_POST["commentform"]=preg_replace ("/href =/i", "''", $_POST["commentform"]); $_POST["commentform"]=preg_replace ("//i", "''", $_POST["commentform"]); $_POST["commentform"]=htmlspecialchars( mysql_real_escape_string ($_POST["commentform"]) ); The first statement doubles up quotes, it's a bit difficult to see in the code. After seeing this: http://ha.ckers.org/xss.html and another similar one for SQL injection, I'm worried that my filters are not enough. What do the pro php programers out there use? Thanks in advance. Dotan Cohen http://lyricslist.com/ http://what-is-what.com/

« previous php.general (#253400) next »