Skip to content

Commit 1226c8a

Browse files
committed
Restrict temp directory permissions and drop a pointless temp file
1 parent f53fac1 commit 1226c8a

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

commons/src/main/java/org/restheart/utils/ResourcesExtractor.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.nio.file.SimpleFileVisitor;
3333
import java.nio.file.StandardCopyOption;
3434
import java.nio.file.attribute.BasicFileAttributes;
35+
import java.nio.file.attribute.FileAttribute;
36+
import java.nio.file.attribute.PosixFilePermissions;
3537
import java.util.HashMap;
3638
import java.util.Map;
3739
import 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
}

core/src/main/java/org/restheart/graal/ResourcesScannerFeature.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.IOException;
2323
import java.net.URI;
24+
import java.nio.charset.StandardCharsets;
2425
import java.nio.file.FileSystem;
2526
import java.nio.file.FileSystems;
2627
import java.nio.file.FileVisitResult;
@@ -62,20 +63,17 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
6263
}
6364
});
6465

65-
// Write the directory index as a resource
66+
// Write the directory index as a resource.
67+
//
68+
// This used to round-trip through a temp file — write the string, read it
69+
// straight back as bytes, delete it. The file bought nothing, and on the
70+
// exception path it was never deleted; handing the bytes over directly is
71+
// both simpler and one less file in a shared temp directory.
6672
if (!directoryIndex.isEmpty()) {
67-
try {
68-
var tmpFile = Files.createTempFile("restheart-resources-", ".properties");
69-
var sb = new StringBuilder();
70-
directoryIndex.forEach((dir, files) -> {
71-
sb.append(dir).append("=").append(files).append("\n");
72-
});
73-
Files.writeString(tmpFile, sb.toString());
74-
RuntimeResourceAccess.addResource(ResourcesScannerFeature.class.getModule(), INDEX_RESOURCE, Files.readAllBytes(tmpFile));
75-
Files.delete(tmpFile);
76-
} catch (IOException e) {
77-
System.err.println("[ResourcesScannerFeature] Failed to write directory index: " + e.getMessage());
78-
}
73+
var sb = new StringBuilder();
74+
directoryIndex.forEach((dir, files) -> sb.append(dir).append("=").append(files).append("\n"));
75+
RuntimeResourceAccess.addResource(ResourcesScannerFeature.class.getModule(), INDEX_RESOURCE,
76+
sb.toString().getBytes(StandardCharsets.UTF_8));
7977
}
8078
}
8179

0 commit comments

Comments
 (0)