Skip to content

Commit 85f5ba6

Browse files
Strip out credentials from git URIs if they are present (#1083)
* Strip out credentials from git URIs if they are present * Provide example URL in a comment
1 parent 256fc4b commit 85f5ba6

File tree

11 files changed

+102
-36
lines changed

11 files changed

+102
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.synopsys.integration.detectable.detectables.git;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
7+
import org.apache.commons.lang3.StringUtils;
8+
9+
public class GitUrlParser {
10+
// Parses urls such as: https://github.com/blackducksoftware/synopsys-detect
11+
public String getRepoName(String remoteUrlString) throws MalformedURLException {
12+
String[] pieces = remoteUrlString.split("[/:]");
13+
if (pieces.length >= 2) {
14+
String organization = pieces[pieces.length - 2];
15+
String repo = pieces[pieces.length - 1];
16+
String name = String.format("%s/%s", organization, repo);
17+
return StringUtils.removeEnd(StringUtils.removeStart(name, "/"), ".git");
18+
} else {
19+
throw new MalformedURLException("Failed to extract repository name from url. Not logging url for security.");
20+
}
21+
}
22+
23+
/**
24+
* Strip out credentials if the string is a URI and contains credentials.
25+
*
26+
* For example, a URL such as https://user:[email protected]/some/repo will become https://synopsys.com/some/repo
27+
*
28+
* @param remoteUrlString
29+
* @return sanitized URI or original string
30+
*/
31+
public String removeCredentialsFromUri(String remoteUrlString) {
32+
if (remoteUrlString != null) {
33+
try {
34+
URI uri = new URI(remoteUrlString);
35+
String userInfo = uri.getUserInfo();
36+
if (userInfo != null) {
37+
remoteUrlString = remoteUrlString.replace(userInfo + "@", "");
38+
}
39+
} catch (URISyntaxException e) {
40+
// this is not a valid URI, so we will not attempt to remove credentials
41+
}
42+
}
43+
return remoteUrlString;
44+
}
45+
}

detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/cli/GitCliExtractor.java

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import com.synopsys.integration.blackduck.bdio2.model.GitInfo;
1010
import com.synopsys.integration.detectable.ExecutableTarget;
11+
import com.synopsys.integration.detectable.detectables.git.GitUrlParser;
1112
import com.synopsys.integration.detectable.extraction.Extraction;
1213
import com.synopsys.integration.detectable.extraction.ExtractionMetadata;
1314
import com.synopsys.integration.detectable.util.ToolVersionLogger;
@@ -36,6 +37,7 @@ public Extraction extract(ExecutableTarget gitExecutable, File directory) {
3637
toolVersionLogger.log(directory, gitExecutable);
3738

3839
String remoteUrl = gitCommandRunner.getRepoUrl(gitExecutable, directory);
40+
remoteUrl = gitUrlParser.removeCredentialsFromUri(remoteUrl);
3941
String repoName = gitUrlParser.getRepoName(remoteUrl);
4042

4143
Optional<String> branch = Optional.ofNullable(gitCommandRunner.getRepoBranch(gitExecutable, directory));

detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/cli/GitUrlParser.java

-20
This file was deleted.

detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/GitParseExtractor.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.slf4j.LoggerFactory;
1515

1616
import com.synopsys.integration.blackduck.bdio2.model.GitInfo;
17+
import com.synopsys.integration.detectable.detectables.git.GitUrlParser;
1718
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfig;
1819
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigResult;
1920
import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigNameVersionTransformer;
@@ -30,11 +31,13 @@ public class GitParseExtractor {
3031
private final GitFileParser gitFileParser;
3132
private final GitConfigNameVersionTransformer gitConfigExtractor;
3233
private final GitConfigNodeTransformer gitConfigNodeTransformer;
34+
private final GitUrlParser gitUrlParser;
3335

34-
public GitParseExtractor(GitFileParser gitFileParser, GitConfigNameVersionTransformer gitConfigExtractor, GitConfigNodeTransformer gitConfigNodeTransformer) {
36+
public GitParseExtractor(GitFileParser gitFileParser, GitConfigNameVersionTransformer gitConfigExtractor, GitConfigNodeTransformer gitConfigNodeTransformer, GitUrlParser gitUrlParser) {
3537
this.gitFileParser = gitFileParser;
3638
this.gitConfigExtractor = gitConfigExtractor;
3739
this.gitConfigNodeTransformer = gitConfigNodeTransformer;
40+
this.gitUrlParser = gitUrlParser;
3841
}
3942

4043
public final Extraction extract(@Nullable File gitConfigFile, @Nullable File gitHeadFile, @Nullable File gitOriginHeadFile) {
@@ -58,7 +61,7 @@ public final Extraction extract(@Nullable File gitConfigFile, @Nullable File git
5861
String headCommitHash = StringUtils.trimToNull(readFileToStringSafetly(gitOriginHeadFile));
5962

6063
GitInfo gitInfo = new GitInfo(
61-
gitConfigResult.getRemoteUrl(),
64+
gitUrlParser.removeCredentialsFromUri(gitConfigResult.getRemoteUrl()),
6265
headCommitHash,
6366
gitConfigResult.getBranch().orElse(null)
6467
);

detectable/src/main/java/com/synopsys/integration/detectable/detectables/git/parsing/parse/GitConfigNameVersionTransformer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
1212

13-
import com.synopsys.integration.detectable.detectables.git.cli.GitUrlParser;
13+
import com.synopsys.integration.detectable.detectables.git.GitUrlParser;
1414
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfig;
1515
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigBranch;
1616
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigRemote;

detectable/src/main/java/com/synopsys/integration/detectable/detectables/swift/cli/SwiftPackageTransformer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import com.synopsys.integration.bdio.model.dependency.Dependency;
1010
import com.synopsys.integration.bdio.model.externalid.ExternalId;
1111
import com.synopsys.integration.detectable.detectable.codelocation.CodeLocation;
12-
import com.synopsys.integration.detectable.detectables.git.cli.GitUrlParser;
12+
import com.synopsys.integration.detectable.detectables.git.GitUrlParser;
1313
import com.synopsys.integration.detectable.detectables.swift.cli.model.SwiftPackage;
1414

1515
public class SwiftPackageTransformer {

detectable/src/main/java/com/synopsys/integration/detectable/detectables/swift/lock/transform/PackageResolvedTransformer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import com.synopsys.integration.bdio.graph.DependencyGraph;
1414
import com.synopsys.integration.bdio.model.Forge;
1515
import com.synopsys.integration.bdio.model.dependency.Dependency;
16-
import com.synopsys.integration.detectable.detectables.git.cli.GitUrlParser;
16+
import com.synopsys.integration.detectable.detectables.git.GitUrlParser;
1717
import com.synopsys.integration.detectable.detectables.swift.lock.data.PackageState;
1818
import com.synopsys.integration.detectable.detectables.swift.lock.data.ResolvedPackage;
1919

detectable/src/main/java/com/synopsys/integration/detectable/factory/DetectableFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@
131131
import com.synopsys.integration.detectable.detectables.docker.parser.DockerInspectorResultsFileParser;
132132
import com.synopsys.integration.detectable.detectables.git.GitCliDetectable;
133133
import com.synopsys.integration.detectable.detectables.git.GitParseDetectable;
134+
import com.synopsys.integration.detectable.detectables.git.GitUrlParser;
134135
import com.synopsys.integration.detectable.detectables.git.cli.GitCliExtractor;
135136
import com.synopsys.integration.detectable.detectables.git.cli.GitCommandRunner;
136-
import com.synopsys.integration.detectable.detectables.git.cli.GitUrlParser;
137137
import com.synopsys.integration.detectable.detectables.git.parsing.GitParseExtractor;
138138
import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigNameVersionTransformer;
139139
import com.synopsys.integration.detectable.detectables.git.parsing.parse.GitConfigNodeTransformer;
@@ -808,7 +808,7 @@ private GitConfigNodeTransformer gitConfigNodeTransformer() {
808808
}
809809

810810
private GitParseExtractor gitParseExtractor() {
811-
return new GitParseExtractor(gitFileParser(), gitConfigNameVersionTransformer(), gitConfigNodeTransformer());
811+
return new GitParseExtractor(gitFileParser(), gitConfigNameVersionTransformer(), gitConfigNodeTransformer(), gitUrlParser());
812812
}
813813

814814
private GitUrlParser gitUrlParser() {

detectable/src/test/java/com/synopsys/integration/detectable/detectables/git/unit/GitConfigNameVersionTransformerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import org.junit.jupiter.api.Test;
1313

14-
import com.synopsys.integration.detectable.detectables.git.cli.GitUrlParser;
14+
import com.synopsys.integration.detectable.detectables.git.GitUrlParser;
1515
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfig;
1616
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigBranch;
1717
import com.synopsys.integration.detectable.detectables.git.parsing.model.GitConfigRemote;

detectable/src/test/java/com/synopsys/integration/detectable/detectables/git/unit/GitUrlParserTest.java

+43-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import org.junit.jupiter.api.Assertions;
66
import org.junit.jupiter.api.Test;
77

8-
import com.synopsys.integration.detectable.detectables.git.cli.GitUrlParser;
8+
import com.synopsys.integration.detectable.detectables.git.GitUrlParser;
99

1010
class GitUrlParserTest {
1111
@Test
12-
void sshUrl() throws MalformedURLException {
12+
void testGetRepoName_sshUrl() throws MalformedURLException {
1313
GitUrlParser gitUrlParser = new GitUrlParser();
1414
final String remoteUrl = "ssh://[email protected]:12345/blackducksoftware/synopsys-detect";
1515
String repoName = gitUrlParser.getRepoName(remoteUrl);
@@ -18,7 +18,7 @@ void sshUrl() throws MalformedURLException {
1818
}
1919

2020
@Test
21-
void gitUrl() throws MalformedURLException {
21+
void testGetRepoName_gitUrl() throws MalformedURLException {
2222
GitUrlParser gitUrlParser = new GitUrlParser();
2323
final String remoteUrl = "git://git.yoctoproject.org/poky.git";
2424
String repoName = gitUrlParser.getRepoName(remoteUrl);
@@ -27,7 +27,7 @@ void gitUrl() throws MalformedURLException {
2727
}
2828

2929
@Test
30-
void gitAtUrl() throws MalformedURLException {
30+
void testGetRepoName_gitAtUrl() throws MalformedURLException {
3131
GitUrlParser gitUrlParser = new GitUrlParser();
3232
final String remoteUrl = "[email protected]:blackducksoftware/synopsys-detect.git";
3333
String repoName = gitUrlParser.getRepoName(remoteUrl);
@@ -36,7 +36,7 @@ void gitAtUrl() throws MalformedURLException {
3636
}
3737

3838
@Test
39-
void httpsUrl() throws MalformedURLException {
39+
void testGetRepoName_httpsUrl() throws MalformedURLException {
4040
GitUrlParser gitUrlParser = new GitUrlParser();
4141
final String remoteUrl = "https://github.com/blackducksoftware/synopsys-detect";
4242
String repoName = gitUrlParser.getRepoName(remoteUrl);
@@ -45,11 +45,47 @@ void httpsUrl() throws MalformedURLException {
4545
}
4646

4747
@Test
48-
void httpsEncodedUsernamePasswordUrl() throws MalformedURLException {
48+
void testGetRepoName_httpsEncodedUsernamePasswordUrl() throws MalformedURLException {
4949
GitUrlParser gitUrlParser = new GitUrlParser();
5050
final String remoteUrl = "https://USERNAME:PASSWORD@SERVER/test/path/to/blackducksoftware/synopsys-detect.git";
5151
String repoName = gitUrlParser.getRepoName(remoteUrl);
5252

5353
Assertions.assertEquals("blackducksoftware/synopsys-detect", repoName);
5454
}
55-
}
55+
56+
@Test
57+
void testRemoveCredentialsFromUrl_nonUri() {
58+
GitUrlParser gitUrlParser = new GitUrlParser();
59+
final String remoteUrl = "[email protected]:blackducksoftware/synopsys-detect.git";
60+
String sanitized = gitUrlParser.removeCredentialsFromUri(remoteUrl);
61+
62+
Assertions.assertEquals(remoteUrl, sanitized);
63+
}
64+
65+
@Test
66+
void testRemoveCredentialsFromUrl_sshUri() {
67+
GitUrlParser gitUrlParser = new GitUrlParser();
68+
final String remoteUrl = "ssh://[email protected]:12345/blackducksoftware/synopsys-detect";
69+
String sanitized = gitUrlParser.removeCredentialsFromUri(remoteUrl);
70+
71+
Assertions.assertEquals("ssh://synopsys.com:12345/blackducksoftware/synopsys-detect", sanitized);
72+
}
73+
74+
@Test
75+
void testRemoveCredentialsFromUrl_httpsWithCredentials() {
76+
GitUrlParser gitUrlParser = new GitUrlParser();
77+
final String remoteUrl = "https://user:[email protected]/blackducksoftware/synopsys-detect.git";
78+
String sanitized = gitUrlParser.removeCredentialsFromUri(remoteUrl);
79+
80+
Assertions.assertEquals("https://github.com/blackducksoftware/synopsys-detect.git", sanitized);
81+
}
82+
83+
@Test
84+
void testRemoveCredentialsFromUrl_httpsWithoutCredentials() {
85+
GitUrlParser gitUrlParser = new GitUrlParser();
86+
final String remoteUrl = "https://github.com/blackducksoftware/synopsys-detect.git";
87+
String sanitized = gitUrlParser.removeCredentialsFromUri(remoteUrl);
88+
89+
Assertions.assertEquals(remoteUrl, sanitized);
90+
}
91+
}

detectable/src/test/java/com/synopsys/integration/detectable/detectables/swift/lock/transform/PackageResolvedTransformerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import com.synopsys.integration.bdio.graph.DependencyGraph;
1515
import com.synopsys.integration.bdio.model.Forge;
1616
import com.synopsys.integration.bdio.model.externalid.ExternalId;
17-
import com.synopsys.integration.detectable.detectables.git.cli.GitUrlParser;
17+
import com.synopsys.integration.detectable.detectables.git.GitUrlParser;
1818
import com.synopsys.integration.detectable.detectables.swift.lock.data.PackageResolvedFormat;
1919
import com.synopsys.integration.detectable.detectables.swift.lock.data.PackageState;
2020
import com.synopsys.integration.detectable.detectables.swift.lock.data.ResolvedPackage;

0 commit comments

Comments
 (0)