Skip to content

Commit

Permalink
Improve error message on invalid file url prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <paolo.ditommaso@gmail.com>
  • Loading branch information
pditommaso committed Oct 13, 2023
1 parent 6672c6d commit 82a3f40
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/nf-commons/src/main/nextflow/file/FileHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class FileHelper {

static final public Pattern URL_PROTOCOL = ~/^([a-zA-Z][a-zA-Z0-9]*):\\/\\/.+/

static final public Pattern INVALID_URL_PREFIX = ~/^(?!file)([a-zA-Z][a-zA-Z0-9]*):\\/[^\\/].+/

static final private Pattern BASE_URL = ~/(?i)((?:[a-z][a-zA-Z0-9]*)?:\/\/[^:|\/]+(?::\d*)?)(?:$|\/.*)/

static final private Path localTempBasePath
Expand Down Expand Up @@ -288,6 +290,10 @@ class FileHelper {
return Paths.get(str)
}

// check for valid the url scheme
if( INVALID_URL_PREFIX.matcher(str).matches() )
throw new IllegalArgumentException("File path is prefixed with an invalid URL scheme - Offending path: '${Escape.blanks(str)}'")

return asPath0(str)
}

Expand Down
29 changes: 29 additions & 0 deletions modules/nf-commons/src/test/nextflow/file/FileHelperTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@ class FileHelperTest extends Specification {
Files.createTempDirectory(tmp, 'test')
}

def 'should match invalid url prefix' (){
expect:
FileHelper.INVALID_URL_PREFIX.matcher(STR).matches() == EXPECTED

where:
EXPECTED| STR
true | 's3:/foo'
true | 'az:/foo'
true | 'gs:/foo'
true | 'xyz:/foo'
and:
false | 'file:/foo' // <- this is ok
and:
false | 's3://foo'
false | 'az://foo'
false | 'gs://foo'
false | 'http://foo'
false | 'https://foo'
false | 'ftp://foo'
false | 'xyz://foo'

}

def 'should return a Path object' () {

expect:
Expand Down Expand Up @@ -79,6 +102,12 @@ class FileHelperTest extends Specification {
then:
e = thrown(IllegalArgumentException)
e.message == "Path string cannot ends with a blank or special characters -- Offending path: '/some/file.txt\\n'"

when:
FileHelper.asPath('s3:/some/broken/url')
then:
e = thrown(IllegalArgumentException)
e.message == "File path is prefixed with an invalid URL scheme - Offending path: 's3:/some/broken/url'"
}

def 'should strip query params from http files' () {
Expand Down

0 comments on commit 82a3f40

Please sign in to comment.