James Dumay avatar James Dumay committed 94f850d

BAM-8352 - ExternalProcess should quote environment variable value. Doing this for non-windows platforms to begin with.

Comments (0)

Files changed (1)

src/main/java/com/atlassian/utils/process/ExternalProcess.java

 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
 
         ProcessBuilder pb = new ProcessBuilder(cmdArray);
         pb.directory(workingDir);
-        pb.environment().putAll(environment);
+        pb.environment().putAll(prepareEnvironment(environment));
         return pb.start();
     }
 
                     .command(cmdArray)
                     .directory(workingDir);
 
-            processBuilder.environment().putAll(environment);
+            processBuilder.environment().putAll(prepareEnvironment(environment));
 
     		return processBuilder.start();
     	}
     }
+
+    /**
+     * Adds quotes around the value of the environment variable to correctly escape spaces for non-windows platforms.
+     * @param environment
+     * @return environment
+     */
+    private Map<String, String> prepareEnvironment(Map<String, String> environment)
+    {
+        if (!isWindows())
+        {
+            final Map<String, String> newEnvironment = new HashMap<String, String>();
+            for (Map.Entry<String, String> entry : environment.entrySet())
+            {
+                final String newValue = "\"" + entry.getValue() + "\"";
+                newEnvironment.put(entry.getKey(), newValue);
+            }
+            return newEnvironment;
+        }
+        return environment;
+    }
     
     /**
      * Start the external process and setup the IO pump threads needed to
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.