Skip to content

Commit

Permalink
Fix sonar FileUtilities permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
EmanueleValentini1 committed Sep 24, 2024
1 parent 219f51b commit b768661
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.EnumSet;
import java.util.Set;

import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.BPMN_FILE_DOES_NOT_HAVE_DEFINITION_KEY;
import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.CANNOT_EXTRACT_FILE_DEFINITION_KEY;
Expand Down Expand Up @@ -99,12 +100,23 @@ public static String toHexString(byte[] hash) {

public static File fromStringToFile(String fileBase64) {
try {

fileBase64 = fileBase64.replace('-', '+').replace('_', '/');

byte[] decodedBytes = Base64.getDecoder().decode(fileBase64);

Path tempDir = Files.createTempDirectory("multipleUpload");

setPermissions(tempDir);

File tempFile = File.createTempFile("tempfile", ".tmp", tempDir.toFile());

try (FileOutputStream fos = new FileOutputStream(tempFile)) {
fos.write(decodedBytes);
}

tempFile.deleteOnExit();

return tempFile;
} catch (IllegalArgumentException e) {
log.error("Errore nella decodifica del Base64: " + e.getMessage());
Expand All @@ -115,6 +127,16 @@ public static File fromStringToFile(String fileBase64) {
}
}

private static void setPermissions(Path tempDir) throws IOException {
try {
Set<PosixFilePermission> perms = EnumSet.of(PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(tempDir, perms);
} catch (UnsupportedOperationException e) {
log.warn("Posix file permissions are not supported on this system.");
}
}


}

0 comments on commit b768661

Please sign in to comment.