我是java新手,自學。我遇到了以下問題并被卡住了。事實上,我正在嘗試清理此代碼以防止命令注入,但無法理解如何。我知道如何清理用戶輸入,但這與在作業系統中執行的命令有關,我不確定任何人如何幫助。這是代碼:
public class CommandProcessor {
public CommandProcessor() {
// TODO Auto-generated constructor stub
}
public int invokeCommand(String command) throws IOException{
int exitCode =0;
if(command !=null && !command.isEmpty()) {
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
process.waitFor();
exitCode = process.exitValue();
}catch(InterruptedException e) {
}
}
return exitCode;
}
}
uj5u.com熱心網友回復:
正確的答案是閱讀檔案,因為您當前的代碼不安全。
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Runtime.html#exec(java.lang.String[])
“要執行的命令”應該是一個常數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/462068.html
