Skip to content

Commit

Permalink
Merge pull request quarkusio#45086 from gsmet/sanitize-git-url
Browse files Browse the repository at this point in the history
Sanitize remote URL in Info extension
  • Loading branch information
gsmet authored Dec 12, 2024
2 parents 5df7b0e + 314abb1 commit b6a42ad
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.info.deployment;

class GitUtil {

static String sanitizeRemoteUrl(String remoteUrl) {
if (remoteUrl == null || remoteUrl.isBlank()) {
return null;
}

String sanitizedRemoteUrl = remoteUrl.trim();
if (sanitizedRemoteUrl.startsWith("https://")) {
int atSign = sanitizedRemoteUrl.indexOf('@');
if (atSign > 0) {
sanitizedRemoteUrl = "https://" + sanitizedRemoteUrl.substring(atSign + 1);
}
} else if (sanitizedRemoteUrl.startsWith("http://")) {
int atSign = sanitizedRemoteUrl.indexOf('@');
if (atSign > 0) {
sanitizedRemoteUrl = "http://" + sanitizedRemoteUrl.substring(atSign + 1);
}
} else {
int atSign = sanitizedRemoteUrl.indexOf('@');
if (atSign > 0) {
sanitizedRemoteUrl = sanitizedRemoteUrl.substring(atSign + 1);
}
}

return sanitizedRemoteUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ void gitInfo(InfoBuildTimeConfig config,

commit.put("id", id);

data.put("remote", git.getRepository().getConfig().getString("remote", "origin", "url"));
data.put("remote",
GitUtil.sanitizeRemoteUrl(git.getRepository().getConfig().getString("remote", "origin", "url")));
data.put("tags", getTags(git, latestCommit));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.info.deployment;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;

public class GitUtilTest {

@Test
public void testSanitizeRemoteUrl() {
assertNull(GitUtil.sanitizeRemoteUrl(null));
assertNull(GitUtil.sanitizeRemoteUrl(""));
assertNull(GitUtil.sanitizeRemoteUrl(" "));
assertEquals("github.com:gsmet/quarkusio.github.io.git",
GitUtil.sanitizeRemoteUrl("[email protected]:gsmet/quarkusio.github.io.git"));
assertEquals("github.com:gsmet/quarkusio.github.io.git",
GitUtil.sanitizeRemoteUrl(" [email protected]:gsmet/quarkusio.github.io.git "));
assertEquals("https://github.com/gsmet/quarkusio.github.io.git",
GitUtil.sanitizeRemoteUrl("https://github.com/gsmet/quarkusio.github.io.git"));
assertEquals("https://github.com/gsmet/quarkusio.github.io.git",
GitUtil.sanitizeRemoteUrl("https://gsmet:[email protected]/gsmet/quarkusio.github.io.git"));
assertEquals("http://github.com/gsmet/quarkusio.github.io.git",
GitUtil.sanitizeRemoteUrl("http://gsmet:[email protected]/gsmet/quarkusio.github.io.git"));
}
}

0 comments on commit b6a42ad

Please sign in to comment.