示例 4:深度验证

扫描示例 4 代码时,第一次扫描包含三个在对应跟踪例程中具有根的 AppScan® Source 跟踪。假定在trace1 中选择 FileInputStream.read 方法,且添加 validate 例程。样本源代码之后的部分描述每个作用域对验证例程的影响。

public class TestCase_IOT_UserValidation {
    ResultSet resultSet;
    FileInputStream fileInputStream;
    PrintWriter printWriter;
    byte[] buffer;

    public static void main(String[] args) throws Exception {
        TestCase_IOT_UserValidation testCase = new TestCase_IOT_UserValidation();
        testCase.trace1();

        TestCase_IOT_UserValidation testCase2 = new TestCase_IOT_UserValidation();
        testCase2.trace2();

        TestCase_IOT_UserValidation testCase3 = new TestCase_IOT_UserValidation();
        testCase3.trace3();
    }

    private void trace1() throws Exception {
        String source = getVulnerableSource1();
        source = validate(source);
        writeToVulnerableSink(source);
    }

    private void trace2() throws Exception {
        String source = getVulnerableSource2();
        source = validate(source);
        writeToVulnerableSink(source);
    }

    private void trace3() throws Exception {
        String source = getVulnerableSource3();
        source = validate(source);
        writeToVulnerableSink(source);
    }

    public String getVulnerableSource1() throws Exception {
        fileInputStream.read(buffer);
        return new String(buffer);
    }

    public String getVulnerableSource2() throws Exception {
        fileInputStream.read(buffer);
        return new String(buffer);
    }

    public String getVulnerableSource3() throws Exception {
        return resultSet.getString("x");
    }

    public void writeToVulnerableSink(String str) throws Exception {
        printWriter.write(str);
    }

    private String validate(String source) throws Exception {
        // validate
        return source;
    }
}

特定于调用站点的验证例程 - 针对 FileInputStream.read 的此调用的输入 FileInputStream.read

在验证仅适合非常狭窄的上下文时,或在输入方法太宽泛而无法提供一个验证例程的情况下,创建特定于调用站点的验证例程。在 trace1 方法中应用于对 FileInputStream.read 的此调用时,trace1 不会在下次扫描后显示为结果,因为其调用堆栈包含对 validate 方法的调用。但是,仍会报告 trace2,尽管它调用 validate,因为验证例程的作用域与 trace1 调用站点相绑定。trace3 方法也调用 validate,但是会继续报告该方法,因为它使用 ResultSet.getString 作为源。

特定于 API 的验证例程 - 针对 FileInputStream.read 的任何调用的输入 FileInputStream.read

验证仅适用于特定源时,创建特定于 API 的验证例程。应用于对 FileInputStream.read 方法的任何调用时,trace1trace2 方法在下次扫描时都没有结果,因为它们包含对 validate 方法的调用。但是,trace3 方法继续存在,尽管它调用 validate,因为它使用 ResultSet.getString 作为源。