Skip to content

Commit c2997b8

Browse files
committed
Merge pull request #241 from damnhandy/master
Added code to ensure that passwords are not included git.remote.origin.url
2 parents 80abddb + a3bcc35 commit c2997b8

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed

src/main/java/pl/project13/maven/git/GitDataProvider.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717

1818
package pl.project13.maven.git;
1919

20+
import org.apache.http.client.utils.URIBuilder;
2021
import org.jetbrains.annotations.NotNull;
2122
import pl.project13.maven.git.log.LoggerBridge;
2223
import pl.project13.maven.git.util.PropertyManager;
2324

24-
import java.io.IOException;
25+
import java.net.URI;
26+
import java.net.URISyntaxException;
2527
import java.util.Map;
2628
import java.util.Properties;
2729
import java.util.TimeZone;
2830
import java.text.SimpleDateFormat;
31+
import java.util.regex.Pattern;
2932

3033
import static com.google.common.base.Strings.isNullOrEmpty;
3134

@@ -228,4 +231,48 @@ protected void put(@NotNull Properties properties, String key, String value) {
228231
log.info("{} {}", keyWithPrefix, value);
229232
PropertyManager.putWithoutPrefix(properties, keyWithPrefix, value);
230233
}
234+
235+
/**
236+
* Regex to check for SCP-style SSH+GIT connection strings such as '[email protected]'
237+
*/
238+
static final Pattern GIT_SCP_FORMAT = Pattern.compile("^([a-zA-Z0-9_.+-])+@(.*)");
239+
/**
240+
* If the git remote value is a URI and contains a user info component, strip the password from it if it exists.
241+
*
242+
* @param gitRemoteString The value of the git remote
243+
* @return
244+
* @throws GitCommitIdExecutionException
245+
*/
246+
protected static String stripCredentialsFromOriginUrl(String gitRemoteString) throws GitCommitIdExecutionException {
247+
248+
// The URL might be null if the repo hasn't set a remote
249+
if (gitRemoteString == null) {
250+
return gitRemoteString;
251+
}
252+
253+
// Remotes using ssh connection strings in the 'git@github' format aren't
254+
// proper URIs and won't parse . Plus since you should be using SSH keys,
255+
// credentials like are not in the URL.
256+
if (GIT_SCP_FORMAT.matcher(gitRemoteString).matches()) {
257+
return gitRemoteString;
258+
}
259+
// At this point, we should have a properly formatted URL
260+
try {
261+
URI original = new URI(gitRemoteString);
262+
String userInfoString = original.getUserInfo();
263+
if (null == userInfoString) {
264+
return gitRemoteString;
265+
}
266+
URIBuilder b = new URIBuilder(gitRemoteString);
267+
String[] userInfo = userInfoString.split(":");
268+
// Build a new URL from the original URL, but nulling out the password
269+
// component of the userinfo. We keep the username so that ssh uris such
270+
// ssh://[email protected] will retain 'git@'.
271+
b.setUserInfo(userInfo[0]);
272+
return b.build().toString();
273+
274+
} catch (URISyntaxException e) {
275+
throw new GitCommitIdExecutionException(e);
276+
}
277+
}
231278
}

src/main/java/pl/project13/maven/git/JGitProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ protected String getCommitTime() throws GitCommitIdExecutionException {
167167

168168
@Override
169169
protected String getRemoteOriginUrl() throws GitCommitIdExecutionException {
170-
return git.getConfig().getString("remote", "origin", "url");
170+
String url = git.getConfig().getString("remote", "origin", "url");
171+
return stripCredentialsFromOriginUrl(url);
171172
}
172173

173174
@Override

src/main/java/pl/project13/maven/git/NativeGitProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private String getOriginRemote(File directory) throws GitCommitIdExecutionExcept
249249
remoteUrl = split[1];
250250
}
251251
}
252-
return remoteUrl;
252+
return stripCredentialsFromOriginUrl(remoteUrl);
253253
}
254254

255255
/**
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package pl.project13.maven.git;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.apache.http.client.utils.URIBuilder;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.net.MalformedURLException;
10+
import java.net.URI;
11+
import java.net.URISyntaxException;
12+
import java.net.URL;
13+
14+
/**
15+
* Created by ryan on 3/21/16.
16+
*/
17+
public class UriUserInfoRemoverTest {
18+
19+
@Test
20+
public void testHttpsUriWithoutUserInfo() throws Exception {
21+
String result = GitDataProvider.stripCredentialsFromOriginUrl("https://example.com");
22+
assertEquals("https://example.com", result);
23+
}
24+
25+
@Test
26+
public void testHttpsUriWithUserInfo() throws Exception {
27+
String result = GitDataProvider.stripCredentialsFromOriginUrl("https://[email protected]");
28+
assertEquals("https://[email protected]", result);
29+
}
30+
31+
@Test
32+
public void testHttpsUriWithUserInfoAndPassword() throws Exception {
33+
String result = GitDataProvider.stripCredentialsFromOriginUrl("https://user:[email protected]");
34+
assertEquals("https://[email protected]", result);
35+
}
36+
37+
@Test
38+
public void testWithSCPStyleSSHProtocolGitHub() throws Exception {
39+
String result = GitDataProvider.stripCredentialsFromOriginUrl("[email protected]");
40+
assertEquals("[email protected]",result);
41+
}
42+
43+
@Test
44+
public void testWithSCPStyleSSHProtocol() throws Exception {
45+
String result = GitDataProvider.stripCredentialsFromOriginUrl("[email protected]:~user/path/to/repo.git");
46+
assertEquals("[email protected]:~user/path/to/repo.git",result);
47+
}
48+
49+
@Test
50+
public void testWithSSHUri() throws Exception {
51+
String result = GitDataProvider.stripCredentialsFromOriginUrl("ssh://[email protected]/");
52+
assertEquals("ssh://[email protected]/",result);
53+
}
54+
}

0 commit comments

Comments
 (0)