我们知道,使用字符串连接形成SQL查询会呈现容易受SQL注入攻击的程序。我通常使用我提供的任何数据库软件的API提供的参数功能来解决这个问题。如何安全地解析字符串?
但我还没有听说过这是常规系统编程中的一个问题。考虑下面的代码作为程序的一部分,该程序允许用户只写入他私人目录中的文件。
Scanner scanner = new Scanner(System.in);
String directoryName = "Bob";
String filePath = null;
String text = "some text";
System.out.print("Enter a file to write to: ");
filePath = scanner.nextLine();
// Write to the file in Bob's personal directory for this program (i.e. Bob/textfile.txt)
FileOutputStream file = new FileOutputStream(directoryName + "/" + filePath);
file.write(text.getBytes());
第二最后一行是漏洞吗?如果是这样,程序如何更安全(特别是在Java,C++和C#中)?一种方法是验证转义字符的输入。还要别的吗?
@HovercraftFullOfEels:我认为这是我一直在寻找的术语。Java官方教程似乎表明,准备好的语句对于SQL来说是特殊的。它们是否可以在通用环境中应用? – InvalidBrainException