Skip to content

Commit f9793d3

Browse files
authored
Ensure socks5 proxy option parsing is to specification (#1688)
Duplicates are not permitted, where as elsewhere in the connection string they are. JAVA-5834
1 parent 6fc9eac commit f9793d3

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

driver-core/src/main/com/mongodb/ConnectionString.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import static com.mongodb.internal.connection.OidcAuthenticator.OidcValidator.validateCreateOidcCredential;
5757
import static java.lang.String.format;
5858
import static java.util.Arrays.asList;
59+
import static java.util.Collections.emptyList;
5960
import static java.util.Collections.singletonList;
6061
import static java.util.Collections.unmodifiableList;
6162

@@ -505,7 +506,7 @@ public ConnectionString(final String connectionString, @Nullable final DnsClient
505506
throw new IllegalArgumentException("srvMaxHosts can not be specified with replica set name");
506507
}
507508

508-
validateProxyParameters();
509+
validateProxyParameters(combinedOptionsMaps);
509510

510511
credential = createCredentials(combinedOptionsMaps, userName, password);
511512
warnOnUnsupportedOptions(combinedOptionsMaps);
@@ -1226,7 +1227,7 @@ private void validatePort(final String port) {
12261227
}
12271228
}
12281229

1229-
private void validateProxyParameters() {
1230+
private void validateProxyParameters(final Map<String, List<String>> optionsMap) {
12301231
if (proxyHost == null) {
12311232
if (proxyPort != null) {
12321233
throw new IllegalArgumentException("proxyPort can only be specified with proxyHost");
@@ -1259,6 +1260,23 @@ private void validateProxyParameters() {
12591260
throw new IllegalArgumentException(
12601261
"Both proxyUsername and proxyPassword must be set together. They cannot be set individually");
12611262
}
1263+
1264+
if (containsDuplicatedOptions("proxyhost", optionsMap)) {
1265+
throw new IllegalArgumentException("Duplicated values for proxyHost: " + optionsMap.get("proxyhost"));
1266+
}
1267+
if (containsDuplicatedOptions("proxyport", optionsMap)) {
1268+
throw new IllegalArgumentException("Duplicated values for proxyPort: " + optionsMap.get("proxyport"));
1269+
}
1270+
if (containsDuplicatedOptions("proxypassword", optionsMap)) {
1271+
throw new IllegalArgumentException("Duplicated values for proxyPassword: " + optionsMap.get("proxypassword"));
1272+
}
1273+
if (containsDuplicatedOptions("proxyusername", optionsMap)) {
1274+
throw new IllegalArgumentException("Duplicated values for proxyUsername: " + optionsMap.get("proxyusername"));
1275+
}
1276+
}
1277+
1278+
private static boolean containsDuplicatedOptions(final String optionName, final Map<String, List<String>> optionsMap) {
1279+
return optionsMap.getOrDefault(optionName, emptyList()).size() > 1;
12621280
}
12631281

12641282
private int countOccurrences(final String haystack, final String needle) {

driver-core/src/test/unit/com/mongodb/UriOptionsTest.java

-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ public void shouldPassAllOutcomes() {
4747
// No CANONICALIZE_HOST_NAME support https://jira.mongodb.org/browse/JAVA-4278
4848
assumeFalse(getDescription().equals("Valid auth options are parsed correctly (GSSAPI)"));
4949

50-
// https://jira.mongodb.org/browse/JAVA-5834
51-
assumeFalse(getFilename().equals("proxy-options.json"));
52-
5350
if (getDefinition().getBoolean("valid", BsonBoolean.TRUE).getValue()) {
5451
testValidOptions();
5552
} else {

0 commit comments

Comments
 (0)