Skip to content

Ensure socks5 proxy option parsing is to specification #1688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions driver-core/src/main/com/mongodb/ConnectionString.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import static com.mongodb.internal.connection.OidcAuthenticator.OidcValidator.validateCreateOidcCredential;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableList;

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

validateProxyParameters();
validateProxyParameters(combinedOptionsMaps);

credential = createCredentials(combinedOptionsMaps, userName, password);
warnOnUnsupportedOptions(combinedOptionsMaps);
Expand Down Expand Up @@ -1226,7 +1227,7 @@ private void validatePort(final String port) {
}
}

private void validateProxyParameters() {
private void validateProxyParameters(final Map<String, List<String>> optionsMap) {
if (proxyHost == null) {
if (proxyPort != null) {
throw new IllegalArgumentException("proxyPort can only be specified with proxyHost");
Expand Down Expand Up @@ -1259,6 +1260,23 @@ private void validateProxyParameters() {
throw new IllegalArgumentException(
"Both proxyUsername and proxyPassword must be set together. They cannot be set individually");
}

if (containsDuplicatedOptions("proxyhost", optionsMap)) {
throw new IllegalArgumentException("Duplicated values for proxyHost: " + optionsMap.get("proxyhost"));
}
if (containsDuplicatedOptions("proxyport", optionsMap)) {
throw new IllegalArgumentException("Duplicated values for proxyPort: " + optionsMap.get("proxyport"));
}
if (containsDuplicatedOptions("proxypassword", optionsMap)) {
throw new IllegalArgumentException("Duplicated values for proxyPassword: " + optionsMap.get("proxypassword"));
}
if (containsDuplicatedOptions("proxyusername", optionsMap)) {
throw new IllegalArgumentException("Duplicated values for proxyUsername: " + optionsMap.get("proxyusername"));
}
}

private static boolean containsDuplicatedOptions(final String optionName, final Map<String, List<String>> optionsMap) {
return optionsMap.getOrDefault(optionName, emptyList()).size() > 1;
}

private int countOccurrences(final String haystack, final String needle) {
Expand Down
3 changes: 0 additions & 3 deletions driver-core/src/test/unit/com/mongodb/UriOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ public void shouldPassAllOutcomes() {
// No CANONICALIZE_HOST_NAME support https://jira.mongodb.org/browse/JAVA-4278
assumeFalse(getDescription().equals("Valid auth options are parsed correctly (GSSAPI)"));

// https://jira.mongodb.org/browse/JAVA-5834
assumeFalse(getFilename().equals("proxy-options.json"));

if (getDefinition().getBoolean("valid", BsonBoolean.TRUE).getValue()) {
testValidOptions();
} else {
Expand Down