How to prevent SQL injection if I don't have option to use "PreparedStatement" in Java/J2EE -
i have 1 application in can’t user “preparedstatement” on of places.
most of sql queries like…. string sql = "delete " + tablename;
so know how fix “sql injection” problem in code.
regards, sanjay singh
=======================edited after getting answer , verify solution========== according provided suggestion have identified 1 strategy prevent sql injection in case …. know views, working on veracode certificate our application…
filter data not content space , escape sql character (so if there injected code, it’ll not going part of dynamic sql, column name , table name can’t use inject sql query).
public static string gettabcolname(string tabcolname)
{
if(tabcolname == null || "".equals(tabcolname.trim()))
return "";
string tempstr = stringescapeutils.escapesql(tabcolname.trim());
//if value content space means not valid table
// or column name, don’t use in dynamic generated sql
//use space create invalid sql query
return tempstr.indexof(' ') == -1 ? tempstr : "";
}
parameterised queries major step towards preventing sql injection attacks. if cannot use them, have equally major setback in hands. can somewhat mitigate danger by:
input string validation. , mean validation bells , whistles, can reach level of full-blown parser, not few checks.
input manipulation (e.g. quoting , string escaping). again, have right, can harder seems.
both techniques problematic - have let valid input through unchanged, in order maintain compatibility current codebase, while still protecting system. luck that...
from experience, refactoring - or rewriting - code use prepared statements save lot of time , tears in long run.
Comments
Post a Comment