Skip to content

Commit b93634e

Browse files
committed
Fix special chars escaping in container env
Signed-off-by: Paolo Di Tommaso <[email protected]>
1 parent 9fa8d75 commit b93634e

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

modules/nextflow/src/main/groovy/nextflow/processor/TaskProcessor.groovy

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ import nextflow.script.params.ValueOutParam
100100
import nextflow.util.ArrayBag
101101
import nextflow.util.BlankSeparatedList
102102
import nextflow.util.CacheHelper
103+
import nextflow.util.Escape
103104
import nextflow.util.LockManager
104105
import nextflow.util.LoggerHelper
105106
import nextflow.util.TestOnly
@@ -1961,7 +1962,7 @@ class TaskProcessor {
19611962
}
19621963
else {
19631964
// escape both wrapping double quotes and the dollar var placeholder
1964-
script << /export $name="${value.replace('$','\\$')}"/
1965+
script << /export $name="${Escape.variable(value)}"/
19651966
}
19661967
}
19671968
script << ''

modules/nextflow/src/test/groovy/nextflow/processor/TaskProcessorTest.groovy

+5-2
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,12 @@ class TaskProcessorTest extends Specification {
779779
.stripIndent().leftTrim()
780780

781781
when:
782-
env = TaskProcessor.bashEnvironmentScript([PATH: 'foo:$PATH'], true)
782+
env = TaskProcessor.bashEnvironmentScript([PATH: 'foo:$PATH', HOLA: 'one|two'], true)
783783
then:
784-
env.trim() == 'export PATH="foo:\\$PATH"'
784+
env == '''\
785+
export PATH="foo:\\$PATH"
786+
export HOLA="one\\|two"
787+
'''.stripIndent()
785788
env.charAt(env.size()-1) == '\n' as char
786789

787790
when:

modules/nf-commons/src/main/nextflow/util/Escape.groovy

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Escape {
3232

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

35+
private static List<String> VAR_CHARS = ['$', "'", '"', '(', ')', '\\', '&', '|', '<', '>', '`']
36+
3537
private static List<String> WILDCARDS = ["*", "?", "{", "}", "[", "]", "'", '"', ' ', '(', ')', '\\', '!', '&', '|', '<', '>', '`', ':']
3638

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

106108
}
109+
110+
static String variable(String val) {
111+
replace(VAR_CHARS, val, false)
112+
}
107113
}

modules/nf-commons/src/test/nextflow/util/EscapeTest.groovy

+20
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,24 @@ class EscapeTest extends Specification {
9595
'foo\f' | 'foo\\f'
9696
'foo\r' | 'foo\\r'
9797
}
98+
99+
def 'should escape special char' () {
100+
expect:
101+
Escape.variable(STR) == EXPECT
102+
where:
103+
STR | EXPECT
104+
'foo' | 'foo'
105+
'foo[x]bar' | 'foo[x]bar'
106+
'foo ' | 'foo '
107+
'foo:bar' | 'foo:bar'
108+
'foo!bar' | 'foo!bar'
109+
'foo[!x]bar'| 'foo[!x]bar'
110+
and:
111+
'$foo' | '\\$foo'
112+
'foo|bar' | 'foo\\|bar'
113+
'foo`bar' | 'foo\\`bar'
114+
'foo&bar' | 'foo\\&bar'
115+
'foo(x)bar' | 'foo\\(x\\)bar'
116+
'foo<x>bar' | 'foo\\<x\\>bar'
117+
}
98118
}

0 commit comments

Comments
 (0)