SAP / jenkins-library

Jenkins shared library for Continuous Delivery pipelines.
https://www.project-piper.io
Apache License 2.0
781 stars 594 forks source link

Escape Characters in Bash Command Parameters #32

Closed daniel-kurzynski closed 5 years ago

daniel-kurzynski commented 6 years ago

As described in https://github.com/SAP/jenkins-library/issues/21 certain parameters in bash commands, e.g. paths or passwords, can contain spaces or other special characters. These have to be escaped.

There are two discussed solutions from https://github.com/SAP/jenkins-library/pull/18:

  1. A BashEscapeUtil adds surrounding single quotes and escapes other single quotes.
String escape(String str) {
    return "\"${str.replace("\"", "\\\"")}\""
}
  1. A BashEscapeUtil escapes all special characters and spaces.
marcusholl commented 6 years ago

I personally prefer strongly a solution where the quotes (regardless of single or double quotes) are contained visibly in the shell script. This applies also in case these quotes inside the shell script needs to be escaped (\"). Reason: in this case it is transparent how the parameter is handled on shell level. Example:

sh "echo \"${param}\""

where param is a groovy parameter containing a shell-escaped value is much more clearer as

sh "echo ${param}"

where param is a groovy parameter containing a shell-escaped and double quote padded value. In the first case the reader can immediately see the parameters is handled on shell level with expansion in the second additional investigation is required.

A BashEscapeUtil adds surrounding single quotes and escapes other single quotes.

To my knowledge is simply not possible to have a single quote in a value padded with single quotes, even if the single quote contained in the value is escaped. But I'm happy to learn new things.

Since it is - to my knowledge - not possible to have single quotes in a values padded by single quotes the only (?) feasible way is to have an escape util which escapes a string so that it fits into being used surrounded by double quotes.

In case we have some escape method which also adds quoting (which I personally would like to avoid) we should choose an appropriate name like BashUtil.surroundWithDoubleQuotesAndEscape(./.)

References:

OliverNocon commented 6 years ago

After discussion with @marcusholl we should add a escape function to make surethat usernames & passwords can contain characters like "$'\n ...:

This can be achieved with the following

def escapedString = str.replace("'", "'\"'\"'")

Since the function also adds surrounding single quotes we should adapt the method naming accordingly, e.g. escapeAndQuote

We need to fix steps like:

daniel-kurzynski commented 6 years ago

@OliverNocon We can just move our calls over: https://github.wdf.sap.corp/MA/cloud-s4-sdk-pipeline-lib/blob/master/src/com/sap/cloud/sdk/s4hana/pipeline/BashUtils.groovy

daniel-kurzynski commented 5 years ago

For neo it is solved with https://github.com/SAP/jenkins-library/pull/440 It also introduces a utils class for that purpose.

marcusholl commented 5 years ago

solved, see above