]> source.dussan.org Git - poi.git/commitdiff
#58597: Add more AccessController.doPrivileged. We should fix them later!
authorUwe Schindler <uschindler@apache.org>
Wed, 11 Nov 2015 11:36:44 +0000 (11:36 +0000)
committerUwe Schindler <uschindler@apache.org>
Wed, 11 Nov 2015 11:36:44 +0000 (11:36 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1713813 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/openxml4j/util/ZipSecureFile.java
src/ooxml/java/org/apache/poi/util/OOXMLLite.java

index 375982b8769831d2a0a9aae2f4310bcf22ad73c1..5a841ddfc3919e5fbeb846eccc48c5310e382a74 100644 (file)
@@ -23,6 +23,8 @@ import java.io.IOException;
 import java.io.InputStream;\r
 import java.io.PushbackInputStream;\r
 import java.lang.reflect.Field;\r
+import java.security.AccessController;\r
+import java.security.PrivilegedAction;\r
 import java.util.zip.InflaterInputStream;\r
 import java.util.zip.ZipEntry;\r
 import java.util.zip.ZipException;\r
@@ -31,6 +33,7 @@ import java.util.zip.ZipInputStream;
 \r
 import org.apache.poi.util.POILogFactory;\r
 import org.apache.poi.util.POILogger;\r
+import org.apache.poi.util.SuppressForbidden;\r
 \r
 /**\r
  * This class wraps a {@link ZipFile} in order to check the\r
@@ -163,20 +166,27 @@ public class ZipSecureFile extends ZipFile {
         return addThreshold(zipIS);\r
     }\r
 \r
-    @SuppressWarnings("resource")\r
-    public static ThresholdInputStream addThreshold(InputStream zipIS) throws IOException {\r
+    public static ThresholdInputStream addThreshold(final InputStream zipIS) throws IOException {\r
         ThresholdInputStream newInner;\r
         if (zipIS instanceof InflaterInputStream) {\r
-            try {\r
-                Field f = FilterInputStream.class.getDeclaredField("in");\r
-                f.setAccessible(true);\r
-                InputStream oldInner = (InputStream)f.get(zipIS);\r
-                newInner = new ThresholdInputStream(oldInner, null);\r
-                f.set(zipIS, newInner);\r
-            } catch (Exception ex) {\r
-                logger.log(POILogger.WARN, "SecurityManager doesn't allow manipulation via reflection for zipbomb detection - continue with original input stream", ex);\r
-                newInner = null;\r
-            }\r
+            newInner = AccessController.doPrivileged(new PrivilegedAction<ThresholdInputStream>() {\r
+                @SuppressForbidden("TODO: Fix this to not use reflection (it will break in Java 9)! " +\r
+                        "Better would be to wrap *before* instead of tyring to insert wrapper afterwards.")\r
+                public ThresholdInputStream run() {\r
+                    ThresholdInputStream newInner = null;\r
+                    try {\r
+                        Field f = FilterInputStream.class.getDeclaredField("in");\r
+                        f.setAccessible(true);\r
+                        InputStream oldInner = (InputStream)f.get(zipIS);\r
+                        newInner = new ThresholdInputStream(oldInner, null);\r
+                        f.set(zipIS, newInner);\r
+                    } catch (Exception ex) {\r
+                        logger.log(POILogger.WARN, "SecurityManager doesn't allow manipulation via reflection for zipbomb detection - continue with original input stream", ex);\r
+                        newInner = null;\r
+                    }\r
+                    return newInner;\r
+                }\r
+            });\r
         } else {\r
             // the inner stream is a ZipFileInputStream, i.e. the data wasn't compressed\r
             newInner = null;\r
index c8aa21c214248b9f74326ecb9fc042fcd49e0cee..de19cb0ed97f3a407741a24365630f559562ce72 100644 (file)
@@ -25,7 +25,9 @@ import java.io.OutputStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.net.URL;
+import java.security.AccessController;
 import java.security.CodeSource;
+import java.security.PrivilegedAction;
 import java.security.ProtectionDomain;
 import java.util.ArrayList;
 import java.util.Enumeration;
@@ -49,7 +51,6 @@ import org.junit.runner.JUnitCore;import org.junit.runner.Result;
  * @author Yegor Kozlov
  */
 public final class OOXMLLite {
-    private static Field _classes;
 
     /**
      * Destination directory to copy filtered classes
@@ -214,12 +215,19 @@ public final class OOXMLLite {
         // make the field accessible, we defer this from static initialization to here to 
         // allow JDKs which do not have this field (e.g. IBM JDK) to at least load the class
         // without failing, see https://issues.apache.org/bugzilla/show_bug.cgi?id=56550
-        try {
-            _classes = ClassLoader.class.getDeclaredField("classes");
-            _classes.setAccessible(true);
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
+        final Field _classes = AccessController.doPrivileged(new PrivilegedAction<Field>() {
+            @SuppressForbidden("TODO: Reflection works until Java 8 on Oracle/Sun JDKs, but breaks afterwards (different classloader types, access checks)")
+            public Field run() {
+                try {
+                    Field fld = ClassLoader.class.getDeclaredField("classes");
+                    fld.setAccessible(true);
+                    return fld;
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+
+            }
+        });
 
         ClassLoader appLoader = ClassLoader.getSystemClassLoader();
         try {