Blame SOURCES/issue1.patch

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