简体   繁体   English

Java Veracode 扫描 - SQL 注入的误报

[英]Java Veracode Scan - False Positive on SQL Injection

We get an "CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')" in the Java code:我们在 Java 代码中得到一个“CWE-89:SQL 命令中使用的特殊元素的不当中和('SQL 注入')”:

    private static void doSomethingWithDB(int queryFetchSize, String sql, Object... params)
        try {
            Connection connection = ...

            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setFetchSize(queryFetchSize);
            for (int i = 0; i < params.length; i++) {
                statement.setObject(i + 1, params[i]);
            }
            ResultSet resultSet = statement.executeQuery(); //this is where Veracode reports error
        ....

passing SQL from outside is not a best design practice but it is OK in this particular case (method is private, SQL queries are under our control).从外部传递 SQL 不是最佳设计实践,但在这种特殊情况下是可以的(方法是私有的,SQL 查询在我们的控制之下)。

How can I make Veracode less paranoid in this case?在这种情况下,如何让 Veracode 不那么偏执?

In this scenario one workaround would be to pass the sql's identifier (eg enum) to the method instead of the sql itself, and then map this identifier to the actual sql inside the method body.在这种情况下,一种解决方法是将 sql 的标识符(例如枚举)传递给方法而不是 sql 本身,然后将此标识符映射到方法主体内的实际 sql。 So the example code would like something like this:所以示例代码应该是这样的:

 private static void doSomethingWithDB(int queryFetchSize, SqlName sqlName, Object... params)
    try {
        Connection connection = ...

        PreparedStatement statement = connection.prepareStatement(SqlMap.get(sqlName));
        statement.setFetchSize(queryFetchSize);
        for (int i = 0; i < params.length; i++) {
            statement.setObject(i + 1, params[i]);
        }
        ResultSet resultSet = statement.executeQuery();
    ....

SqlName is the enum representing your predefined sqls and SqlMap is the enum-keyed map definied somewhere else, containing actual sqls. SqlName是代表您预定义的 sql 的枚举, SqlMap是在其他地方定义的枚举键映射,包含实际的 sql。 With this solution, Veracode stops complaining about sql injection, which make sense to me as now you cannot use any sql in your method, only one you recognize.有了这个解决方案,Veracode 不再抱怨 sql 注入,这对我来说很有意义,因为现在你不能在你的方法中使用任何sql,只有你认识的一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM