Blame SOURCES/issue1.patch

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