Skip to content

Commit

Permalink
Fix special chars escaping in container env
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
pditommaso committed Mar 11, 2023
1 parent 9fa8d75 commit 0d59b4c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import nextflow.script.params.ValueOutParam
import nextflow.util.ArrayBag
import nextflow.util.BlankSeparatedList
import nextflow.util.CacheHelper
import nextflow.util.Escape
import nextflow.util.LockManager
import nextflow.util.LoggerHelper
import nextflow.util.TestOnly
Expand Down Expand Up @@ -1961,7 +1962,7 @@ class TaskProcessor {
}
else {
// escape both wrapping double quotes and the dollar var placeholder
script << /export $name="${value.replace('$','\\$')}"/
script << /export $name="${Escape.variable(value)}"/
}
}
script << ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,9 +779,12 @@ class TaskProcessorTest extends Specification {
.stripIndent().leftTrim()

when:
env = TaskProcessor.bashEnvironmentScript([PATH: 'foo:$PATH'], true)
env = TaskProcessor.bashEnvironmentScript([PATH: 'foo:$PATH', HOLA: 'one|two'], true)
then:
env.trim() == 'export PATH="foo:\\$PATH"'
env == '''\
export PATH="foo:\\$PATH"
export HOLA="one\\|two"
'''.stripIndent()
env.charAt(env.size()-1) == '\n' as char

when:
Expand Down
4 changes: 2 additions & 2 deletions modules/nf-commons/src/main/nextflow/Const.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class Const {
/**
* The app build time as linux/unix timestamp
*/
static public final long APP_TIMESTAMP = 1677436983659
static public final long APP_TIMESTAMP = 1678524585748

/**
* The app build number
*/
static public final int APP_BUILDNUM = 5845
static public final int APP_BUILDNUM = 5846

/**
* The app build time string relative to UTC timezone
Expand Down
6 changes: 6 additions & 0 deletions modules/nf-commons/src/main/nextflow/util/Escape.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Escape {

private static List<String> SPECIAL_CHARS = ["'", '"', ' ', '(', ')', '\\', '!', '&', '|', '<', '>', '`', ':']

private static List<String> VAR_CHARS = ['$', "'", '"', '(', ')', '\\', '&', '|', '<', '>', '`']

private static List<String> WILDCARDS = ["*", "?", "{", "}", "[", "]", "'", '"', ' ', '(', ')', '\\', '!', '&', '|', '<', '>', '`', ':']

private static String replace(List<String> special, String str, boolean doNotEscapeComplement=false) {
Expand Down Expand Up @@ -104,4 +106,8 @@ class Escape {
.replaceAll('\f',/\\f/)

}

static String variable(String val) {
replace(VAR_CHARS, val, false)
}
}
20 changes: 20 additions & 0 deletions modules/nf-commons/src/test/nextflow/util/EscapeTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,24 @@ class EscapeTest extends Specification {
'foo\f' | 'foo\\f'
'foo\r' | 'foo\\r'
}

def 'should escape special char' () {
expect:
Escape.variable(STR) == EXPECT
where:
STR | EXPECT
'foo' | 'foo'
'foo[x]bar' | 'foo[x]bar'
'foo ' | 'foo '
'foo:bar' | 'foo:bar'
'foo!bar' | 'foo!bar'
'foo[!x]bar'| 'foo[!x]bar'
and:
'$foo' | '\\$foo'
'foo|bar' | 'foo\\|bar'
'foo`bar' | 'foo\\`bar'
'foo&bar' | 'foo\\&bar'
'foo(x)bar' | 'foo\\(x\\)bar'
'foo<x>bar' | 'foo\\<x\\>bar'
}
}

0 comments on commit 0d59b4c

Please sign in to comment.