src/jdk.jpackage/linux/classes/jdk/jpackage/internal/LinuxPackageBundler.java
branchJDK-8200758-branch
changeset 58434 b00cbf427368
parent 58417 67ffaf3a2b75
child 58435 788704ff2559
--- a/src/jdk.jpackage/linux/classes/jdk/jpackage/internal/LinuxPackageBundler.java	Mon Sep 30 20:26:28 2019 -0400
+++ b/src/jdk.jpackage/linux/classes/jdk/jpackage/internal/LinuxPackageBundler.java	Tue Oct 01 18:22:34 2019 -0400
@@ -90,6 +90,8 @@
         // we are not interested in return code, only possible exception
         APP_BUNDLER.fetchFrom(params).validate(params);
 
+        validateInstallDir(LINUX_INSTALL_DIR.fetchFrom(params));
+
         validateFileAssociations(FILE_ASSOCIATIONS.fetchFrom(params));
 
         // If package name has some restrictions, the string converter will
@@ -303,6 +305,36 @@
         return ApplicationLayout.linuxAppImage();
     }
 
+    private static void validateInstallDir(String installDir) throws
+            ConfigException {
+        if (installDir.startsWith("/usr/") || installDir.equals("/usr")) {
+            throw new ConfigException(MessageFormat.format(I18N.getString(
+                    "error.unsupported-install-dir"), installDir), null);
+        }
+
+        if (installDir.isEmpty()) {
+            throw new ConfigException(MessageFormat.format(I18N.getString(
+                    "error.invalid-install-dir"), "/"), null);
+        }
+
+        boolean valid = false;
+        try {
+            final Path installDirPath = Path.of(installDir);
+            valid = installDirPath.isAbsolute();
+            if (valid && !installDirPath.normalize().toString().equals(
+                    installDirPath.toString())) {
+                // Don't allow '/opt/foo/..' or /opt/.
+                valid = false;
+            }
+        } catch (InvalidPathException ex) {
+        }
+
+        if (!valid) {
+            throw new ConfigException(MessageFormat.format(I18N.getString(
+                    "error.invalid-install-dir"), installDir), null);
+        }
+    }
+
     private static void validateFileAssociations(
             List<Map<String, ? super Object>> associations) throws
             ConfigException {