【发布时间】:2017-09-24 12:55:23
【问题描述】:
在构建 sql 查询和更新以提交到我的数据库之前,我需要清理一些用户输入的数据。
我知道最好使用prepared statements,但这不是一个选项。不幸的是,我被 escaping all user supplied Input 卡住了。
看起来 Postgres JDBC 库带有一个进行字符串转义的工具。见org.postgresql.core.Utils.escapeLiteral(..)(附在下面)。我希望既然 Postgres 附带了它,那么它可以安全使用。经过几个小时的谷歌搜索和查看 SQL 备忘单后,我无法找到一个可以打破这一点的示例。
以下看起来足够安全吗?
public class FruitDb {
private Connection connection;
public void findFruit ( String /* user enterable field */ fruitColor ) {
String query = "SELECT * FROM fruit WHERE fruit_color = " + quote( fruitColor );
Statement statement = connection.createStatement();
statement.executeQuery( sql );
}
private String quote( String toQuote ) {
return "'" + Utils.escapeLiteral( null, s, true ).toString() + "'";
}
}
对于那些感兴趣的人,这里是Utils.escapeLiteral 的实现。对我来说看起来相当安全......
package org.postgresql.core;
class Utils {
...
/**
* Escape the given literal <tt>value</tt> and append it to the string builder
* <tt>sbuf</tt>. If <tt>sbuf</tt> is <tt>null</tt>, a new StringBuilder will be
* returned. The argument <tt>standardConformingStrings</tt> defines whether the
* backend expects standard-conforming string literals or allows backslash
* escape sequences.
*
* @param sbuf the string builder to append to; or <tt>null</tt>
* @param value the string value
* @param standardConformingStrings if standard conforming strings should be used
* @return the sbuf argument; or a new string builder for sbuf == null
* @throws SQLException if the string contains a <tt>\0</tt> character
*/
public static StringBuilder escapeLiteral(StringBuilder sbuf, String value, boolean standardConformingStrings)
throws SQLException
{
if (sbuf == null)
{
sbuf = new StringBuilder(value.length() * 11 / 10); // Add 10% for escaping.
}
doAppendEscapedLiteral(sbuf, value, standardConformingStrings);
return sbuf;
}
private static void doAppendEscapedLiteral(Appendable sbuf, String value, boolean standardConformingStrings)
throws SQLException
{
try
{
if (standardConformingStrings)
{
// With standard_conforming_strings on, escape only single-quotes.
for (int i = 0; i < value.length(); ++i)
{
char ch = value.charAt(i);
if (ch == '\0')
throw new PSQLException(GT.tr("Zero bytes may not occur in string parameters."), PSQLState.INVALID_PARAMETER_VALUE);
if (ch == '\'')
sbuf.append('\'');
sbuf.append(ch);
}
}
else
{
// REMOVED. I am using standard encoding.
}
}
catch (IOException e)
{
throw new PSQLException(GT.tr("No IOException expected from StringBuffer or StringBuilder"), PSQLState.UNEXPECTED_ERROR, e);
}
}
}
类似问题:
- How to safely escape arbitrary strings for SQL in PostgreSQL using Java - 我实际上回答了这个建议使用 Utils.escapeLiteral(..),因为我认为这是一个比例外答案更好的解决方案。
- Can I protect against SQL Injection by escaping single-quote and surrounding user input with single-quotes?
- 非常好的帖子: How can sanitation that escapes single quotes be defeated by SQL injection in SQL Server?
【问题讨论】:
-
为什么没有 PreparedStatements 选项。这是防止 SQL 注入的唯一 100% 安全方法。
-
@a_horse_with_no_name - 两个原因 1. 我试图了解问题,但无法说服自己这是一个问题。 2.遗留代码。很多。
-
如果你暗示你正在重构使用
Utils.escapeLiteral,你为什么不重构使用准备好的语句呢?除非现有代码已经使用Utils.escapeLiteral? -
如果所有遗留代码都遵循类似的模式......使用正则表达式将您的示例转换为准备好的语句将非常简单。我之前已经为修改数百条类似的代码做过类似的更改……没有什么能阻止您编写匹配并一次性替换多行的正则表达式。如果代码不是很一致,显然会变得更加困难。
-
JFI: COMMENT ON TABLE .. IS 'comment' is not possible with JDBC PreparedStatement.. 需要某种形式的转义
标签: java postgresql sql-injection