Search code examples
javaprocessbuilder

How to restrict directory with ProcessBuilder?


I want a process to have write access to only one directory. If the process attempts to write to another directory, it should be restricted.

I tried directory() method, but this only sets default.

 ProcessBuilder processBuilder = new ProcessBuilder();
 File allowedDir = new File(System.getProperty("user.home") + "/allowedDirectory/"); 

 // setting allowed directory 
 processBuilder.directory(allowedDir); 

 //expect this to fail 
 processBuilder.command("cmd.exe", "/c", "rmdir /s /q \"C:\\Users\\restrictedDirectory\\restrictedFolder\""); 
        Process process = processBuilder.start();
        int errCode = process.waitFor(); 

However the command that I was expecting to fail, works and removes files in the restrictedDirectory. How to restrict ProcessBuilder to have write access to only a single directory?


Solution

  • That's not possible - ProcessBuilder simply executes programs on your computer, it doesn't run them in a sandbox.

    ProcessBuilder#directory(File) sets the working directory for the process.