Skip to content

Commit b6a42ad

Browse files
authored
Merge pull request #45086 from gsmet/sanitize-git-url
Sanitize remote URL in Info extension
2 parents 5df7b0e + 314abb1 commit b6a42ad

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.quarkus.info.deployment;
2+
3+
class GitUtil {
4+
5+
static String sanitizeRemoteUrl(String remoteUrl) {
6+
if (remoteUrl == null || remoteUrl.isBlank()) {
7+
return null;
8+
}
9+
10+
String sanitizedRemoteUrl = remoteUrl.trim();
11+
if (sanitizedRemoteUrl.startsWith("https://")) {
12+
int atSign = sanitizedRemoteUrl.indexOf('@');
13+
if (atSign > 0) {
14+
sanitizedRemoteUrl = "https://" + sanitizedRemoteUrl.substring(atSign + 1);
15+
}
16+
} else if (sanitizedRemoteUrl.startsWith("http://")) {
17+
int atSign = sanitizedRemoteUrl.indexOf('@');
18+
if (atSign > 0) {
19+
sanitizedRemoteUrl = "http://" + sanitizedRemoteUrl.substring(atSign + 1);
20+
}
21+
} else {
22+
int atSign = sanitizedRemoteUrl.indexOf('@');
23+
if (atSign > 0) {
24+
sanitizedRemoteUrl = sanitizedRemoteUrl.substring(atSign + 1);
25+
}
26+
}
27+
28+
return sanitizedRemoteUrl;
29+
}
30+
}

extensions/info/deployment/src/main/java/io/quarkus/info/deployment/InfoProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ void gitInfo(InfoBuildTimeConfig config,
112112

113113
commit.put("id", id);
114114

115-
data.put("remote", git.getRepository().getConfig().getString("remote", "origin", "url"));
115+
data.put("remote",
116+
GitUtil.sanitizeRemoteUrl(git.getRepository().getConfig().getString("remote", "origin", "url")));
116117
data.put("tags", getTags(git, latestCommit));
117118
}
118119

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.quarkus.info.deployment;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class GitUtilTest {
9+
10+
@Test
11+
public void testSanitizeRemoteUrl() {
12+
assertNull(GitUtil.sanitizeRemoteUrl(null));
13+
assertNull(GitUtil.sanitizeRemoteUrl(""));
14+
assertNull(GitUtil.sanitizeRemoteUrl(" "));
15+
assertEquals("github.com:gsmet/quarkusio.github.io.git",
16+
GitUtil.sanitizeRemoteUrl("[email protected]:gsmet/quarkusio.github.io.git"));
17+
assertEquals("github.com:gsmet/quarkusio.github.io.git",
18+
GitUtil.sanitizeRemoteUrl(" [email protected]:gsmet/quarkusio.github.io.git "));
19+
assertEquals("https://github.com/gsmet/quarkusio.github.io.git",
20+
GitUtil.sanitizeRemoteUrl("https://github.com/gsmet/quarkusio.github.io.git"));
21+
assertEquals("https://github.com/gsmet/quarkusio.github.io.git",
22+
GitUtil.sanitizeRemoteUrl("https://gsmet:[email protected]/gsmet/quarkusio.github.io.git"));
23+
assertEquals("http://github.com/gsmet/quarkusio.github.io.git",
24+
GitUtil.sanitizeRemoteUrl("http://gsmet:[email protected]/gsmet/quarkusio.github.io.git"));
25+
}
26+
}

0 commit comments

Comments
 (0)