PreparedStatement是如何防止SQL注入的?

为什么在Java中PreparedStatement能够有效防止SQL注入?这可能是每个Java程序员思考过的问题。

 

首先我们来看下直观的现象(注:需要提前打开mysql的SQL文日志

1. 不使用PreparedStatement的set方法设置参数(效果跟Statement相似,相当于执行静态SQL)

String param = "'test' or 1=1";
String sql = "select file from file where name = " + param; // 拼接SQL参数
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
System.out.println(resultSet.next());

输出结果为true,DB中执行的SQL为

-- 永真条件1=1成为了查询条件的一部分,可以返回所有数据,造成了SQL注入问题
select
file from file where name = 'test' or 1=1

 

2. 使用PreparedStatement的set方法设置参数

String param = "'test' or 1=1";
String sql = "select file from file where name = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, param);
ResultSet resultSet = preparedStatement.executeQuery();
System.out.println(resultSet.next());

输出结果为false,DB中执行的SQL为

select file from file where name = '\'test\' or 1=1'

我们可以看到输出的SQL文是把整个参数用引号包起来,并把参数中的引号作为转义字符,从而避免了参数也作为条件的一部分

 


 

接下来我们分析下源码(以mysql驱动实现为例)

打开java.sql.PreparedStatement通用接口,看到如下注释,了解到PreparedStatement就是为了提高statement(包括SQL,存储过程等)执行的效率。

An object that represents a precompiled SQL statement.
A SQL statement is precompiled and stored in a PreparedStatement object.
This object can then be used to efficiently execute this statement multiple times.

那么,什么是所谓的“precompiled SQL statement”呢?

回答这个问题之前需要先了解下一个SQL文在DB中执行的具体步骤:

  1. Convert given SQL query into DB format -- 将SQL语句转化为DB形式(语法树结构)
  2. Check for syntax -- 检查语法
  3. Check for semantics -- 检查语义
  4. Prepare execution plan -- 准备执行计划(也是优化的过程,这个步骤比较重要,关系到你SQL文的效率,准备在后续文章介绍)
  5. Set the run-time values into the query -- 设置运行时的参数
  6. Run the query and fetch the output -- 执行查询并取得结果

而所谓的“precompiled SQL statement”,就是同样的SQL文(包括不同参数的),1-4步骤只在第一次执行,所以大大提高了执行效率(特别是对于需要重复执行同一SQL的)

 

言归正传,回到source中,我们重点关注一下setString方法(因为其它设置参数的方法诸如setInt,setDouble之类,编译器会检查参数类型,已经避免了SQL注入。)

查看mysql中实现PreparedStatement接口的类com.mysql.jdbc.PreparedStatement中的setString方法(部分代码)

    public void setString(int parameterIndex, String x) throws SQLException {
        synchronized (checkClosed().getConnectionMutex()) {
            // if the passed string is null, then set this column to null
            if (x == null) {
                setNull(parameterIndex, Types.CHAR);
            } else {
                checkClosed();

                int stringLength = x.length();

                if (this.connection.isNoBackslashEscapesSet()) {
                    // Scan for any nasty chars
// 判断是否需要转义处理(比如包含引号,换行等字符) boolean needsHexEscape = isEscapeNeededForString(x, stringLength);
// 如果不需要转义,则在两边加上单引号
if (!needsHexEscape) { byte[] parameterAsBytes = null; StringBuilder quotedString = new StringBuilder(x.length() + 2); quotedString.append('\''); quotedString.append(x); quotedString.append('\''); ... } else { ... } String parameterAsString = x; boolean needsQuoted = true; // 如果需要转义,则做转义处理 if (this.isLoadDataQuery || isEscapeNeededForString(x, stringLength)) { ...

从上面加红色注释的可以明白为什么参数会被单引号包裹,并且类似单引号之类的特殊字符会被转义处理,就是因为这些代码的控制避免了SQL注入。 

这里只对SQL注入相关的代码进行解读,如果在setString前后输出预处理语句(preparedStatement.toString()),会发现如下输出

Before bind: com.mysql.jdbc.JDBC42PreparedStatement@b1a58a3: select file from file where name = ** NOT SPECIFIED **
After bind: com.mysql.jdbc.JDBC42PreparedStatement@b1a58a3: select file from file where name = '\'test\' or 1=1'

编程中建议大家使用PrepareStatement + Bind-variable的方式避免SQL注入

大家有什么其它的看法,欢迎留下评论!

参考:https://stackoverflow.com/questions/30587736/what-is-pre-compiled-sql-statement

posted @ 2018-09-26 09:26  栖息之鹰  阅读(13966)  评论(3编辑  收藏  举报