Skip to content

Commit 2cf85ab

Browse files
committed
Fix modular jar final permissions
When a new modular jar file is generated with maven-jar-plugin with Java 11, the final permissions of the file are restricted to the current user instead of using the environment umask which usually allows for group and other users to access the file as well. This is caused by the use of Files#createTempFile() which has a restrictive file permission model for security reason but as the temporary file is generated next to the original jar file, and there's no sensitive reason to restrict its access, the restrictive file permission should not be needed. Fix the issue by creating a simple temporary file generator method.
1 parent 8458ffa commit 2cf85ab

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/main/java/org/codehaus/plexus/archiver/jar/JarToolModularJarArchiver.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.PrintStream;
2424
import java.lang.reflect.Method;
25+
import java.nio.file.FileAlreadyExistsException;
2526
import java.nio.file.Files;
2627
import java.nio.file.Path;
2728
import java.nio.file.StandardCopyOption;
@@ -31,6 +32,7 @@
3132
import java.util.Enumeration;
3233
import java.util.List;
3334
import java.util.Locale;
35+
import java.util.Random;
3436
import java.util.TimeZone;
3537
import java.util.regex.Pattern;
3638
import java.util.zip.ZipEntry;
@@ -147,7 +149,7 @@ protected void postCreateArchive() throws ArchiverException {
147149
private void fixLastModifiedTimeZipEntries() throws IOException {
148150
long timeMillis = getLastModifiedTime().toMillis();
149151
Path destFile = getDestFile().toPath();
150-
Path tmpZip = Files.createTempFile(destFile.getParent(), null, null);
152+
Path tmpZip = createTempFile(destFile.getParent());
151153
try (ZipFile zipFile = new ZipFile(getDestFile());
152154
ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(tmpZip))) {
153155
Enumeration<? extends ZipEntry> entries = zipFile.entries();
@@ -263,4 +265,24 @@ private boolean isJarDateOptionSupported(Method runMethod) {
263265
return false;
264266
}
265267
}
268+
269+
/**
270+
* Create a temporary file in the provided directory.
271+
*
272+
* It is an unsecure replacement for {@code Files#createTempFile(Path, String, String, java.nio.file.attribute.FileAttribute...)}:
273+
* The new file permissions are controlled by the umask property instead of just being accessible to the current user.
274+
*/
275+
private Path createTempFile(Path dir) throws IOException {
276+
Random random = new Random();
277+
for (; ; ) {
278+
279+
String name = Long.toUnsignedString(random.nextLong()) + ".tmp";
280+
Path path = dir.resolve(name);
281+
try {
282+
return Files.createFile(path);
283+
} catch (FileAlreadyExistsException e) {
284+
// retry;
285+
}
286+
}
287+
}
266288
}

0 commit comments

Comments
 (0)