Blame SOURCES/issue3.patch

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