Blame SOURCES/issue1.patch

6f88fb
commit 78cf73473dda5ceee3eecda5169621f36b93c3db
6f88fb
Author: Jiri Vanek <jvanek@redhat.com>
6f88fb
Date:   Tue Jun 18 15:37:47 2019 +0200
6f88fb
6f88fb
    Fixed bug when relative path (..) could leak up (even out of cache)
6f88fb
6f88fb
--- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java
6f88fb
+++ a/netx/net/sourceforge/jnlp/cache/CacheUtil.java
6f88fb
@@ -696,46 +696,68 @@
6f88fb
             path.append(location.getPort());
6f88fb
             path.append(File.separatorChar);
6f88fb
         }
6f88fb
-        path.append(location.getPath().replace('/', File.separatorChar));
6f88fb
-        if (location.getQuery() != null && !location.getQuery().trim().isEmpty()) {
6f88fb
-            path.append(".").append(location.getQuery());
6f88fb
-        }
6f88fb
-
6f88fb
-        File candidate = new File(FileUtils.sanitizePath(path.toString()));
6f88fb
-        if (candidate.getName().length() > 255) {
6f88fb
-            /**
6f88fb
-             * When filename is longer then 255 chars, then then various
6f88fb
-             * filesytems have issues to save it. By saving the file by its
6f88fb
-             * summ, we are trying to prevent collision of two files differs in
6f88fb
-             * suffixes (general suffix of name, not only 'filetype suffix')
6f88fb
-             * only. It is also preventing bug when truncate (files with 1000
6f88fb
-             * chars hash in query) cuts to much.
6f88fb
-             */
6f88fb
+        String locationPath = location.getPath().replace('/', File.separatorChar);
6f88fb
+        if (locationPath.contains("..")){
6f88fb
             try {
6f88fb
-                MessageDigest md = MessageDigest.getInstance("SHA-256");
6f88fb
-                byte[] sum = md.digest(candidate.getName().getBytes(StandardCharsets.UTF_8));
6f88fb
-                //convert the byte to hex format method 2
6f88fb
-                StringBuilder hexString = new StringBuilder();
6f88fb
-                for (int i = 0; i < sum.length; i++) {
6f88fb
-                    hexString.append(Integer.toHexString(0xFF & sum[i]));
6f88fb
-                }
6f88fb
-                String extension = "";
6f88fb
-                int i = candidate.getName().lastIndexOf('.');
6f88fb
-                if (i > 0) {
6f88fb
-                    extension = candidate.getName().substring(i);//contains dot
6f88fb
-                }
6f88fb
-                if (extension.length() < 10 && extension.length() > 1) {
6f88fb
-                    hexString.append(extension);
6f88fb
-                }
6f88fb
-                candidate = new File(candidate.getParentFile(), hexString.toString());
6f88fb
+                /**
6f88fb
+                 * if path contains .. then it can harm lcoal system
6f88fb
+                 * So without mercy, hash it
6f88fb
+                 */
6f88fb
+                String hexed = hex(new File(locationPath).getName(), locationPath);
6f88fb
+                return new File(path.toString(), hexed.toString());
6f88fb
             } catch (NoSuchAlgorithmException ex) {
6f88fb
-                // should not occure, cite from javadoc:
6f88fb
-                // every java iomplementation should support
6f88fb
+                // should not occur, cite from javadoc:
6f88fb
+                // every java implementation should support
6f88fb
                 // MD5 SHA-1 SHA-256
6f88fb
                 throw new RuntimeException(ex);
6f88fb
             }
6f88fb
-        }
6f88fb
-        return candidate;
6f88fb
+        } else {
6f88fb
+            path.append(locationPath);
6f88fb
+            if (location.getQuery() != null && !location.getQuery().trim().isEmpty()) {
6f88fb
+                path.append(".").append(location.getQuery());
6f88fb
+            }
6f88fb
+
6f88fb
+            File candidate = new File(FileUtils.sanitizePath(path.toString()));
6f88fb
+            try {
6f88fb
+                if (candidate.getName().length() > 255) {
6f88fb
+                    /**
6f88fb
+                     * When filename is longer then 255 chars, then then various
6f88fb
+                     * filesystems have issues to save it. By saving the file by its
6f88fb
+                     * sum, we are trying to prevent collision of two files differs in
6f88fb
+                     * suffixes (general suffix of name, not only 'filetype suffix')
6f88fb
+                     * only. It is also preventing bug when truncate (files with 1000
6f88fb
+                     * chars hash in query) cuts to much.
6f88fb
+                     */
6f88fb
+                    String hexed = hex(candidate.getName(), candidate.getName());
6f88fb
+                    candidate = new File(candidate.getParentFile(), hexed.toString());
6f88fb
+                }
6f88fb
+            } catch (NoSuchAlgorithmException ex) {
6f88fb
+                // should not occur, cite from javadoc:
6f88fb
+                // every java implementation should support
6f88fb
+                // MD5 SHA-1 SHA-256
6f88fb
+                throw new RuntimeException(ex);
6f88fb
+            }
6f88fb
+            return candidate;
6f88fb
+        }
6f88fb
+    }
6f88fb
+
6f88fb
+    private static String hex(String origName, String candidate) throws NoSuchAlgorithmException {
6f88fb
+        MessageDigest md = MessageDigest.getInstance("SHA-256");
6f88fb
+        byte[] sum = md.digest(candidate.getBytes(StandardCharsets.UTF_8));
6f88fb
+        //convert the byte to hex format method 2
6f88fb
+        StringBuilder hexString = new StringBuilder();
6f88fb
+        for (int i = 0; i < sum.length; i++) {
6f88fb
+            hexString.append(Integer.toHexString(0xFF & sum[i]));
6f88fb
+        }
6f88fb
+        String extension = "";
6f88fb
+        int i = origName.lastIndexOf('.');
6f88fb
+        if (i > 0) {
6f88fb
+            extension = origName.substring(i);//contains dot
6f88fb
+        }
6f88fb
+        if (extension.length() < 10 && extension.length() > 1) {
6f88fb
+            hexString.append(extension);
6f88fb
+        }
6f88fb
+        return hexString.toString();
6f88fb
     }
6f88fb
 
6f88fb
     /**
6f88fb
diff --git a/netx/net/sourceforge/jnlp/util/FileUtils.java b/netx/net/sourceforge/jnlp/util/FileUtils.java
6f88fb
index 89216375..a5356e08 100644
6f88fb
--- a/netx/net/sourceforge/jnlp/util/FileUtils.java
6f88fb
+++ b/netx/net/sourceforge/jnlp/util/FileUtils.java
6f88fb
@@ -183,6 +183,13 @@
6f88fb
      */
6f88fb
     public static void createParentDir(File f, String eMsg) throws IOException {
6f88fb
         File parent = f.getParentFile();
6f88fb
+        // warning, linux and windows behave differently. Below snippet will pass on win(security hole), fail on linux
6f88fb
+        // warning  mkdir is canonicaling, but exists/isDirectory is not. So  where mkdirs return true, and really creates dir, isDirectory can still return false
6f88fb
+        // can be seen on this example
6f88fb
+        // mkdirs /a/b/../c
6f88fb
+        // where b do not exists will lead creation of /a/c
6f88fb
+        // but exists on /a/b/../c is false on linux  even afterwards
6f88fb
+        // without hexing of .. paths,
6f88fb
         if (!parent.isDirectory() && !parent.mkdirs()) {
6f88fb
             throw new IOException(R("RCantCreateDir",
6f88fb
                     eMsg == null ? parent : eMsg));
6f88fb
diff --git a/tests/netx/unit/net/sourceforge/jnlp/cache/CacheUtilTest.java b/tests/netx/unit/net/sourceforge/jnlp/cache/CacheUtilTest.java
6f88fb
index 6422246b..0d2d9811 100644
6f88fb
--- a/tests/netx/unit/net/sourceforge/jnlp/cache/CacheUtilTest.java
6f88fb
+++ b/tests/netx/unit/net/sourceforge/jnlp/cache/CacheUtilTest.java
6f88fb
@@ -88,6 +88,53 @@ public class CacheUtilTest {
6f88fb
         final File expected = new File("/tmp/https/example.com/5050/applet/e4f3cf11f86f5aa33f424bc3efe3df7a9d20837a6f1a5bbbc60c1f57f3780a4");
6f88fb
         Assert.assertEquals(expected, CacheUtil.urlToPath(u, "/tmp"));
6f88fb
     }
6f88fb
+
6f88fb
+    @Test
6f88fb
+    public void tesPathUpNoGoBasic() throws Exception {
6f88fb
+        final URL u = new URL("https://example.com/applet/../my.jar");
6f88fb
+        final File expected = new File("/tmp/https/example.com/abca4723622ed60db3dea12cbe2402622a74f7a49b73e23b55988e4eee5ded.jar");
6f88fb
+        File r = CacheUtil.urlToPath(u, "/tmp/");
6f88fb
+        Assert.assertEquals(expected, r);
6f88fb
+    }
6f88fb
+
6f88fb
+    @Test
6f88fb
+    public void tesPathUpNoGoBasicLong() throws Exception {
6f88fb
+        final URL u = new URL("https://example.com/applet/../my.jar.q_SlNFU1NJT05JRD02OUY1ODVCNkJBOTM1NThCQjdBMTA5RkQyNDZEQjEwRi5wcm9kX3RwdG9tY2F0MjE1X2p2bTsgRW50cnVzdFRydWVQYXNzUmVkaXJlY3RVcmw9Imh0dHBzOi8vZWZzLnVzcHRvLmdvdi9FRlNXZWJVSVJlZ2lzdGVyZWQvRUZTV2ViUmVnaXN0ZXJlZCI7IFRDUFJPRFBQQUlSc2Vzc2lvbj02MjIxMjk0MTguMjA0ODAuMDAwMA\"");
6f88fb
+        final File expected = new File("/tmp/https/example.com/ec97413e3f6eee8215ecc8375478cc1ae5f44f18241b9375361d5dfcd7b0ec");
6f88fb
+        File r = CacheUtil.urlToPath(u, "/tmp/");
6f88fb
+        Assert.assertEquals(expected, r);
6f88fb
+    }
6f88fb
+
6f88fb
+    @Test
6f88fb
+    public void tesPathUpNoGoBasic2() throws Exception {
6f88fb
+        final URL u = new URL("https://example.com/../my.jar");
6f88fb
+        final File expected = new File("/tmp/https/example.com/eb1a56bed34523dbe7ad84d893ebc31a8bbbba9ce3f370e42741b6a5f067c140.jar");
6f88fb
+        File r = CacheUtil.urlToPath(u, "/tmp/");
6f88fb
+        Assert.assertEquals(expected, r);
6f88fb
+    }
6f88fb
+
6f88fb
+    @Test
6f88fb
+    public void tesPathUpNoGoBasicEvil() throws Exception {
6f88fb
+        final URL u = new URL("https://example.com/../../my.jar");
6f88fb
+        final File expected = new File("/tmp/https/example.com/db464f11d68af73e37eefaef674517b6be23f0e4a5738aaee774ecf5b58f1bfc.jar");
6f88fb
+        File r = CacheUtil.urlToPath(u, "/tmp/");
6f88fb
+        Assert.assertEquals(expected, r);
6f88fb
+    }
6f88fb
+
6f88fb
+    @Test
6f88fb
+    public void tesPathUpNoGoBasicEvil2() throws Exception {
6f88fb
+        final URL u = new URL("https://example.com:99/../../../my.jar");
6f88fb
+        final File expected = new File("/tmp/https/example.com/99/95401524c345e0d554d4d77330e86c98a77b9bb58a0f93094204df446b356.jar");
6f88fb
+        File r = CacheUtil.urlToPath(u, "/tmp/");
6f88fb
+        Assert.assertEquals(expected, r);
6f88fb
+    }
6f88fb
+    @Test
6f88fb
+    public void tesPathUpNoGoBasicEvilest() throws Exception {
6f88fb
+        final URL u = new URL("https://example2.com/something/../../../../../../../../../../../my.jar");
6f88fb
+        final File expected = new File("/tmp/https/example2.com/a8df64388f5b84d5f635e4d6dea5f4d2f692ae5381f8ec6736825ff8d6ff2c0.jar");
6f88fb
+        File r = CacheUtil.urlToPath(u, "/tmp/");
6f88fb
+        Assert.assertEquals(expected, r);
6f88fb
+    }
6f88fb
     
6f88fb
     
6f88fb
     @Test
6f88fb
diff --git a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java
6f88fb
index 100d9150..7580d23b 100644
6f88fb
--- a/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java
6f88fb
+++ b/tests/netx/unit/net/sourceforge/jnlp/runtime/JNLPClassLoaderTest.java
6f88fb
@@ -43,6 +43,8 @@
6f88fb
 import java.io.File;
6f88fb
 import java.io.FileOutputStream;
6f88fb
 import java.io.InputStream;
6f88fb
+import java.net.URL;
6f88fb
+import java.nio.charset.Charset;
6f88fb
 import java.nio.file.Files;
6f88fb
 import java.util.Arrays;
6f88fb
 import java.util.List;
6f88fb
@@ -55,6 +57,12 @@
6f88fb
 import net.sourceforge.jnlp.browsertesting.browsers.firefox.FirefoxProfilesOperator;
6f88fb
 import net.sourceforge.jnlp.cache.UpdatePolicy;
6f88fb
 import net.sourceforge.jnlp.config.DeploymentConfiguration;
6f88fb
+import net.sourceforge.jnlp.config.PathsAndFiles;
6f88fb
+import net.sourceforge.jnlp.JNLPFile;
6f88fb
+import net.sourceforge.jnlp.ServerAccess;
6f88fb
+import net.sourceforge.jnlp.ServerLauncher;
6f88fb
+import net.sourceforge.jnlp.util.StreamUtils;
6f88fb
+import net.sourceforge.jnlp.cache.CacheUtil;
6f88fb
 import net.sourceforge.jnlp.mock.DummyJNLPFileWithJar;
6f88fb
 import net.sourceforge.jnlp.security.appletextendedsecurity.AppletSecurityLevel;
6f88fb
 import net.sourceforge.jnlp.security.appletextendedsecurity.AppletStartupSecuritySettings;
6f88fb
@@ -65,6 +73,7 @@
6f88fb
 import org.junit.BeforeClass;
6f88fb
 
6f88fb
 import org.junit.Test;
6f88fb
+import org.junit.Ignore;
6f88fb
 
6f88fb
 public class JNLPClassLoaderTest extends NoStdOutErrTest {
6f88fb
 
6f88fb
@@ -138,7 +147,8 @@
6f88fb
         File tempDirectory = FileTestUtils.createTempDirectory();
6f88fb
         File jarLocation = new File(tempDirectory, "test.jar");
6f88fb
 
6f88fb
-        /* Test with main-class in manifest */ {
6f88fb
+        /* Test with main-class in manifest */
6f88fb
+        {
6f88fb
             Manifest manifest = new Manifest();
6f88fb
             manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, "DummyClass");
6f88fb
             FileTestUtils.createJarWithContents(jarLocation, manifest);
6f88fb
@@ -156,8 +166,10 @@
6f88fb
     }
6f88fb
 
6f88fb
     @Test
6f88fb
+    @Ignore
6f88fb
     public void getMainClassNameTestEmpty() throws Exception {
6f88fb
-        /* Test with-out any main-class specified */ {
6f88fb
+        /* Test with-out any main-class specified */
6f88fb
+        {
6f88fb
             File tempDirectory = FileTestUtils.createTempDirectory();
6f88fb
             File jarLocation = new File(tempDirectory, "test.jar");
6f88fb
             FileTestUtils.createJarWithContents(jarLocation /* No contents */);
6f88fb
@@ -363,4 +375,57 @@
6f88fb
         }
6f88fb
 
6f88fb
     }
6f88fb
+
6f88fb
+    @Test
6f88fb
+    public void testRelativePathInUrl() throws Exception {
6f88fb
+        CacheUtil.clearCache();
6f88fb
+        int port = ServerAccess.findFreePort();
6f88fb
+        File dir = FileTestUtils.createTempDirectory();
6f88fb
+        dir.deleteOnExit();
6f88fb
+        dir = new File(dir,"base");
6f88fb
+        dir.mkdir();
6f88fb
+        File jar = new File(dir,"j1.jar");
6f88fb
+        File jnlp = new File(dir+"/a/b/up.jnlp");
6f88fb
+        jnlp.getParentFile().mkdirs();
6f88fb
+        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("net/sourceforge/jnlp/runtime/up.jnlp");
6f88fb
+        String jnlpString = StreamUtils.readStreamAsString(is, true, "utf-8");
6f88fb
+        is.close();
6f88fb
+        jnlpString = jnlpString.replaceAll("8080", ""+port);
6f88fb
+        is = ClassLoader.getSystemClassLoader().getResourceAsStream("net/sourceforge/jnlp/runtime/j1.jar");
6f88fb
+        StreamUtils.copyStream(is, new FileOutputStream(jar));
6f88fb
+        Files.write(jnlp.toPath(),jnlpString.getBytes("utf-8"));
6f88fb
+        ServerLauncher as = ServerAccess.getIndependentInstance(jnlp.getParent(), port);
6f88fb
+        boolean verifyBackup = JNLPRuntime.isVerifying();
6f88fb
+        boolean trustBackup= JNLPRuntime.isTrustAll();
6f88fb
+        boolean securityBAckup= JNLPRuntime.isSecurityEnabled();
6f88fb
+        boolean verbose= JNLPRuntime.isDebug();
6f88fb
+        JNLPRuntime.setVerify(false);
6f88fb
+        JNLPRuntime.setTrustAll(true);
6f88fb
+        JNLPRuntime.setSecurityEnabled(false);
6f88fb
+        JNLPRuntime.setDebug(true);
6f88fb
+        try {
6f88fb
+            final JNLPFile jnlpFile1 = new JNLPFile(new URL("http://localhost:" + port + "/up.jnlp"));
6f88fb
+            final JNLPClassLoader classLoader1 = new JNLPClassLoader(jnlpFile1, UpdatePolicy.ALWAYS) {
6f88fb
+                @Override
6f88fb
+                protected void activateJars(List<JARDesc> jars) {
6f88fb
+                    super.activateJars(jars);
6f88fb
+                }
6f88fb
+
6f88fb
+            };
6f88fb
+            InputStream is1 = classLoader1.getResourceAsStream("Hello1.class");
6f88fb
+            is1.close();
6f88fb
+            is1 = classLoader1.getResourceAsStream("META-INF/MANIFEST.MF");
6f88fb
+            is1.close();
6f88fb
+            Assert.assertTrue(new File(PathsAndFiles.CACHE_DIR.getFullPath()+"/0/http/localhost/"+port+"/up.jnlp").exists());
6f88fb
+            Assert.assertTrue(new File(PathsAndFiles.CACHE_DIR.getFullPath()+"/1/http/localhost/"+port+"/f812acb32c857fd916c842e2bf4fb32b9c3837ef63922b167a7e163305058b7.jar").exists());
6f88fb
+        } finally {
6f88fb
+            JNLPRuntime.setVerify(verifyBackup);
6f88fb
+            JNLPRuntime.setTrustAll(trustBackup);
6f88fb
+            JNLPRuntime.setSecurityEnabled(securityBAckup);
6f88fb
+            JNLPRuntime.setDebug(verbose);
6f88fb
+            as.stop();
6f88fb
+        }
6f88fb
+
6f88fb
+    }
6f88fb
+
6f88fb
 }
6f88fb
diff --git a/tests/netx/unit/net/sourceforge/jnlp/runtime/up.jnlp b/tests/netx/unit/net/sourceforge/jnlp/runtime/up.jnlp
6f88fb
new file mode 100644
6f88fb
index 00000000..b22fdfb7
6f88fb
--- /dev/null
6f88fb
+++ b/tests/netx/unit/net/sourceforge/jnlp/runtime/up.jnlp
6f88fb
@@ -0,0 +1,15 @@
6f88fb
+
6f88fb
+<jnlp spec="6.0+" codebase=".">
6f88fb
+
6f88fb
+<information><title>1965</title><vendor>Nemzeti Ado- es Vamhivatal</vendor><offline-allowed/></information>
6f88fb
+
6f88fb
+
6f88fb
+<resources>
6f88fb
+  <j2se href="http://java.sun.com/products/autodl/j2se" version="1.8+" />
6f88fb
+
6f88fb
+  <jar href="http://localhost:8080/../../../base/j1.jar" version="2.0"/>
6f88fb
+</resources>
6f88fb
+
6f88fb
+<application-desc main-class="Hello1" />
6f88fb
+
6f88fb
+</jnlp>