On this page
secret-interpolation#
A secret from withCredentials or credentials() is interpolated by Groovy
into the script of a sh, bat, powershell or pwsh step, or into a
command line that Docker Pipeline runs in a shell.
| Severity | high |
|---|---|
| Confidence | high |
| Persona | regular |
What it finds#
withCredentials([usernamePassword(credentialsId: 'nexus',
usernameVariable: 'USER',
passwordVariable: 'PASS')]) {
sh "curl -u ${USER}:${PASS} https://nexus.example.com" // PASS is reported
}
The double quotes make this a Groovy string, so Groovy replaces ${PASS}
with the password before the step runs. Jenkins then receives a script with
the password written into it.
It also finds the secret when it gets there another way:
sh "... $PASS ..."andsh "... ${env.PASS} ..."sh "... ${PASS.trim()} ...", or any expression that uses the secretsh 'curl -u admin:' + PASS, joined with+def cmd = "... ${PASS} ..."and latersh cmdorsh "${cmd}", anddef pw = env.PASSand latersh "... ${pw} ..."- an environment variable set from the secret, with
environment,withEnvorenv.X = ..., and later interpolated sh script: "...", returnStdout: true, and"""..."""strings, with or without.stripIndent()- the arguments of
docker.build(image, args), and ofrun(args, command)andwithRun(args, command)on an image, which Docker Pipeline joins into ashcommand line
The secrets it knows:
withCredentialsbindings:string,usernameColonPasswordandusernamePassword;- the passphrase of
sshUserPrivateKey, the password ofcertificate, and the secret key ofAmazonWebServicesCredentialsBinding; - any other binding's
...Variableparameter; GIT_PASSWORDfromgitUsernamePassword.environmentsections, at the pipeline or stage level:X_PSWforX = credentials('id').
Why it matters#
The Jenkins documentation says it plainly: Groovy string interpolation should never be used with credentials.
- The secret is copied into the process arguments, where any process on
the agent can read it, for example with
ps. - Shell metacharacters in the secret run as shell syntax. A password
with
$(...), a backtick or;in it changes the command. A secret someone else can set becomes a way to run commands on the agent. - Jenkins masks secrets in the build log, but not in the process arguments
or in the script file that
shwrites to the agent's disk.
Recent Jenkins versions also print a warning in the build log when a step receives an interpolated secret.
How to fix it#
Pass the script in single quotes, so that Groovy leaves it alone and the
shell reads the secret from its environment, where withCredentials and
environment put it:
withCredentials([usernamePassword(credentialsId: 'nexus',
usernameVariable: 'USER',
passwordVariable: 'PASS')]) {
sh 'curl -u "$USER:$PASS" https://nexus.example.com'
}
| Step | Read the secret as |
|---|---|
sh |
"$PASS", in double quotes inside the single-quoted Groovy string |
bat |
%PASS% |
powershell, pwsh |
$env:PASS |
When the script also needs a Groovy value, escape the secret's $ instead,
so that only the Groovy value is interpolated:
sh "curl -u \"\$USER:\$PASS\" ${env.NEXUS_URL}"
What it does not report#
- Paths to secret files.
file(variable: 'KUBECONFIG'),sshUserPrivateKey(keyFileVariable: 'KEY')and the keystore ofcertificatehold a path, not the secret.--kubeconfig ${KUBECONFIG}is fine. - Secrets used outside their
withCredentialsblock or stage. There, Groovy does not read them as secrets. - Strings in single quotes and escaped dollars (
\$PASS). The shell reads those, not Groovy.
Other personas#
- pedantic:
- User names and key IDs (
usernameVariable,X_USR,accessKeyVariable): low severity, since they are rarely what an attacker wants. XforX = credentials('id'): high severity, but medium confidence. For a user name and password,Xholdsuser:password, but for a secret file it holds a path.- Secrets interpolated into other steps, such as
echoorwriteFile: low severity. Jenkins warns about these too, but outside a script they do less harm. - auditor: the same as pedantic.
When to ignore it#
When the value is not really secret, for example a fake token in a test pipeline:
// jenkinsfilelint: ignore[secret-interpolation] the token in this test is fake
sh "deploy --token ${TOKEN}"
References#
- Jenkins: Interpolation of sensitive environment variables
- Jenkins: Injection via interpolation
- Jenkins: Handling credentials