我們知道,使用字符串連接形成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