Skip to content

Commit

Permalink
Improve paths validation
Browse files Browse the repository at this point in the history
  • Loading branch information
pditommaso committed Dec 6, 2021
1 parent 4daf8d0 commit 0849d58
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
6 changes: 5 additions & 1 deletion modules/nf-commons/src/main/nextflow/file/FileHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ class FileHelper {
static Path asPath( String str ) {
if( !str )
throw new IllegalArgumentException("Path string cannot be empty")
if( str != Bolts.leftTrim(str) )
throw new IllegalArgumentException("Path string cannot start with blank or a special characters -- Offending path: '${Escape.blanks(str)}'")
if( str != Bolts.rightTrim(str) )
throw new IllegalArgumentException("Path string cannot ends with blank or a special characters -- Offending path: '${Escape.blanks(str)}'")

if( !str.contains(':/') ) {
return Paths.get(str)
Expand All @@ -267,7 +271,7 @@ class FileHelper {
return result
}

asPath(toPathURI(str))
return asPath(toPathURI(str))
}

static private Map<String,String> PLUGINS_MAP = [s3:'nf-amazon', gs:'nf-google', az:'nf-azure']
Expand Down
9 changes: 9 additions & 0 deletions modules/nf-commons/src/main/nextflow/util/Escape.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,13 @@ class Escape {
loc = loc.substring(0,loc.length()-1)
return prefix + path(loc)
}

static String blanks(String str) {
str
.replaceAll('\n',/\\n/)
.replaceAll('\t',/\\t/)
.replaceAll('\r',/\\r/)
.replaceAll('\f',/\\f/)

}
}
20 changes: 19 additions & 1 deletion modules/nf-commons/src/test/nextflow/file/FileHelperTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,26 @@ class FileHelperTest extends Specification {
when:
FileHelper.asPath('file://some/file.txt')
then:
thrown(IllegalArgumentException)
def e = thrown(IllegalArgumentException)
e.message == 'Malformed file URI: file://some/file.txt -- It must start either with a `file:/` or `file:///` prefix'

when:
FileHelper.asPath('')
then:
e = thrown(IllegalArgumentException)
e.message == 'Path string cannot be empty'

when:
FileHelper.asPath('\n/some/file.txt')
then:
e = thrown(IllegalArgumentException)
e.message == "Path string cannot start with blank or a special characters -- Offending path: '\\n/some/file.txt'"

when:
FileHelper.asPath('/some/file.txt\n')
then:
e = thrown(IllegalArgumentException)
e.message == "Path string cannot ends with blank or a special characters -- Offending path: '/some/file.txt\\n'"
}

def 'should strip query params from http files' () {
Expand Down
13 changes: 13 additions & 0 deletions modules/nf-commons/src/test/nextflow/util/EscapeTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package nextflow.util
import java.nio.file.Paths

import spock.lang.Specification
import spock.lang.Unroll

/**
*
Expand Down Expand Up @@ -82,4 +83,16 @@ class EscapeTest extends Specification {
Escape.cli('nextflow','--foo','a[!b-c]') == "nextflow --foo 'a[!b-c]'"
}

@Unroll
def 'should escape blanks' () {
expect:
Escape.blanks(STR) == EXPECT
where:
STR | EXPECT
'foo ' | 'foo '
'foo\n' | 'foo\\n'
'foo\t' | 'foo\\t'
'foo\f' | 'foo\\f'
'foo\r' | 'foo\\r'
}
}

0 comments on commit 0849d58

Please sign in to comment.