过于广泛的捕获和可疑的信息泄露 - 如何解决它

时间:2017-07-05 09:43:51

标签: java fortify software-quality

我有一个方法,它接受两个参数,并从arg1填充arg2。如果对象具有意外值,则填充逻辑会调用一个方法,该方法可能会抛出扩展的Exception,也可能抛出NumberFormatException。方法逻辑是:

public Type1 populateType1FromType2(Type1 arg1, Type2 arg2) {
    if(arg2 is null or empty) {
        return arg1;
    }
    try {
        //invoke setters on arg1 and populate values from arg2 //may lead to NPE here or in hierarchy
        //statements
    } catch(Exception e) { //Issue 1
        //log message that this population failed along with exception trace //Issue 2
    }
    return arg1; //may be null, may be half populated, may be fully populated if no exception above
}

现在运行Fortify后,我正在报告上述代码段中的问题。我理解它违背了打字异常的目的。但同时我不知道如何处理这个问题,因为任何异常来了,我们抓住它(问题1)我们基本上记录它并告知它失败(问题2)。如果我明确提到的话,我无法抓住NPE,它是荒谬的。

强化报告和期望:

问题1报告:不要捕获广泛的异常类,例如Exception,Throwable,Error,或者除了在程序或线程的最顶层(因为我们正在捕获Exception对象)

问题1期望:应用程序不应该失败,即使它是一个空指针,只是方法不起作用,我们仍将继续应用程序逻辑的其他部分。

问题2报告:该功能可能会显示系统数据或调试信息。 (因为我们也在添加异常跟踪)

问题2期望:如果有任何异常,我们将记录跟踪,这是预期的行为,它不包含任何客户敏感信息,而是内部ID,我们需要它。

怎么办?

0 个答案:

没有答案