gpt4 book ai didi

java - 为什么我的扫描工具仍然报告安全问题?

转载 作者:行者123 更新时间:2023-12-01 10:34:56 25 4
gpt4 key购买 nike

我们的应用程序使用 HP Fortify 扫描安全漏洞。发现的一个漏洞是未发布资源:数据库漏洞。

摘要建议将此作为解决方案:

public void execCxnSql(Connection conn) {

Statement stmt;

try {

stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery(CXN_SQL);

...

}

finally {

if (stmt != null) {

safeClose(stmt);

}

}

}

public static void safeClose(Statement stmt) {

if (stmt != null) {

try {

stmt.close();

} catch (SQLException e) {

log(e);

}

}

}

我已经在以下类型的数据库连接关闭上取得了一些成功:

if(conn != null){

try{

conn.rollback();

conn.Close(); // Report indicates an issue here

}catch SQLException{}

将其转换为:

if(conn != null){

try{

conn.rollback();

}catch SQLException{}

finally{

safeClose(conn); //Issue no longer reported

}



public static void safeClose(Connection stmt) {

if (stmt != null) {

try {

stmt.close();

} catch (SQLException e) {

log(e);

}

}

}

(注:“连接”是SQL数据库连接)

但是,尝试对此类数据库关闭进行相同的修复:

if(conn != null){

try{

conn.commit ();

conn.Close(); // report indicates an issue here

}catch SQLException{}

无法从我们的安全扫描中消除该问题。

if(conn != null){

try{

conn.commit();

}catch SQLException{}

finally{

safeClose(conn); // Issue not resolved

}

(请注意,我们的应用程序是 Java 1.4,因此我们不能使用“try-with-resourses”)

实际扫描并没有详细说明为什么两者不同 - 因此假设有关应用程序使用连接的所有其他内容都相同(据我所知),原因是什么它可以在一种情况下报告安全问题,而不是在另一种情况下报告安全问题?

最佳答案

if(conn != null){

try{

conn.commit ();

conn.Close(); // report indicates an issue here

}catch SQLException{}

因此,如果 conn.commit() 抛出 SQLException 或任何其他异常,则 conn.close() 将不会被执行。

正如您所猜测的,如果在其他情况下可以解决问题,则将其移至finally 应该可以解决该问题。

此外,由于ConnectionAutoCloseable,请考虑使用try-with-resources

关于java - 为什么我的扫描工具仍然报告安全问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34818639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com