Skip to content

Commit c0a49d5

Browse files
authored
rework temp file permissions (#1065)
1 parent 7e761ad commit c0a49d5

1 file changed

Lines changed: 70 additions & 31 deletions

File tree

poi/src/main/java/org/apache/poi/util/DefaultTempFileCreationStrategy.java

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1717

1818
package org.apache.poi.util;
1919

20+
import org.apache.logging.log4j.Logger;
21+
import org.apache.poi.logging.PoiLogManager;
22+
2023
import static org.apache.poi.util.TempFile.JAVA_IO_TMPDIR;
2124

2225
import java.io.File;
@@ -51,6 +54,9 @@ public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy
5154
/** To use files.deleteOnExit after clean JVM exit, set the <code>-Dpoi.delete.tmp.files.on.exit</code> JVM property */
5255
public static final String DELETE_FILES_ON_EXIT = "poi.delete.tmp.files.on.exit";
5356

57+
private static final Logger logger =
58+
PoiLogManager.getLogger(DefaultTempFileCreationStrategy.class);
59+
5460
/** The directory where the temporary files will be created (<code>null</code> to use the default directory). */
5561
private volatile File dir;
5662

@@ -60,6 +66,10 @@ public class DefaultTempFileCreationStrategy implements TempFileCreationStrategy
6066
/** The lock to make dir initialized only once. */
6167
private final Lock dirLock = new ReentrantLock();
6268

69+
// File permissions that are applied as best effort
70+
private final Set<PosixFilePermission> posixRWFilePermissions = createPosixRWFilePermissions();
71+
private final Set<PosixFilePermission> posixRWXFilePermissions = createPosixRWXFilePermissions();
72+
6373
/**
6474
* Creates the strategy so that it creates the temporary files in the default directory.
6575
*
@@ -96,24 +106,16 @@ public File createTempFile(String prefix, String suffix) throws IOException {
96106

97107
// Generate a unique new filename
98108
File newFile;
99-
try {
100-
// Try POSIX permissions first (owner read/write only)
101-
Path p = Files.createTempFile(dir.toPath(), prefix, suffix,
102-
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")));
103-
newFile = p.toFile();
104-
} catch (UnsupportedOperationException | IOException e) {
105-
// POSIX not supported (e.g., Windows) or failed: fall back to creating normally
106-
newFile = Files.createTempFile(dir.toPath(), prefix, suffix).toFile();
109+
if (posixRWFilePermissions == null) {
110+
newFile = createTempFileFallback(prefix, suffix);
111+
} else {
107112
try {
108-
// Clear all perms for everyone, then set owner-only perms where supported
109-
newFile.setReadable(false, false);
110-
newFile.setWritable(false, false);
111-
newFile.setExecutable(false, false);
112-
newFile.setReadable(true, true);
113-
newFile.setWritable(true, true);
114-
newFile.setExecutable(false, true);
115-
} catch (Exception ignore) {
116-
// best-effort only
113+
// Try POSIX permissions first (owner read/write only)
114+
Path p = Files.createTempFile(dir.toPath(), prefix, suffix,
115+
PosixFilePermissions.asFileAttribute(posixRWFilePermissions));
116+
newFile = p.toFile();
117+
} catch (UnsupportedOperationException | IOException e) {
118+
newFile = createTempFileFallback(prefix, suffix);
117119
}
118120
}
119121

@@ -126,6 +128,13 @@ public File createTempFile(String prefix, String suffix) throws IOException {
126128
return newFile;
127129
}
128130

131+
// POSIX not supported (e.g., Windows) or failed: fall back to creating normally
132+
private File createTempFileFallback(String prefix, String suffix) throws IOException {
133+
File newFile = Files.createTempFile(dir.toPath(), prefix, suffix).toFile();
134+
setOwnerOnlyFilePermissions(newFile, false);
135+
return newFile;
136+
}
137+
129138
/* (non-JavaDoc) Created directory path is <JAVA_IO_TMPDIR>/poifiles/prefix0123456789 */
130139
@Override
131140
public File createTempDirectory(String prefix) throws IOException {
@@ -134,21 +143,15 @@ public File createTempDirectory(String prefix) throws IOException {
134143

135144
// Generate a unique new filename
136145
File newDirectory;
137-
try {
138-
Path p = Files.createTempDirectory(dir.toPath(), prefix,
139-
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")));
140-
newDirectory = p.toFile();
141-
} catch (UnsupportedOperationException | IOException e) {
142-
newDirectory = Files.createTempDirectory(dir.toPath(), prefix).toFile();
146+
if (posixRWXFilePermissions == null) {
147+
newDirectory = createTempDirFallback(prefix);
148+
} else {
143149
try {
144-
newDirectory.setReadable(false, false);
145-
newDirectory.setWritable(false, false);
146-
newDirectory.setExecutable(false, false);
147-
newDirectory.setReadable(true, true);
148-
newDirectory.setWritable(true, true);
149-
newDirectory.setExecutable(true, true);
150-
} catch (Exception ignore) {
151-
// best-effort only
150+
Path p = Files.createTempDirectory(dir.toPath(), prefix,
151+
PosixFilePermissions.asFileAttribute(posixRWXFilePermissions));
152+
newDirectory = p.toFile();
153+
} catch (UnsupportedOperationException | IOException e) {
154+
newDirectory = createTempDirFallback(prefix);
152155
}
153156
}
154157

@@ -159,6 +162,13 @@ public File createTempDirectory(String prefix) throws IOException {
159162
return newDirectory;
160163
}
161164

165+
// POSIX not supported (e.g., Windows) or failed: fall back to creating normally
166+
private File createTempDirFallback(String prefix) throws IOException {
167+
File newDirectory = Files.createTempDirectory(dir.toPath(), prefix).toFile();
168+
setOwnerOnlyFilePermissions(newDirectory, true);
169+
return newDirectory;
170+
}
171+
162172
protected String getJavaIoTmpDir() throws IOException {
163173
final String tmpDir = System.getProperty(JAVA_IO_TMPDIR);
164174
if (tmpDir == null) {
@@ -220,4 +230,33 @@ private void createPOIFilesDirectoryIfNecessary() throws IOException {
220230
}
221231
}
222232

233+
private static void setOwnerOnlyFilePermissions(final File file, final boolean executable) {
234+
try {
235+
file.setReadable(true, true);
236+
file.setWritable(true, true);
237+
file.setExecutable(executable, true);
238+
} catch (Exception ignore) {
239+
// best-effort only
240+
}
241+
242+
}
243+
244+
private static Set<PosixFilePermission> createPosixRWFilePermissions() {
245+
try {
246+
return PosixFilePermissions.fromString("rw-------");
247+
} catch (Exception e) {
248+
logger.warn("Failed to init the PosixFilePermissions, continuing with weaker permissions", e);
249+
return null;
250+
}
251+
}
252+
253+
private static Set<PosixFilePermission> createPosixRWXFilePermissions() {
254+
try {
255+
return PosixFilePermissions.fromString("rwx------");
256+
} catch (Exception e) {
257+
logger.warn("Failed to init the PosixFilePermissions, continuing with weaker permissions", e);
258+
return null;
259+
}
260+
}
261+
223262
}

0 commit comments

Comments
 (0)