Blame SOURCES/issue1.patch

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