1e67f5
commit b4232ae35d2b86592a945a56c948f107fe7efabe
1e67f5
Author: Jiri Vanek <jvanek@redhat.com>
1e67f5
Date:   Wed Jun 26 13:46:45 2019 +0200
1e67f5
1e67f5
    Nested jar, if by relative path point up, is stored as hashed
1e67f5
1e67f5
diff --git a/netx/net/sourceforge/jnlp/cache/CacheUtil.java b/netx/net/sourceforge/jnlp/cache/CacheUtil.java
1e67f5
index a972eb8e..5c8652b6 100644
1e67f5
--- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java
1e67f5
+++ b/netx/net/sourceforge/jnlp/cache/CacheUtil.java
1e67f5
@@ -741,7 +741,7 @@
1e67f5
         }
1e67f5
     }
1e67f5
 
1e67f5
-    private static String hex(String origName, String candidate) throws NoSuchAlgorithmException {
1e67f5
+    public static String hex(String origName, String candidate) throws NoSuchAlgorithmException {
1e67f5
         MessageDigest md = MessageDigest.getInstance("SHA-256");
1e67f5
         byte[] sum = md.digest(candidate.getBytes(StandardCharsets.UTF_8));
1e67f5
         //convert the byte to hex format method 2
1e67f5
diff --git a/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java b/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
1e67f5
index e015f348..117163f3 100644
1e67f5
--- a/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
1e67f5
+++ b/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
1e67f5
@@ -1340,7 +1340,11 @@
1e67f5
                                         // (inline loading with "jar:..!/..." path will not work
1e67f5
                                         // with standard classloader methods)
1e67f5
                                         
1e67f5
-                                        String extractedJarLocation = localFile + ".nested/" + je.getName();
1e67f5
+                                        String name = je.getName();
1e67f5
+                                        if (name.contains("..")){
1e67f5
+                                            name=CacheUtil.hex(name, name);
1e67f5
+                                        }
1e67f5
+                                        String extractedJarLocation = localFile + ".nested/" + name;
1e67f5
                                         File parentDir = new File(extractedJarLocation).getParentFile();
1e67f5
                                         if (!parentDir.isDirectory() && !parentDir.mkdirs()) {
1e67f5
                                             throw new RuntimeException(R("RNestedJarExtration"));
1e67f5
diff --git a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java
1e67f5
index 7580d23b..a20a1d8f 100644
1e67f5
--- a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java
1e67f5
+++ b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java
1e67f5
@@ -43,6 +43,8 @@
1e67f5
 import java.io.File;
1e67f5
 import java.io.FileOutputStream;
1e67f5
 import java.io.InputStream;
1e67f5
+import java.io.OutputStream;
1e67f5
+import net.sourceforge.jnlp.ResourcesDesc;
1e67f5
 import java.net.URL;
1e67f5
 import java.nio.charset.Charset;
1e67f5
 import java.nio.file.Files;
1e67f5
@@ -407,13 +409,7 @@ public class JNLPClassLoaderTest extends NoStdOutErrTest {
1e67f5
         JNLPRuntime.setDebug(true);
1e67f5
         try {
1e67f5
             final JNLPFile jnlpFile1 = new JNLPFile(new URL("http://localhost:" + port + "/up.jnlp"));
1e67f5
-            final JNLPClassLoader classLoader1 = new JNLPClassLoader(jnlpFile1, UpdatePolicy.ALWAYS) {
1e67f5
-                @Override
1e67f5
-                protected void activateJars(List<JARDesc> jars) {
1e67f5
-                    super.activateJars(jars);
1e67f5
-                }
1e67f5
-
1e67f5
-            };
1e67f5
+            final JNLPClassLoader classLoader1 = JNLPClassLoader.getInstance(jnlpFile1, UpdatePolicy.ALWAYS, false);
1e67f5
             InputStream is1 = classLoader1.getResourceAsStream("Hello1.class");
1e67f5
             is1.close();
1e67f5
             is1 = classLoader1.getResourceAsStream("META-INF/MANIFEST.MF");
1e67f5
@@ -430,4 +426,74 @@ public class JNLPClassLoaderTest extends NoStdOutErrTest {
1e67f5
 
1e67f5
     }
1e67f5
 
1e67f5
+    @Test
1e67f5
+    public void testRelativePathInNestedJars() throws Exception {
1e67f5
+        CacheUtil.clearCache();
1e67f5
+        int port = ServerAccess.findFreePort();
1e67f5
+        File dir = FileTestUtils.createTempDirectory();
1e67f5
+        dir.deleteOnExit();
1e67f5
+        File jar = new File(dir,"jar03_dotdotN1.jar");
1e67f5
+        File jnlp = new File(dir,"jar_03_dotdot_jarN1.jnlp");
1e67f5
+        InputStream is1 = ClassLoader.getSystemClassLoader().getResourceAsStream("net/sourceforge/jnlp/runtime/jar_03_dotdot_jarN1.jnlp");
1e67f5
+        InputStream is2 = ClassLoader.getSystemClassLoader().getResourceAsStream("net/sourceforge/jnlp/runtime/jar03_dotdotN1.jar");
1e67f5
+        OutputStream fos1 = new FileOutputStream(jnlp);
1e67f5
+        OutputStream fos2 = new FileOutputStream(jar);
1e67f5
+        StreamUtils.copyStream(is1, fos1);
1e67f5
+        StreamUtils.copyStream(is2, fos2);
1e67f5
+        fos1.flush();;
1e67f5
+        fos2.flush();
1e67f5
+        fos1.close();
1e67f5
+        fos2.close();
1e67f5
+        ServerLauncher as = ServerAccess.getIndependentInstance(dir.getAbsolutePath(), port);
1e67f5
+        boolean verifyBackup = JNLPRuntime.isVerifying();
1e67f5
+        boolean trustBackup= JNLPRuntime.isTrustAll();
1e67f5
+        boolean securityBAckup= JNLPRuntime.isSecurityEnabled();
1e67f5
+        boolean verbose= JNLPRuntime.isDebug();
1e67f5
+        JNLPRuntime.setVerify(false);
1e67f5
+        JNLPRuntime.setTrustAll(true);
1e67f5
+        JNLPRuntime.setSecurityEnabled(false);
1e67f5
+        JNLPRuntime.setDebug(true);
1e67f5
+        try {
1e67f5
+            //it is invalid jar, so we have to disable checks first
1e67f5
+            final JNLPFile jnlpFile = new JNLPFile(new URL("http://localhost:" + port + "/jar_03_dotdot_jarN1.jnlp"));
1e67f5
+            final JNLPClassLoader classLoader = JNLPClassLoader.getInstance(jnlpFile, UpdatePolicy.ALWAYS, false);
1e67f5
+
1e67f5
+            //ThreadGroup group = Thread.currentThread().getThreadGroup();
1e67f5
+            //ApplicationInstance app = new ApplicationInstance(jnlpFile, group, classLoader);
1e67f5
+            //classLoader.setApplication(app);
1e67f5
+            //app.initialize();
1e67f5
+
1e67f5
+            //this test is actually not testing mutch. The app must be accessing the nested jar in plugin-like way
1e67f5
+            InputStream is = classLoader.getResourceAsStream("application/abev/nyomtatvanyinfo/1965.teminfo.enyk");
1e67f5
+            is.close();
1e67f5
+            is = classLoader.getResourceAsStream("META-INF/MANIFEST.MF");
1e67f5
+            is.close();
1e67f5
+            is = classLoader.getResourceAsStream("META-INF/j1.jar");
1e67f5
+            is.close();
1e67f5
+            is = classLoader.getResourceAsStream("META-INF/../../jar01_to_be_injected.jar");
1e67f5
+            //the .. is not recognized correctly
1e67f5
+            //is.close();
1e67f5
+            //Class c = classLoader.getClass().forName("Hello1");
1e67f5
+            // in j1.jar
1e67f5
+            is = classLoader.getResourceAsStream("Hello1.class");
1e67f5
+            //is.close(); nested jar is not on defualt CP
1e67f5
+            //in  jar01
1e67f5
+            //c = classLoader.getClass().forName("com.devdaily.FileUtilities");
1e67f5
+            is = classLoader.getResourceAsStream("com/devdaily/FileUtilities.class");
1e67f5
+            // is.close(); nested jar is not on defualt CP
1e67f5
+            Assert.assertTrue(new File(PathsAndFiles.CACHE_DIR.getFullPath()+"/0/http/localhost/"+port+"/jar_03_dotdot_jarN1.jnlp").exists());
1e67f5
+            Assert.assertTrue(new File(PathsAndFiles.CACHE_DIR.getFullPath()+"/1/http/localhost/"+port+"/jar03_dotdotN1.jar").exists());
1e67f5
+            Assert.assertTrue(new File(PathsAndFiles.CACHE_DIR.getFullPath()+"/1/http/localhost/"+port+"/jar03_dotdotN1.jar.nested/99a90686bfbe84e3f9dbeed8127bba85672ed73688d3c69191aa1ee70916a.jar").exists());
1e67f5
+            Assert.assertTrue(new File(PathsAndFiles.CACHE_DIR.getFullPath()+"/1/http/localhost/"+port+"/jar03_dotdotN1.jar.nested/META-INF/j1.jar").exists());
1e67f5
+        } finally {
1e67f5
+            JNLPRuntime.setVerify(verifyBackup);
1e67f5
+            JNLPRuntime.setTrustAll(trustBackup);
1e67f5
+            JNLPRuntime.setSecurityEnabled(securityBAckup);
1e67f5
+            JNLPRuntime.setDebug(verbose);
1e67f5
+            as.stop();
1e67f5
+        }
1e67f5
+
1e67f5
+    }
1e67f5
+
1e67f5
+
1e67f5
 }
1e67f5
diff --git a/tests/netx/unit/net/sourceforge/jnlp/runtime/jar_03_dotdot_jarN1.jnlp b/tests/netx/unit/net/sourceforge/jnlp/runtime/jar_03_dotdot_jarN1.jnlp
1e67f5
new file mode 100644
1e67f5
index 00000000..71bdea87
1e67f5
--- /dev/null
1e67f5
+++ b/tests/netx/unit/net/sourceforge/jnlp/runtime/jar_03_dotdot_jarN1.jnlp
1e67f5
@@ -0,0 +1,15 @@
1e67f5
+
1e67f5
+<jnlp spec="6.0+" >
1e67f5
+
1e67f5
+<information><title>1965</title><vendor>Nemzeti Ado- es Vamhivatal</vendor><offline-allowed/></information>
1e67f5
+
1e67f5
+<security><all-permissions/></security>
1e67f5
+
1e67f5
+<resources>
1e67f5
+  <j2se href="http://java.sun.com/products/autodl/j2se" version="1.8+" />
1e67f5
+  <jar href="jar03_dotdotN1.jar" version="2.0"/>
1e67f5
+</resources>
1e67f5
+
1e67f5
+<application-desc main-class="http://localhost/jar01.jar!META-INF/jar01_to_be_injected.jar!METAxINF.Test" />
1e67f5
+
1e67f5
+</jnlp>