|
28 | 28 | import static java.util.Objects.nonNull; |
29 | 29 |
|
30 | 30 | import java.io.Closeable; |
| 31 | +import java.io.File; |
31 | 32 | import java.io.IOException; |
32 | 33 | import java.io.InputStream; |
33 | 34 | import java.io.OutputStream; |
|
37 | 38 | import java.lang.reflect.Field; |
38 | 39 | import java.lang.reflect.Method; |
39 | 40 | import java.nio.ByteBuffer; |
| 41 | +import java.nio.file.FileSystems; |
| 42 | +import java.nio.file.Files; |
| 43 | +import java.nio.file.Path; |
| 44 | +import java.nio.file.attribute.AclEntry; |
| 45 | +import java.nio.file.attribute.AclEntryPermission; |
| 46 | +import java.nio.file.attribute.AclEntryType; |
| 47 | +import java.nio.file.attribute.AclFileAttributeView; |
| 48 | +import java.nio.file.attribute.PosixFilePermissions; |
| 49 | +import java.nio.file.attribute.UserPrincipal; |
40 | 50 | import java.security.AccessController; |
41 | 51 | import java.security.PrivilegedAction; |
| 52 | +import java.util.Collections; |
| 53 | +import java.util.Comparator; |
42 | 54 | import java.util.Objects; |
43 | 55 | import java.util.Optional; |
44 | 56 | import java.util.function.Consumer; |
| 57 | +import java.util.stream.Stream; |
45 | 58 |
|
46 | 59 | import org.apache.logging.log4j.Logger; |
47 | 60 | import org.apache.logging.log4j.LogManager; |
@@ -319,4 +332,88 @@ public static StreamCacheCreateFunction createTempFileOnlyStreamCache() |
319 | 332 | { |
320 | 333 | return MemoryUsageSetting.setupTempFileOnly().streamCache; |
321 | 334 | } |
| 335 | + |
| 336 | + /** |
| 337 | + * Creates a temporary directory in the default temporary-file directory |
| 338 | + * with owner-only permissions and registers a shutdown hook to delete it on JVM exit. |
| 339 | + * |
| 340 | + * <p>Note: This method is designed to be used for storing temporary files that may contain sensitive data |
| 341 | + * in a temporary directories with restricted permissions, to mitigate the risk of unauthorized access by |
| 342 | + * other users or processes on the same system. Used e.g. by PDFDebugger.</p> |
| 343 | + * |
| 344 | + * @return the path to the created temporary directory |
| 345 | + * @throws IOException |
| 346 | + */ |
| 347 | + public static Path createProtectedTempDir() throws IOException |
| 348 | + { |
| 349 | + // S5443: permissions are immediately restricted to owner-only by |
| 350 | + // applyOwnerOnlyPermissions(), mitigating the default-permission risk. |
| 351 | + @SuppressWarnings("java:S5443") |
| 352 | + Path tempPath = Files.createTempDirectory("pdfbox-"); |
| 353 | + applyOwnerOnlyPermissions(tempPath); |
| 354 | + |
| 355 | + // use shutdown hook instead of deleteOnExit() to ensure deletion |
| 356 | + // of the entire directory in case of not automatically deleted on |
| 357 | + // JVM exit (e.g. due to open file handles or when the temp directory is not empty) |
| 358 | + Runtime.getRuntime().addShutdownHook(new Thread(() -> |
| 359 | + { |
| 360 | + try (Stream<Path> entries = Files.walk(tempPath)) |
| 361 | + { |
| 362 | + entries.sorted(Comparator.reverseOrder()) |
| 363 | + .forEach(p -> p.toFile().delete()); |
| 364 | + } |
| 365 | + catch (IOException ignored) {} |
| 366 | + })); |
| 367 | + |
| 368 | + return tempPath; |
| 369 | + } |
| 370 | + |
| 371 | + private static void applyOwnerOnlyPermissions(Path dir) throws IOException |
| 372 | + { |
| 373 | + if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) |
| 374 | + { |
| 375 | + // Unix/macOS — rwx------ |
| 376 | + Files.setPosixFilePermissions(dir, PosixFilePermissions.fromString("rwx------")); |
| 377 | + } |
| 378 | + else |
| 379 | + { |
| 380 | + // Windows — replace the entire ACL with a single owner-only ALLOW entry |
| 381 | + AclFileAttributeView aclView = |
| 382 | + Files.getFileAttributeView(dir, AclFileAttributeView.class); |
| 383 | + |
| 384 | + if (aclView == null) |
| 385 | + { |
| 386 | + File tempDir = dir.toFile(); |
| 387 | + tempDir.setReadable(true, true); |
| 388 | + tempDir.setWritable(true, true); |
| 389 | + tempDir.setExecutable(true, true); |
| 390 | + return; |
| 391 | + } |
| 392 | + |
| 393 | + UserPrincipal owner = aclView.getOwner(); |
| 394 | + |
| 395 | + AclEntry ownerFullControl = AclEntry.newBuilder() |
| 396 | + .setType(AclEntryType.ALLOW) |
| 397 | + .setPrincipal(owner) |
| 398 | + .setPermissions( |
| 399 | + AclEntryPermission.READ_DATA, |
| 400 | + AclEntryPermission.WRITE_DATA, |
| 401 | + AclEntryPermission.APPEND_DATA, |
| 402 | + AclEntryPermission.READ_NAMED_ATTRS, |
| 403 | + AclEntryPermission.WRITE_NAMED_ATTRS, |
| 404 | + AclEntryPermission.EXECUTE, |
| 405 | + AclEntryPermission.DELETE_CHILD, |
| 406 | + AclEntryPermission.READ_ATTRIBUTES, |
| 407 | + AclEntryPermission.WRITE_ATTRIBUTES, |
| 408 | + AclEntryPermission.DELETE, |
| 409 | + AclEntryPermission.READ_ACL, |
| 410 | + AclEntryPermission.WRITE_ACL, |
| 411 | + AclEntryPermission.SYNCHRONIZE |
| 412 | + ) |
| 413 | + .build(); |
| 414 | + |
| 415 | + // Set so that only the owner has permissions, and remove any inherited ACL entries |
| 416 | + aclView.setAcl(Collections.singletonList(ownerFullControl)); |
| 417 | + } |
| 418 | + } |
322 | 419 | } |
0 commit comments