範例 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
當驗證只適用於範圍非常狹小的環境定義,或輸入方法太普通,以致於無法提供一個驗證常式時,請建立呼叫位置特定驗證常式。當您在 trace1
方法中套用於這個對於 FileInputStream.read 的呼叫時,在下一次掃描之後,trace1
不會呈現為發現項目,因為其呼叫堆疊包含對於 validate
方法的呼叫。不過,仍會報告 trace2
(即使呼叫 validate
也一樣),因為驗證常式的範圍與 trace1
呼叫位置緊密相關。trace3
方法也呼叫 validate
,但因為是以 ResultSet.getString
為來源,所以仍會繼續報告此方法。
API 特定驗證常式 - 輸入對於以下方法的呼叫: FileInputStream.read
當驗證只套用於特定來源時,請建立 API 特定驗證常式。當您套用於任何對於 FileInputStream.read 方法的呼叫時,trace1
和 trace2
方法因為含有對於 validate
方法的呼叫,所以在下一次掃描時不會出現發現項目。不過,trace3
方法因為是以 ResultSet.getString
為來源,即使呼叫 validate
,還是會繼續存在。