【发布时间】:2011-10-09 18:30:25
【问题描述】:
我们知道,使用字符串连接来形成 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 特有的。它们可以应用于通用上下文吗?
标签: java string security parsing