Is there a way to check if a linux path is valid in Java? I don't want to use regex and don't want to create a file. Valid paths are /some/path. I'm trying to pass this file path as input in a shell command and trying to validate the input path to avoid shell injection. Any ways I can do this? Thanks in advance.
Asked
Active
Viewed 58 times
0
-
[Regex pattern to validate Linux folder path](https://stackoverflow.com/questions/55069650/regex-pattern-to-validate-linux-folder-path); [Validating the file path using regex](https://coderanch.com/t/551777/java/Validating-file-path-regex); [Validate String as Filename in Java](https://www.baeldung.com/java-validate-filename); [google](https://www.google.com/search?q=java+linux+path+validation&rlz=1C5GCEM_enAU1020AU1020&oq=java+linux+path+validation&aqs=chrome..69i57.9466j0j7&sourceid=chrome&ie=UTF-8) – MadProgrammer Oct 11 '22 at 00:42
-
There should be a better way to avoid shell injection than verifying that the path is valid yourself. I know for Windows, you can pass each part of the command as a separate parameter in the processbuilder, that way the shell knows not to run any commands that are injected. I'm sure there's something similar for Linux, that way the shell is responsible for verifying paths, not you. – Charlie Armstrong Oct 11 '22 at 00:51
-
1define "valid path", linux allows almost everything. `mkdir '${foo'';Alert("Error")}'` `cd \$\{foo\;Alert\(\"Error\"\)\}` – zapl Oct 11 '22 at 00:58
-
I'm using this method to validate the linux path public static boolean isValidFilename(String s) { try { Paths.get(s); return true; } catch (InvalidPathException e) { return false; } } But when I give input like this "/hjj:?/ed it is not throwing exception. – tejaswi chillakuru Oct 11 '22 at 06:50