diff --git a/modules/nf-commons/src/main/nextflow/file/FileHelper.groovy b/modules/nf-commons/src/main/nextflow/file/FileHelper.groovy index 665829a644..47d5a72841 100644 --- a/modules/nf-commons/src/main/nextflow/file/FileHelper.groovy +++ b/modules/nf-commons/src/main/nextflow/file/FileHelper.groovy @@ -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 @@ -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) } diff --git a/modules/nf-commons/src/test/nextflow/file/FileHelperTest.groovy b/modules/nf-commons/src/test/nextflow/file/FileHelperTest.groovy index a6a2542d0c..72aeee017d 100644 --- a/modules/nf-commons/src/test/nextflow/file/FileHelperTest.groovy +++ b/modules/nf-commons/src/test/nextflow/file/FileHelperTest.groovy @@ -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: @@ -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' () {