我需要做什么才能正确清理/转义正在输入到编程 SSH 命令中的参数?
例如,路径参数 -
public boolean exists(String path) {
try {
ChannelExec c = (ChannelExec) session.openChannel("exec");
//Here *** would like to be sure that the path is completely valid
c.setCommand("[ -f " + path + " ] && echo \"File exists\" || echo \"File does not exists\"");
InputStream in = c.getInputStream();
c.connect();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(in, out);
in.close();
out.close();
System.out.println(out.toString("UTF-8"));
c.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// TODO Auto-generated method stub
return false;
}
它不安全的原因是路径参数可能来自用户上传的文件。从技术上讲,恶意用户可以上传带有无效文件名的文件。虽然我可以事先检查(我正在这样做),但我也想在这里检查。