|
| 1 | +import java.io.File; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.UncheckedIOException; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.nio.file.attribute.PosixFilePermission; |
| 7 | +import java.nio.file.attribute.PosixFilePermissions; |
| 8 | + |
| 9 | +import java.util.EnumSet; |
| 10 | + |
| 11 | +public class TestSafe { |
| 12 | + /* |
| 13 | + * An example of a safe use of createFile or createDirectory if your code must support windows and unix-like systems. |
| 14 | + */ |
| 15 | + void exampleSafeWithWindowsSupportFile() { |
| 16 | + // Creating a temporary file with a non-randomly generated name |
| 17 | + File tempChildFile = new File(System.getProperty("java.io.tmpdir"), "/child-create-file.txt"); |
| 18 | + createTempFile(tempChildFile.toPath()); // GOOD: Good has permissions `-rw-------` |
| 19 | + } |
| 20 | + |
| 21 | + static void createTempFile(Path tempDirChild) { |
| 22 | + try { |
| 23 | + if (tempDirChild.getFileSystem().supportedFileAttributeViews().contains("posix")) { |
| 24 | + // Explicit permissions setting is only required on unix-like systems because |
| 25 | + // the temporary directory is shared between all users. |
| 26 | + // This is not necessary on Windows, each user has their own temp directory |
| 27 | + final EnumSet<PosixFilePermission> posixFilePermissions = |
| 28 | + EnumSet.of( |
| 29 | + PosixFilePermission.OWNER_READ, |
| 30 | + PosixFilePermission.OWNER_WRITE |
| 31 | + ); |
| 32 | + if (!Files.exists(tempDirChild)) { |
| 33 | + Files.createFile( |
| 34 | + tempDirChild, |
| 35 | + PosixFilePermissions.asFileAttribute(posixFilePermissions) |
| 36 | + ); // GOOD: Directory has permissions `-rw-------` |
| 37 | + } else { |
| 38 | + Files.setPosixFilePermissions( |
| 39 | + tempDirChild, |
| 40 | + posixFilePermissions |
| 41 | + ); // GOOD: Good has permissions `-rw-------`, or will throw an exception if this fails |
| 42 | + } |
| 43 | + } else if (!Files.exists(tempDirChild)) { |
| 44 | + // On Windows, we still need to create the directory, when it doesn't already exist. |
| 45 | + Files.createDirectory(tempDirChild); // GOOD: Windows doesn't share the temp directory between users |
| 46 | + } |
| 47 | + } catch (IOException exception) { |
| 48 | + throw new UncheckedIOException("Failed to create temp file", exception); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + void exampleSafeWithWindowsSupportDirectory() { |
| 53 | + File tempDirChildDir = new File(System.getProperty("java.io.tmpdir"), "/child-dir"); |
| 54 | + createTempDirectories(tempDirChildDir.toPath()); // GOOD: Directory has permissions `drwx------` |
| 55 | + } |
| 56 | + |
| 57 | + static void createTempDirectories(Path tempDirChild) { |
| 58 | + try { |
| 59 | + if (tempDirChild.getFileSystem().supportedFileAttributeViews().contains("posix")) { |
| 60 | + // Explicit permissions setting is only required on unix-like systems because |
| 61 | + // the temporary directory is shared between all users. |
| 62 | + // This is not necessary on Windows, each user has their own temp directory |
| 63 | + final EnumSet<PosixFilePermission> posixFilePermissions = |
| 64 | + EnumSet.of( |
| 65 | + PosixFilePermission.OWNER_READ, |
| 66 | + PosixFilePermission.OWNER_WRITE, |
| 67 | + PosixFilePermission.OWNER_EXECUTE |
| 68 | + ); |
| 69 | + if (!Files.exists(tempDirChild)) { |
| 70 | + Files.createDirectories( |
| 71 | + tempDirChild, |
| 72 | + PosixFilePermissions.asFileAttribute(posixFilePermissions) |
| 73 | + ); // GOOD: Directory has permissions `drwx------` |
| 74 | + } else { |
| 75 | + Files.setPosixFilePermissions( |
| 76 | + tempDirChild, |
| 77 | + posixFilePermissions |
| 78 | + ); // GOOD: Good has permissions `drwx------`, or will throw an exception if this fails |
| 79 | + } |
| 80 | + } else if (!Files.exists(tempDirChild)) { |
| 81 | + // On Windows, we still need to create the directory, when it doesn't already exist. |
| 82 | + Files.createDirectories(tempDirChild); // GOOD: Windows doesn't share the temp directory between users |
| 83 | + } |
| 84 | + } catch (IOException exception) { |
| 85 | + throw new UncheckedIOException("Failed to create temp dir", exception); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments