# HG changeset patch # User mbaesken # Date 1535550203 -7200 # Wed Aug 29 15:43:23 2018 +0200 # Node ID c05e1b2b1cec6b148b05730339b3c43a82c5721b # Parent 7c3891b9f1e00e9d80ebedd0b5a410fbbb6f9551 8205525: Improve exception messages during manifest parsing of jar archives diff --git a/src/java.base/share/classes/java/util/jar/Attributes.java b/src/java.base/share/classes/java/util/jar/Attributes.java --- a/src/java.base/share/classes/java/util/jar/Attributes.java +++ b/src/java.base/share/classes/java/util/jar/Attributes.java @@ -26,7 +26,11 @@ package java.util.jar; import java.io.DataOutputStream; +import java.io.File; import java.io.IOException; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.Security; import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashMap; @@ -34,6 +38,8 @@ import java.util.Objects; import java.util.Set; +import sun.security.util.SecurityProperties; + import sun.util.logging.PlatformLogger; /** @@ -60,6 +66,9 @@ */ protected Map map; + private static final boolean jarPathInExceptionText = + SecurityProperties.includedInExceptions("jarPath"); + /** * Constructs a new, empty Attributes object with default size. */ @@ -369,17 +378,25 @@ * Reads attributes from the specified input stream. * XXX Need to handle UTF8 values. */ + void read(Manifest.FastInputStream is, byte[] lbuf) throws IOException { + read(is, lbuf, null, 0); + } + @SuppressWarnings("deprecation") - void read(Manifest.FastInputStream is, byte[] lbuf) throws IOException { + int read(Manifest.FastInputStream is, byte[] lbuf, String filename, int offset) throws IOException { String name = null, value; byte[] lastline = null; + int lineNumber = offset; int len; while ((len = is.readLine(lbuf)) != -1) { boolean lineContinued = false; byte c = lbuf[--len]; + lineNumber++; + if (c != '\n' && c != '\r') { - throw new IOException("line too long"); + throw new IOException("line too long (" + + getErrorPosition(filename, lineNumber) + ")"); } if (len > 0 && lbuf[len-1] == '\r') { --len; @@ -391,7 +408,8 @@ if (lbuf[0] == ' ') { // continuation of previous line if (name == null) { - throw new IOException("misplaced continuation line"); + throw new IOException("misplaced continuation line (" + + getErrorPosition(filename, lineNumber) + ")"); } lineContinued = true; byte[] buf = new byte[lastline.length + len - 1]; @@ -406,11 +424,13 @@ } else { while (lbuf[i++] != ':') { if (i >= len) { - throw new IOException("invalid header field"); + throw new IOException("invalid header field (" + + getErrorPosition(filename, lineNumber) + ")"); } } if (lbuf[i++] != ' ') { - throw new IOException("invalid header field"); + throw new IOException("invalid header field (" + + getErrorPosition(filename, lineNumber) + ")"); } name = new String(lbuf, 0, 0, i - 2); if (is.peek() == ' ') { @@ -433,9 +453,24 @@ + "entry in the jar file."); } } catch (IllegalArgumentException e) { - throw new IOException("invalid header field name: " + name); + throw new IOException("invalid header field name: " + name + + " (" + getErrorPosition(filename, lineNumber) + ")"); } } + return lineNumber; + } + + static String getErrorPosition(String filename, final int lineNumber) { + if (filename == null || !jarPathInExceptionText) { + return "line " + lineNumber; + } + + final File file = new File(filename); + return AccessController.doPrivileged(new PrivilegedAction() { + public String run() { + return file.getAbsolutePath() + ":" + lineNumber; + } + }); } /** diff --git a/src/java.base/share/classes/java/util/jar/JarFile.java b/src/java.base/share/classes/java/util/jar/JarFile.java --- a/src/java.base/share/classes/java/util/jar/JarFile.java +++ b/src/java.base/share/classes/java/util/jar/JarFile.java @@ -417,12 +417,12 @@ if (manEntry != null) { if (verify) { byte[] b = getBytes(manEntry); - man = new Manifest(new ByteArrayInputStream(b)); + man = new Manifest(new ByteArrayInputStream(b), getName()); if (!jvInitialized) { jv = new JarVerifier(b); } } else { - man = new Manifest(super.getInputStream(manEntry)); + man = new Manifest(super.getInputStream(manEntry), getName()); } manRef = new SoftReference<>(man); } diff --git a/src/java.base/share/classes/java/util/jar/Manifest.java b/src/java.base/share/classes/java/util/jar/Manifest.java --- a/src/java.base/share/classes/java/util/jar/Manifest.java +++ b/src/java.base/share/classes/java/util/jar/Manifest.java @@ -53,6 +53,9 @@ // manifest entries private Map entries = new HashMap<>(); + // name of the corresponding jar archive if available. + private String jarFilename; + /** * Constructs a new, empty Manifest. */ @@ -70,6 +73,18 @@ } /** + * Constructs a new Manifest from the specified input stream. + * + * @param is the input stream containing manifest data + * @param jarFilename the name of the corresponding jar archive if available + * @throws IOException if an I/O error has occured + */ + Manifest(InputStream is, String jarFilename) throws IOException { + read(is); + this.jarFilename = jarFilename; + } + + /** * Constructs a new Manifest that is a copy of the specified Manifest. * * @param man the Manifest to copy @@ -193,7 +208,7 @@ // Line buffer byte[] lbuf = new byte[512]; // Read the main attributes for the manifest - attr.read(fis, lbuf); + int lineNumber = attr.read(fis, lbuf, jarFilename, 0); // Total number of entries, attributes read int ecount = 0, acount = 0; // Average size of entry attributes @@ -206,8 +221,11 @@ while ((len = fis.readLine(lbuf)) != -1) { byte c = lbuf[--len]; + lineNumber++; + if (c != '\n' && c != '\r') { - throw new IOException("manifest line too long"); + throw new IOException("manifest line too long (" + + Attributes.getErrorPosition(jarFilename, lineNumber) + ")"); } if (len > 0 && lbuf[len-1] == '\r') { --len; @@ -220,7 +238,8 @@ if (name == null) { name = parseName(lbuf, len); if (name == null) { - throw new IOException("invalid manifest format"); + throw new IOException("invalid manifest format" + + Attributes.getErrorPosition(jarFilename, lineNumber) + ")"); } if (fis.peek() == ' ') { // name is wrapped @@ -246,7 +265,7 @@ attr = new Attributes(asize); entries.put(name, attr); } - attr.read(fis, lbuf); + lineNumber = attr.read(fis, lbuf, jarFilename, lineNumber); ecount++; acount += attr.size(); //XXX: Fix for when the average is 0. When it is 0, diff --git a/src/java.base/share/classes/sun/security/util/SecurityProperties.java b/src/java.base/share/classes/sun/security/util/SecurityProperties.java new file mode 100644 --- /dev/null +++ b/src/java.base/share/classes/sun/security/util/SecurityProperties.java @@ -0,0 +1,39 @@ +package sun.security.util; + +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.Security; + + +public class SecurityProperties { + + public static String privilegeGetOverridable(String propName) { + return AccessController.doPrivileged((PrivilegedAction) + () -> { + String val = System.getProperty(propName); + if (val == null) { + return Security.getProperty(propName); + } else { + return val; + } + }); + } + + public static boolean includedInExceptions(String refName) { + String val = privilegeGetOverridable("jdk.includeInExceptions"); + if (val == null) { + return false; + } + + String[] tokens = val.split(","); + for (String token : tokens) { + if (token.equalsIgnoreCase(refName)) { + return true; + } + } + return false; + } + +} + + diff --git a/src/java.base/share/conf/security/java.security b/src/java.base/share/conf/security/java.security --- a/src/java.base/share/conf/security/java.security +++ b/src/java.base/share/conf/security/java.security @@ -1081,7 +1081,10 @@ # java.nio.channels package will contain enhanced exception # message information # +# jarPath - enables more detailed information in the IOExceptions thrown +# by java.util.jar.Attributes, java.util.jar.JarFile and java.util.jar.Manifest +# # The property setting in this file can be overridden by a system property of # the same name, with the same syntax and possible values. # -#jdk.includeInExceptions=hostInfo +#jdk.includeInExceptions=hostInfo,jarPath