Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape paths with n #152

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -69,17 +70,23 @@
private Map<String, String> getVars(@Nonnull String content, @Nonnull Map<String, String> currentEnvVars)
throws EnvInjectException {

// Replace single backslashes with double ones so they won't be removed by Property.load()
String escapedContent = content;
escapedContent = escapedContent.replaceAll("(?<![\\\\])\\\\(?![n:*?\"<>\\\\/])(?![\\\\])(?![\n])", "\\\\\\\\");
//Escape windows network shares initial double backslash i.e \\Network\Share
escapedContent = escapedContent.replaceAll("(?m)^([^=]+=)(\\\\\\\\)(?![:*?\"<>\\\\/])", "$1\\\\\\\\\\\\\\\\");

String[] contentArray = content.split("\n");
content = "";
//Check if string contains windows network or local paths
for (String s : contentArray) {
if (s.indexOf("=\\\\", 0) != -1 || Pattern.matches("[A-z_0-9]+=[A-z]:\\\\.*", s)) {
// Replace single backslashes with double ones so they won't be removed by Property.load()
s = s.replaceAll("(?<![\\\\])\\\\(?![:*?\"<>\\\\/])(?![\\\\])(?![\n])", "\\\\\\\\");
//Escape windows network shares initial double backslash i.e \\Network\Share
s = s.replaceAll("(?m)^([^=]+=)(\\\\\\\\)(?![:*?\"<>\\\\/])", "$1\\\\\\\\\\\\\\\\");
}
content += s + "\n";

Check warning on line 83 in src/main/java/org/jenkinsci/plugins/envinject/service/PropertiesLoader.java

View check run for this annotation

ci.jenkins.io / SpotBugs

SBSC_USE_STRINGBUFFER_CONCATENATION

NORMAL: org.jenkinsci.plugins.envinject.service.PropertiesLoader.getVars(String, Map) concatenates strings using + in a loop
Raw output
<p> The method seems to be building a String using concatenation in a loop. In each iteration, the String is converted to a StringBuffer/StringBuilder, appended to, and converted back to a String. This can lead to a cost quadratic in the number of iterations, as the growing string is recopied in each iteration. </p> <p>Better performance can be obtained by using a StringBuffer (or StringBuilder in Java 1.5) explicitly.</p> <p> For example:</p> <pre><code>// This is bad String s = ""; for (int i = 0; i &lt; field.length; ++i) { s = s + field[i]; } // This is better StringBuffer buf = new StringBuffer(); for (int i = 0; i &lt; field.length; ++i) { buf.append(field[i]); } String s = buf.toString(); </code></pre>
}
Map<String, String> result = new LinkedHashMap<>();

Properties properties = new Properties();

try (StringReader stringReader = new StringReader(escapedContent)) {
try (StringReader stringReader = new StringReader(content)) {
properties.load(stringReader);
} catch (IOException ioe) {
throw new EnvInjectException("Problem occurs on loading content", ioe);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ public void contentWithBackSlashes() throws Exception {
}

private void checkWithBackSlashes(boolean fromFile) throws Exception {
String content = "KEY1=Test\\Path\\Variable\nKEY2=C:\\Windows\\Temp\nKEY3=\\\\Test\\Path\\Variable";
String content = "KEY1=C:\\Windows\\Temp\nKEY2=\\\\Test\\Path\\Variable\nKEY3=C:\\npm";
Map<String, String> gatherVars = gatherEnvVars(fromFile, content, new HashMap<String, String>());
assertNotNull(gatherVars);
assertEquals(3, gatherVars.size());

assertEquals("Test\\Path\\Variable", gatherVars.get("KEY1"));
assertEquals("C:\\Windows\\Temp", gatherVars.get("KEY2"));
assertEquals("\\\\Test\\Path\\Variable", gatherVars.get("KEY3"));
assertEquals("C:\\Windows\\Temp", gatherVars.get("KEY1"));
assertEquals("\\\\Test\\Path\\Variable", gatherVars.get("KEY2"));
assertEquals("C:\\npm", gatherVars.get("KEY3"));
}

private Map<String, String> gatherEnvVars(boolean fromFile, String content2Load, Map<String, String> currentEnvVars) throws Exception {
Expand Down