@Test public void testCmd() { String[][] commands = { {"nslookup unknown.com &", "nslookupunknown.com"}, {"cat `whoami`", "catwhoami"}, {"whoami > /var/www/static/whoami.txt", "whoami/var/www/static/whoami.txt"}, {"c1 || c2# || c3 || *c4\\", "c1c2c3c4"}, {"c1 &&", "c1"}, {"c1 + > c2 [p1]%", "c1c2[p1]%"}, {"c1 | ${c2}", "c1c2"}, }; for (String[] pair : commands) { assertEquals(pair[1], CliCommandExecutor.checkParameter(pair[0])); } }
public static String checkParameter(String commandParameter) { String repaired = commandParameter.replaceAll(COMMAND_INJECT_REX, ""); if (repaired.length() != commandParameter.length()) { logger.info("Detected illegal character in command {}, replace it to {}.", commandParameter, repaired); } return repaired; }
CliCommandExecutor { public static String checkParameter(String commandParameter) { String repaired = commandParameter.replaceAll(COMMAND_INJECT_REX, ""); if (repaired.length() != commandParameter.length()) { logger.info("Detected illegal character in command {}, replace it to {}.", commandParameter, repaired); } return repaired; } }
CliCommandExecutor { public static String checkParameter(String commandParameter) { String repaired = commandParameter.replaceAll(COMMAND_INJECT_REX, ""); if (repaired.length() != commandParameter.length()) { logger.info("Detected illegal character in command {}, replace it to {}.", commandParameter, repaired); } return repaired; } CliCommandExecutor(); }
CliCommandExecutor { public static String checkParameter(String commandParameter) { String repaired = commandParameter.replaceAll(COMMAND_INJECT_REX, ""); if (repaired.length() != commandParameter.length()) { logger.info("Detected illegal character in command {}, replace it to {}.", commandParameter, repaired); } return repaired; } CliCommandExecutor(); void setRunAtRemote(String host, int port, String user, String pwd); void setRunAtLocal(); void copyFile(String localFile, String destDir); Pair<Integer, String> execute(String command); Pair<Integer, String> execute(String command, Logger logAppender, String jobId); static String checkParameter(String commandParameter); }
CliCommandExecutor { public static String checkParameter(String commandParameter) { String repaired = commandParameter.replaceAll(COMMAND_INJECT_REX, ""); if (repaired.length() != commandParameter.length()) { logger.info("Detected illegal character in command {}, replace it to {}.", commandParameter, repaired); } return repaired; } CliCommandExecutor(); void setRunAtRemote(String host, int port, String user, String pwd); void setRunAtLocal(); void copyFile(String localFile, String destDir); Pair<Integer, String> execute(String command); Pair<Integer, String> execute(String command, Logger logAppender, String jobId); static String checkParameter(String commandParameter); static final String COMMAND_INJECT_REX; }