Skip to content

The Unified and Autonomous Development Platform

Fix issue #856 - Automatic escaping of characters in secret during git sync

Robin Shenf5805a47546f
diff --git a/server-core/src/main/java/io/onedev/server/buildspec/step/SyncRepository.java b/server-core/src/main/java/io/onedev/server/buildspec/step/SyncRepository.java
index 68b17999f0..60b017225b 100644
--- a/server-core/src/main/java/io/onedev/server/buildspec/step/SyncRepository.java
+++ b/server-core/src/main/java/io/onedev/server/buildspec/step/SyncRepository.java
@@ -1,5 +1,8 @@
package io.onedev.server.buildspec.step;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.stream.Collectors;
@@ -114,21 +117,27 @@ public abstract class SyncRepository extends ServerSideStep implements Validatab
}
public String getRemoteUrlWithCredential(Build build) {
- String password = null;
- if (getPasswordSecret() != null)
- password = build.getJobSecretAuthorizationContext().getSecretValue(getPasswordSecret());
+ String encodedPassword = null;
+ if (getPasswordSecret() != null) {
+ try {
+ String password = build.getJobSecretAuthorizationContext().getSecretValue(getPasswordSecret());
+ encodedPassword = URLEncoder.encode(password, StandardCharsets.UTF_8.name());
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+ }
String protocol = StringUtils.substringBefore(getRemoteUrl(), "//");
String hostAndPath = StringUtils.substringAfter(getRemoteUrl(), "//");
String remoteUrlWithCredentials = protocol + "//";
- if (getUserName() != null && password != null)
- remoteUrlWithCredentials += getUserName() + ":" + password + "@" + hostAndPath;
+ if (getUserName() != null && encodedPassword != null)
+ remoteUrlWithCredentials += getUserName() + ":" + encodedPassword + "@" + hostAndPath;
else if (getUserName() != null)
remoteUrlWithCredentials += getUserName() + "@" + hostAndPath;
- else if (password != null)
- remoteUrlWithCredentials += password + "@" + hostAndPath;
+ else if (encodedPassword != null)
+ remoteUrlWithCredentials += encodedPassword + "@" + hostAndPath;
else
remoteUrlWithCredentials += hostAndPath;