3232import java .nio .file .SimpleFileVisitor ;
3333import java .nio .file .StandardCopyOption ;
3434import java .nio .file .attribute .BasicFileAttributes ;
35+ import java .nio .file .attribute .FileAttribute ;
36+ import java .nio .file .attribute .PosixFilePermissions ;
3537import java .util .HashMap ;
3638import java .util .Map ;
3739import java .util .regex .Pattern ;
@@ -180,7 +182,7 @@ public static File extract(Class clazz, String resourcePath) throws IOException,
180182
181183 try {
182184 // used when run as a JAR file
183- Path destinationDir = Files .createTempDirectory ("restheart-" );
185+ Path destinationDir = Files .createTempDirectory ("restheart-" , ownerOnly () );
184186
185187 ret = destinationDir .toFile ();
186188
@@ -325,7 +327,7 @@ private static java.net.URL findResource(Class clazz, String resourcePath) {
325327 */
326328 @ SuppressWarnings ("rawtypes" )
327329 private static File extractNativeImageResource (Class clazz , String resourcePath ) throws IOException {
328- Path destinationDir = Files .createTempDirectory ("restheart-" );
330+ Path destinationDir = Files .createTempDirectory ("restheart-" , ownerOnly () );
329331
330332 var index = getNativeImageDirectoryIndex ();
331333 var filesCsv = index .get (resourcePath );
@@ -348,4 +350,21 @@ private static File extractNativeImageResource(Class clazz, String resourcePath)
348350
349351 return destinationDir .toFile ();
350352 }
353+
354+ /**
355+ * Owner-only permissions for a temporary directory.
356+ *
357+ * <p>{@code createTempDirectory} without attributes falls back to the
358+ * filesystem default, which in a shared temp directory means anything the
359+ * umask allows. Extracted resources are readable by whoever can reach them,
360+ * so the permissions are stated rather than inherited.
361+ *
362+ * <p>Empty on filesystems with no POSIX view — Windows — where asking for
363+ * POSIX permissions would throw rather than protect anything.
364+ */
365+ private static FileAttribute <?>[] ownerOnly () {
366+ return FileSystems .getDefault ().supportedFileAttributeViews ().contains ("posix" )
367+ ? new FileAttribute <?>[] { PosixFilePermissions .asFileAttribute (PosixFilePermissions .fromString ("rwx------" )) }
368+ : new FileAttribute <?>[0 ];
369+ }
351370}
0 commit comments