Skip to content

Commit

Permalink
[FIXED JENKINS-43137] Properly escape multiline env vars
Browse files Browse the repository at this point in the history
And env vars containing double-quotes.
  • Loading branch information
abayer committed Mar 27, 2017
1 parent fd0759a commit ff93a66
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
Expand Up @@ -211,7 +211,14 @@ public class ModelInterpreter implements Serializable {
List<String> evaledEnv = new ArrayList<>()
for (int i = 0; i < envVars.size(); i++) {
// Evaluate to deal with any as-of-yet unresolved expressions.
evaledEnv.add((String)script.evaluate('"' + envVars.get(i) + '"'))
String toEval = envVars.get(i)
if (toEval.indexOf('\n') == -1) {
toEval = '"' + (toEval.replace('"', '\\"')) + '"';
} else {
toEval = '"""' + (toEval.replace('"""', '\\"\\"\\"')) + '"""';
}

evaledEnv.add((String)script.evaluate(toEval))
}
return {
script.withEnv(evaledEnv) {
Expand Down
Expand Up @@ -56,6 +56,16 @@ public void simpleEnvironment() throws Exception {
.go();
}

@Issue("JENKINS-43137")
@Test
public void multilineEnvironment() throws Exception {
expect("multilineEnvironment")
.logContains("[Pipeline] { (foo)",
"FOO is BAR",
"MULTILINE is VALID\n\"SO THERE\"")
.go();
}

@Issue("JENKINS-42771")
@Test
public void multiExpressionEnvironment() throws Exception {
Expand Down
@@ -0,0 +1,48 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

pipeline {
environment {
FOO = "BAR"
MULTILINE = '''VALID
"SO THERE"
'''
}

agent {
label "some-label"
}

stages {
stage("foo") {
steps {
sh 'echo "FOO is $FOO"'
sh 'echo "MULTILINE is $MULTILINE"'
}
}
}
}



0 comments on commit ff93a66

Please sign in to comment.