为什么Veracode报告CWE-89?

问题描述

我有一个用JDBC编写的SQL查询,其中包含动态表和字段名称

    private String checkDimension(String tenantId,String prefix,Long schemaMetaId,Long transactionalMetaId,String dimension,String dimensionValue) {
        DataSource tenantDataSource = tenantDataSourceProvider.getTenantDataSource(checkEntityName(tenantId));
        try (Connection connection = tenantDataSource.getConnection()) {

            String sql = String.format("select * from \"%s_%s\" input_file join \"DIMENSION_VALUES_%s\" dv on " +
                            "(dv.\"DIMENSION\" = ? and dv.\"DIMENSION_VALUE\" = input_file.\"%s\") limit 1",checkEntityName(prefix),checkEntityName(schemaMetaId.toString()),checkEntityName(transactionalMetaId.toString()),checkEntityName(dimensionValue));
            try (PreparedStatement statement = connection.prepareStatement(sql)) {
                statement.setString(1,dimension);
                try (ResultSet resultSet = statement.executeQuery()) {
                    if (resultSet.next()) {
                        return "OK";
                    } else {
                        return "Values do not match";
                    }
                }
            }
        } catch (sqlException exception) {
            throw new IllegalStateException(exception);
        }
    }

为缓解漏洞,我正在使用checkEntityName函数,该函数应使其静音(在Veracode扫描期间将其误报)

    @sqlQueryCleanser
    static String checkEntityName(String entityName) {
        if (!entityName.matches("^[-a-zA-Z0-9_]*$")) {
            throw new SecurityException(String.format("Invalid name for entity: %s",entityName));
        }
        return entityName;
    }

但是Veracode仍在报告此问题:

缺陷ID:1433

说明:此数据库查询包含一个sql注入漏洞。对java.sql.Statement.executeQuery()的调用使用从不受信任的输入派生的变量构造动态SQL查询。攻击者可能利用此漏洞对数据库执行任意SQL查询。 executeQuery()的第一个参数包含来自变量format()的污染数据。受污染的数据源自对java.sql.Statement.executeQuery的早期调用

补救措施:避免动态构造SQL查询。而是使用参数化的预处理语句来防止数据库将绑定变量的内容解释为查询的一部分。始终使用集中的数据验证例程来验证不受信任的输入,以确保其符合预期的格式。

您能给我建议如何解决这个问题吗?谢谢:)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)