Skip to content

Commit f8d44e0

Browse files
authored
Fix modular jar final permissions (#333)
* 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. Instead of relying on current umask property, read mjar permissions and provide it to Files#createTempFile(...) * Clean temporary file if an error occurs * Do not follow symlink when reading jar attributes
1 parent e9c5dcb commit f8d44e0

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

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

+39-14
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@
2323
import java.io.PrintStream;
2424
import java.lang.reflect.Method;
2525
import java.nio.file.Files;
26+
import java.nio.file.LinkOption;
2627
import java.nio.file.Path;
2728
import java.nio.file.StandardCopyOption;
29+
import java.nio.file.attribute.FileAttribute;
2830
import java.nio.file.attribute.FileTime;
31+
import java.nio.file.attribute.PosixFileAttributeView;
32+
import java.nio.file.attribute.PosixFileAttributes;
33+
import java.nio.file.attribute.PosixFilePermissions;
2934
import java.util.ArrayList;
3035
import java.util.Calendar;
3136
import java.util.Enumeration;
@@ -147,23 +152,43 @@ protected void postCreateArchive() throws ArchiverException {
147152
private void fixLastModifiedTimeZipEntries() throws IOException {
148153
long timeMillis = getLastModifiedTime().toMillis();
149154
Path destFile = getDestFile().toPath();
150-
Path tmpZip = Files.createTempFile(destFile.getParent(), null, null);
151-
try (ZipFile zipFile = new ZipFile(getDestFile());
152-
ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(tmpZip))) {
153-
Enumeration<? extends ZipEntry> entries = zipFile.entries();
154-
while (entries.hasMoreElements()) {
155-
ZipEntry entry = entries.nextElement();
156-
// Not using setLastModifiedTime(FileTime) as it sets the extended timestamp
157-
// which is not compatible with the jar tool output.
158-
entry.setTime(timeMillis);
159-
out.putNextEntry(entry);
160-
if (!entry.isDirectory()) {
161-
IOUtil.copy(zipFile.getInputStream(entry), out);
155+
PosixFileAttributes posixFileAttributes = Files.getFileAttributeView(
156+
destFile, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS)
157+
.readAttributes();
158+
FileAttribute<?>[] attributes;
159+
if (posixFileAttributes != null) {
160+
attributes = new FileAttribute<?>[1];
161+
attributes[0] = PosixFilePermissions.asFileAttribute(posixFileAttributes.permissions());
162+
} else {
163+
attributes = new FileAttribute<?>[0];
164+
}
165+
Path tmpZip = Files.createTempFile(destFile.getParent(), null, null, attributes);
166+
try {
167+
try (ZipFile zipFile = new ZipFile(getDestFile());
168+
ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(tmpZip))) {
169+
Enumeration<? extends ZipEntry> entries = zipFile.entries();
170+
while (entries.hasMoreElements()) {
171+
ZipEntry entry = entries.nextElement();
172+
// Not using setLastModifiedTime(FileTime) as it sets the extended timestamp
173+
// which is not compatible with the jar tool output.
174+
entry.setTime(timeMillis);
175+
out.putNextEntry(entry);
176+
if (!entry.isDirectory()) {
177+
IOUtil.copy(zipFile.getInputStream(entry), out);
178+
}
179+
out.closeEntry();
162180
}
163-
out.closeEntry();
164181
}
182+
Files.move(tmpZip, destFile, StandardCopyOption.REPLACE_EXISTING);
183+
} catch (IOException e) {
184+
// Clean up temporary file if an error occurs
185+
try {
186+
Files.delete(tmpZip);
187+
} catch (IOException ioe) {
188+
e.addSuppressed(ioe);
189+
}
190+
throw e;
165191
}
166-
Files.move(tmpZip, destFile, StandardCopyOption.REPLACE_EXISTING);
167192
}
168193

169194
/**

0 commit comments

Comments
 (0)