Wrap AccessController around the parts that do the outright IO, this allow modules from being able to do IO operations using this class
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
/**
* This class wrap common file operations so they're only allowed to happen
@@ -49,6 +51,8 @@ public SandboxFileManager() {
*/
public byte[] readFile(String filename) {
Path sandboxPath = pathManager.getSandboxPath(filename);
+
+ return AccessController.doPrivileged((PrivilegedAction<byte[]>) () -> {
try {
return Files.readAllBytes(sandboxPath);
} catch (IOException e) {
@@ -56,6 +60,7 @@ public SandboxFileManager() {
}
return null;
+ });
}
/**
@@ -66,11 +71,16 @@ public SandboxFileManager() {
*/
public void writeFile(String filename, byte[] data) {
Path sandboxPath = pathManager.getSandboxPath(filename);
+
+ AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
try {
Files.write(sandboxPath, data);
} catch (IOException e) {
logger.error("Could not write the file: " + filename, e);
}
+
+ return null;
+ });
}
/**
@@ -80,10 +90,15 @@ public void writeFile(String filename, byte[] data) {
*/
public void deleteFile(String filename) {
Path sandboxPath = pathManager.getSandboxPath(filename);
+
+ AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
try {
Files.delete(sandboxPath);
} catch (IOException e) {
logger.error("Could not delete the file: " + filename, e);
}
+
+ return null;
+ });
}
}
