示例 1:从源到接收器
在以下代码样本中,main 方法调用方法 getVulnerableSource,后者返回一个字符串。请注意,虽然该方法从完全未知的文件读取数据,但是它从不检查所返回数据的有效性。然后,main 方法将此感染的数据传递到 writeToVulnerableSink 中。writeToVulnerableSink 方法将数据写出到文件,从不检查其有效性。
import java.io.*;
public class TestCase_IOT_Static {
public static void main(String[] args) {
try {
writeToVulnerableSink(getVulnerableSource(args[0]));
} catch (Exception e) {
}
}
public static String getVulnerableSource(String file)
throws java.io.IOException, java.io.FileNotFoundException {
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[100];
fis.read(buf);
String ret = new String(buf);
fis.close();
return ret;
}
public static void writeToVulnerableSink(String str)
throws java.io.FileNotFoundException {
FileOutputStream fos = new FileOutputStream(str);
PrintWriter writer = new PrintWriter(fos);
writer.write(str);
}
}代码样本生成以下跟踪:
此窗格显示输入堆栈(在其中,main 调用 getVulnerableSource,后者调用 FileInputStream.read),以及输出堆栈(在其中,main 调用 writeToVulnerableSink,后者调用 PrintWriter.write)。该图形显示数据如何从 read 方法流到 write 方法(通过 main 连接两个调用堆栈)。“数据流”部分显示了 main 方法的操作中传递感染的行号。在此示例中,两个方法调用都存在于方法中的同一行(第 15 行)中。(在上面的示例代码中,这为第 7 行;在屏幕截图中,该文件包含 8 行备注。)