Skip to content

Commit 7894e09

Browse files
jorgeepditommaso
andauthored
Add URL encoding when revision name is used as HTTP query parameter (#6598)
Signed-off-by: jorgee <[email protected]> Signed-off-by: Paolo Di Tommaso <[email protected]> Co-authored-by: Paolo Di Tommaso <[email protected]>
1 parent 3421734 commit 7894e09

11 files changed

+90
-13
lines changed

modules/nextflow/src/main/groovy/nextflow/scm/AzureRepositoryProvider.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package nextflow.scm
1818

1919
import java.net.http.HttpResponse
20+
import java.nio.charset.StandardCharsets
2021
import java.util.regex.Pattern
2122

2223
import groovy.transform.CompileDynamic
@@ -97,7 +98,7 @@ final class AzureRepositoryProvider extends RepositoryProvider {
9798
'path':path
9899
] as Map<String,Object>
99100
if( revision ) {
100-
queryParams['versionDescriptor.version']=revision
101+
queryParams['versionDescriptor.version']=URLEncoder.encode(revision, StandardCharsets.UTF_8)
101102

102103
if( COMMIT_REGEX.matcher(revision).matches() )
103104
queryParams['versionDescriptor.versionType'] = 'commit'
@@ -203,7 +204,7 @@ final class AzureRepositoryProvider extends RepositoryProvider {
203204
] as Map<String,Object>
204205

205206
if( revision ) {
206-
queryParams['versionDescriptor.version'] = revision
207+
queryParams['versionDescriptor.version'] = URLEncoder.encode(revision, StandardCharsets.UTF_8)
207208
if( COMMIT_REGEX.matcher(revision).matches() )
208209
queryParams['versionDescriptor.versionType'] = 'commit'
209210
}
@@ -236,7 +237,7 @@ final class AzureRepositoryProvider extends RepositoryProvider {
236237
}
237238

238239
if (revision) {
239-
queryParams['versionDescriptor.version'] = revision
240+
queryParams['versionDescriptor.version'] = URLEncoder.encode(revision, StandardCharsets.UTF_8)
240241
if (COMMIT_REGEX.matcher(revision).matches()) {
241242
queryParams['versionDescriptor.versionType'] = 'commit'
242243
}

modules/nextflow/src/main/groovy/nextflow/scm/BitbucketServerRepositoryProvider.groovy

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import groovy.json.JsonSlurper
2121
import groovy.transform.Memoized
2222
import groovy.util.logging.Slf4j
2323
import nextflow.exception.AbortOperationException
24+
25+
import java.nio.charset.StandardCharsets
26+
2427
/**
2528
* Implements a repository provider for the private hosted BitBucket Server service
2629
*
@@ -73,7 +76,7 @@ final class BitbucketServerRepositoryProvider extends RepositoryProvider {
7376
//
7477
def result = "${config.endpoint}/rest/api/1.0/projects/${project}/repos/${repository}/raw/${path}"
7578
if( revision )
76-
result += "?at=$revision"
79+
result += "?at=${URLEncoder.encode(revision, StandardCharsets.UTF_8)}"
7780
return result
7881
}
7982

modules/nextflow/src/main/groovy/nextflow/scm/GiteaRepositoryProvider.groovy

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ package nextflow.scm
2020
import groovy.transform.CompileDynamic
2121
import groovy.transform.CompileStatic
2222
import groovy.util.logging.Slf4j
23+
24+
import java.nio.charset.StandardCharsets
25+
2326
/**
2427
* Implements a repository provider for Gitea service
2528
*
@@ -86,7 +89,7 @@ final class GiteaRepositoryProvider extends RepositoryProvider {
8689
// note: `ref` is undocumented
8790
def result = "${config.endpoint}/repos/$project/raw/$path"
8891
if( revision )
89-
result += "?ref=$revision"
92+
result += "?ref=${URLEncoder.encode(revision, StandardCharsets.UTF_8)}"
9093
return result
9194
}
9295

@@ -118,7 +121,7 @@ final class GiteaRepositoryProvider extends RepositoryProvider {
118121
/** {@inheritDoc} */
119122
@Override
120123
List<RepositoryEntry> listDirectory(String path, int depth) {
121-
final branch = revision ?: "master"
124+
final branch = URLEncoder.encode(revision ?: "master", StandardCharsets.UTF_8)
122125
// Normalize path using base class helper
123126
final dirPath = normalizePath(path)
124127

modules/nextflow/src/main/groovy/nextflow/scm/GithubRepositoryProvider.groovy

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import groovy.transform.Memoized
2222
import groovy.util.logging.Slf4j
2323
import nextflow.SysEnv
2424

25+
import java.nio.charset.StandardCharsets
26+
2527
/**
2628
* Implements a repository provider for GitHub service
2729
*
@@ -69,7 +71,7 @@ class GithubRepositoryProvider extends RepositoryProvider {
6971
//
7072
def result = "${config.endpoint}/repos/$project/contents/$path"
7173
if( revision )
72-
result += "?ref=$revision"
74+
result += "?ref=${URLEncoder.encode(revision, StandardCharsets.UTF_8)}"
7375
return result
7476
}
7577

@@ -205,7 +207,8 @@ class GithubRepositoryProvider extends RepositoryProvider {
205207
if (revision) {
206208
// Try to resolve the revision to a commit SHA
207209
try {
208-
Map ref = invokeAndParseResponse("${config.endpoint}/repos/$project/git/refs/heads/$revision")
210+
211+
Map ref = invokeAndParseResponse("${config.endpoint}/repos/$project/git/refs/heads/${URLEncoder.encode(revision, StandardCharsets.UTF_8)}")
209212
Map object = ref.get('object') as Map
210213
return object.get('sha') as String
211214
} catch (Exception e) {

modules/nextflow/src/main/groovy/nextflow/scm/GitlabRepositoryProvider.groovy

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package nextflow.scm
1818

1919
import groovy.json.JsonSlurper
2020
import groovy.util.logging.Slf4j
21+
22+
import java.nio.charset.StandardCharsets
23+
2124
/**
2225
* Implements a repository provider for GitHub service
2326
*
@@ -94,8 +97,8 @@ class GitlabRepositoryProvider extends RepositoryProvider {
9497
// see
9598
// https://docs.gitlab.com/ee/api/repository_files.html#get-raw-file-from-repository
9699
//
97-
final ref = revision ?: getDefaultBranch()
98-
final encodedPath = URLEncoder.encode(path.stripStart('/'),'utf-8')
100+
final ref = URLEncoder.encode(revision ?: getDefaultBranch(), StandardCharsets.UTF_8)
101+
final encodedPath = URLEncoder.encode(path.stripStart('/'), StandardCharsets.UTF_8)
99102
return "${config.endpoint}/api/v4/projects/${getProjectName()}/repository/files/${encodedPath}?ref=${ref}"
100103
}
101104

@@ -128,9 +131,9 @@ class GitlabRepositoryProvider extends RepositoryProvider {
128131
/** {@inheritDoc} */
129132
@Override
130133
List<RepositoryEntry> listDirectory(String path, int depth) {
131-
final ref = revision ?: getDefaultBranch()
134+
final ref = URLEncoder.encode(revision ?: getDefaultBranch(),StandardCharsets.UTF_8)
132135
final normalizedPath = normalizePath(path)
133-
final encodedPath = normalizedPath ? URLEncoder.encode(normalizedPath, 'utf-8') : ""
136+
final encodedPath = normalizedPath ? URLEncoder.encode(normalizedPath, StandardCharsets.UTF_8) : ""
134137

135138
// Build the Tree API URL
136139
String url = "${config.endpoint}/api/v4/projects/${getProjectName()}/repository/tree"

modules/nextflow/src/test/groovy/nextflow/scm/AzureRepositoryProviderTest.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class AzureRepositoryProviderTest extends Specification {
112112

113113
expect:
114114
new AzureRepositoryProvider('t-neumann/hello', obj).setRevision("a-branch").getContentUrl('main.nf') == 'https://dev.azure.com/t-neumann/hello/_apis/git/repositories/hello/items?download=false&includeContent=true&includeContentMetadata=false&api-version=6.0&\$format=json&path=main.nf&versionDescriptor.version=a-branch'
115+
and:
116+
new AzureRepositoryProvider('t-neumann/hello', obj).setRevision("test/branch+with&strangecharacters").getContentUrl('main.nf') == 'https://dev.azure.com/t-neumann/hello/_apis/git/repositories/hello/items?download=false&includeContent=true&includeContentMetadata=false&api-version=6.0&\$format=json&path=main.nf&versionDescriptor.version=test%2Fbranch%2Bwith%26strangecharacters'
115117
}
116118

117119
/*
@@ -133,6 +135,8 @@ class AzureRepositoryProviderTest extends Specification {
133135
def result = repo.readText('main.nf')
134136
then:
135137
result == 'println "Hello from Azure repos!"'
138+
139+
136140
}
137141

138142
@IgnoreIf({System.getenv('NXF_SMOKE')})
@@ -226,6 +230,15 @@ class AzureRepositoryProviderTest extends Specification {
226230
def result = repo.readText('file-on-dev.txt')
227231
then:
228232
result=='hello\n'
233+
234+
when:
235+
// check revision with special branches
236+
repo = new AzureRepositoryProvider('pditommaso/nf-azure-repo', config)
237+
repo.revision = 'test/branch+with&special-chars'
238+
result = repo.readText('main.nf')
239+
then:
240+
result == 'println "Hello from Azure repos!"'
241+
229242
}
230243

231244
@IgnoreIf({System.getenv('NXF_SMOKE')})

modules/nextflow/src/test/groovy/nextflow/scm/BitbucketRepositoryProviderTest.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ class BitbucketRepositoryProviderTest extends Specification {
123123
.setRevision('test/tag/v2')
124124
.getContentUrl('main.nf') == 'https://api.bitbucket.org/2.0/repositories/pditommaso/tutorial/src/8f849beceb2ea479ef836809ca33d3daeeed25f9/main.nf'
125125

126+
and:
127+
new BitbucketRepositoryProvider('pditommaso/tutorial', config)
128+
.setRevision('test/branch+with&special-chars')
129+
.getContentUrl('main.nf') == 'https://api.bitbucket.org/2.0/repositories/pditommaso/tutorial/src/755ba829cbc4f28dcb3c16b9dcc1c49c7ee47ff5/main.nf'
126130
}
127131

128132
@Requires( { System.getenv('NXF_BITBUCKET_ACCESS_TOKEN') } )
@@ -171,6 +175,14 @@ class BitbucketRepositoryProviderTest extends Specification {
171175
then:
172176
!data.contains('world')
173177
data.contains('mundo')
178+
179+
when:
180+
repo.setRevision('test/branch+with&special-chars')
181+
and:
182+
data = repo.readText('main.nf')
183+
then:
184+
data.contains('world')
185+
!data.contains('WORLD')
174186
}
175187

176188
@Unroll

modules/nextflow/src/test/groovy/nextflow/scm/BitbucketServerRepositoryProviderTest.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ class BitbucketServerRepositoryProviderTest extends Specification {
9494
new BitbucketServerRepositoryProvider('pditommaso/hello', obj)
9595
.setRevision('foo')
9696
.getContentUrl('main.nf') == 'https://bitbucket.server.com/rest/api/1.0/projects/pditommaso/repos/hello/raw/main.nf?at=foo'
97+
and:
98+
new BitbucketServerRepositoryProvider('pditommaso/hello', obj)
99+
.setRevision('test/branch+with&strangecharacters')
100+
.getContentUrl('main.nf') == 'https://bitbucket.server.com/rest/api/1.0/projects/pditommaso/repos/hello/raw/main.nf?at=test%2Fbranch%2Bwith%26strangecharacters'
97101

98102
}
99103

modules/nextflow/src/test/groovy/nextflow/scm/GiteaRepositoryProviderTest.groovy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ class GiteaRepositoryProviderTest extends Specification {
5454
new GiteaRepositoryProvider('pditommaso/hello', obj)
5555
.setRevision('12345')
5656
.getContentUrl('main.nf') == 'https://gitea.com/api/v1/repos/pditommaso/hello/raw/main.nf?ref=12345'
57-
57+
and:
58+
new GiteaRepositoryProvider('pditommaso/hello', obj)
59+
.setRevision('test/branch+with&strangecharacters')
60+
.getContentUrl('main.nf') == 'https://gitea.com/api/v1/repos/pditommaso/hello/raw/main.nf?ref=test%2Fbranch%2Bwith%26strangecharacters'
5861
}
5962

6063
@Unroll
@@ -92,6 +95,13 @@ class GiteaRepositoryProviderTest extends Specification {
9295
// result = repo.readText('README.md')
9396
// then:
9497
// result.contains("foo branch")
98+
99+
when:
100+
repo = new GiteaRepositoryProvider('pditommaso/test-hello', config)
101+
repo.setRevision('test/branch+with&special-chars')
102+
result = repo.readText('README.md')
103+
then:
104+
result.contains('Basic Nextflow script')
95105
}
96106

97107
@IgnoreIf({System.getenv('NXF_SMOKE')})

modules/nextflow/src/test/groovy/nextflow/scm/GithubRepositoryProviderTest.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ class GithubRepositoryProviderTest extends Specification {
5252
def result = repo.readText('main.nf')
5353
then:
5454
result.trim().startsWith('#!/usr/bin/env nextflow')
55+
56+
//Read from branch
57+
when:
58+
repo.setRevision('test/branch+with&strangecharacters')
59+
result = repo.readText('/test/branch_name')
60+
then:
61+
result.trim().startsWith('test/branch+with&strangecharacters')
62+
5563
}
5664

5765
@Requires({System.getenv('NXF_GITHUB_ACCESS_TOKEN')})
@@ -95,6 +103,11 @@ class GithubRepositoryProviderTest extends Specification {
95103
.setRevision('the-commit-id')
96104
.getContentUrl('main.nf') == 'https://github.com/repos/pditommaso/hello/contents/main.nf?ref=the-commit-id'
97105

106+
and:
107+
new GithubRepositoryProvider('pditommaso/hello', obj)
108+
.setRevision('test/branch+with&strangecharacters')
109+
.getContentUrl('main.nf') == 'https://github.com/repos/pditommaso/hello/contents/main.nf?ref=test%2Fbranch%2Bwith%26strangecharacters'
110+
98111
}
99112

100113
def 'should user github token as creds' () {

0 commit comments

Comments
 (0)