# HG changeset patch # User mbaesken # Date 1531747845 -7200 # Mon Jul 16 15:30:45 2018 +0200 # Node ID e16436d8b0433c2295e29b5ca0cf40e9196adeb5 # Parent a8ee31fb99e158bd6be84fb76e741794e26ed7b1 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; @@ -61,6 +65,39 @@ protected Map map; /** + * Security or system property which specifies categories of + * (potentially sensitive) information that may be included + * in exception text. This class only defines one category: + * "jarpath" which represents the path of a jar file + * relating to an IO exception. + * The property value is a comma separated list of + * case insignificant category names. + */ + private static final String enhancedTextPropname = "jdk.includeInExceptions"; + + private static final boolean jarPathInExceptionText = initTextProp(); + + private static boolean initTextProp() { + return AccessController.doPrivileged(new PrivilegedAction() { + public Boolean run() { + String val = System.getProperty(enhancedTextPropname); + if (val == null) { + val = Security.getProperty(enhancedTextPropname); + if (val == null) + return false; + } + String[] tokens = val.split(","); + for (String token : tokens) { + if (token.equalsIgnoreCase("jarpath")) + return true; + } + return false; + } + }); + } + + + /** * Constructs a new, empty Attributes object with default size. */ public Attributes() { @@ -369,17 +406,24 @@ * 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 +435,7 @@ 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 +450,11 @@ } 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 +477,23 @@ + "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 = null; + /** * 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 { + this.jarFilename = jarFilename; + read(is); + } + + /** * 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/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 @@ -1072,3 +1072,16 @@ # The setting in this file can be overridden by a system property of the same name # and with the same syntax and possible values. #jdk.net.includeInExceptions=hostInfo + +# Enhanced exception message texts for manifest parsing of jar archives +# +# By default, IO exception messages do not include potentially sensitive +# information such as jar paths. This property may be set to one +# or more values, separated by commas, and with no white-space. Each value +# represents a category of enhanced information. Currently, the only category defined +# is "jarpath" which enables more detailed information in the IOExceptions +# thrown by java.util.jar.Attributes and java.util.jar.Manifest. +# The setting in this file can be overridden by a system property of the same name +# and with the same syntax and possible values. +#jdk.includeInExceptions=jarpath +