Skip to content

Commit 0797e45

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

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

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

Lines changed: 2 additions & 1 deletion
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/executor/BashWrapperBuilderTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,8 @@ class BashWrapperBuilderTest extends Specification {
986986
nxf_container_env() {
987987
cat << EOF
988988
export FOO="hello"
989-
export BAR="hello world"
990-
export PATH="/some/path:\\$PATH"
989+
export BAR="hello\\ world"
990+
export PATH="/some/path\\:\\$PATH"
991991
EOF
992992
}
993993
'''

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

Lines changed: 5 additions & 2 deletions
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

Lines changed: 6 additions & 0 deletions
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> SPECIAL_CHARS_DOLLAR = SPECIAL_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(SPECIAL_CHARS_DOLLAR, val, false)
112+
}
107113
}

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

Lines changed: 20 additions & 0 deletions
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+
and:
107+
'foo ' | 'foo\\ '
108+
'$foo' | '\\$foo'
109+
'foo|bar' | 'foo\\|bar'
110+
'foo`bar' | 'foo\\`bar'
111+
'foo&bar' | 'foo\\&bar'
112+
'foo:bar' | 'foo\\:bar'
113+
'foo!bar' | 'foo\\!bar'
114+
'foo[!x]bar'| 'foo[\\!x]bar'
115+
'foo(x)bar' | 'foo\\(x\\)bar'
116+
'foo<x>bar' | 'foo\\<x\\>bar'
117+
}
98118
}

0 commit comments

Comments
 (0)