jenkinsci / jenkinsci/docker-workflow-plugin

[JENKINS-41395] docker.build() should sanitize input for newlines

Open
#475 4 comments 0 reactions 0 assignees View on GitHub
component:docker-workflow-plugin imported-jira-issue priority:minor resolution:unresolved robustness
Dominant language
Java
Stars
527
Forks
422
Avg merge
10m
Merged PRs (30d)
1

Description

Using the following basic Jenkinsfile I accidentally broke all my docker.build() invocations in a very difficult to grok bug.

node('docker') {

/* Make sure we're starting with a pristine workspace */
deleteDir()

String imageName = 'jenkinsciinfra/terraform'
String commitHash
def container = null

stage('Checkout') {
checkout scm
commitHash = sh(returnStdout: true, script: 'git rev-parse --short origin/master')
}

stage('Build') {
container = docker.build("${imageName}:${commitHash}")
}
}

This would fail with:

[Pipeline] // stage

[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
[terraform] Running shell script
+ docker build -t jenkinsciinfra/terraform:f84fd39
"docker build" requires exactly 1 argument(s).
See 'docker build --help'.

Usage: docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile
[Pipeline] }
[Pipeline] // stage

The problem here is that the commitHash has a newline character hidden at the end of it; the fix is obviously to add .trim(), but the fact that docker.build() doesn't sanitize the imageName parameter means that the user isn't provided any useful feedback when the invalid input breaks the command.

I would suggest either sanitizing the imageName input, or run some validation to ensure that imageName is valid.

---
Originally reported by rtyler, imported from: docker.build() should sanitize input for newlines


  • status: Open
  • priority: Minor
  • component(s): docker-workflow-plugin
  • label(s): robustness
  • resolution: Unresolved
  • votes: 0
  • watchers: 6
  • imported: 2025-12-07

Raw content of original issue

Using the following basic Jenkinsfile I accidentally broke all my docker.build() invocations in a very difficult to grok bug.


node('docker') {

/* Make sure we're starting with a pristine workspace */
deleteDir()

String imageName = 'jenkinsciinfra/terraform'
String commitHash
def container = null

stage('Checkout') {
checkout scm
commitHash = sh(returnStdout: true, script: 'git rev-parse --short origin/master')
}

stage('Build') {
container = docker.build("${imageName}:${commitHash}")
}
}


This would fail with:


[Pipeline] // stage

[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
[terraform] Running shell script
+ docker build -t jenkinsciinfra/terraform:f84fd39
"docker build" requires exactly 1 argument(s).
See 'docker build --help'.

Usage: docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile
[Pipeline] }
[Pipeline] // stage


The problem here is that the commitHash has a newline character hidden at the end of it; the fix is obviously to add .trim(), but the fact that docker.build() doesn't sanitize the imageName parameter means that the user isn't provided any useful feedback when the invalid input breaks the command.

I would suggest either sanitizing the imageName input, or run some validation to ensure that imageName is valid.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.