【发布时间】:2016-02-13 00:09:57
【问题描述】:
我们的数据库层中有一个方法,如下所示:
public List<String> getNamesFromId(List<Long> idsList){
StringBuilder query = new StringBuilder();
query.append("Select first_name from person where id in (");
for (int pos = 0; pos < idsList.size(); pos++) {
query.append("?");
query.append(",");
}
query.deleteCharAt(query.length() - 1).append(")");
try {
conn = establishConnection();
pstmt = conn.prepareStatement(query.toString());
for (int i = 0; i < selections.size(); i++) {
pstmt.setLong(i + 1, idsList.get(i));
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
//
}
try {
List<String> namesList = new ArrayList<String>();
while (rs.next()) {
namesList.add(rs.getString("FIRST_NAME"));
}
} catch (SQLException e) {
//
}
// close the Connection object
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
//
}
在我们的强化扫描期间,它将此标记为 SQL 注入说 “调用使用可能来自不受信任来源的输入构建的 SQL 查询。此调用可能允许攻击者修改语句的含义或执行任意 SQL 命令。”
这是因为这是一个面向公众的方法,并且我们正在为准备好的语句的 IN 部分传递参数吗?如果是这样,我们怎样才能做得更好?还是fortify的虚惊一场?
【问题讨论】:
标签: java jdbc prepared-statement sql-injection fortify