Skip to content
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

Connection String #1467

Merged
merged 3 commits into from
Aug 7, 2024
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
18 changes: 8 additions & 10 deletions driver-core/src/main/com/mongodb/ConnectionString.java
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ private List<String> parseHosts(final List<String> rawHosts) {
}
int idx = host.indexOf("]:");
if (idx != -1) {
validatePort(host, host.substring(idx + 2));
validatePort(host.substring(idx + 2));
}
} else {
int colonCount = countOccurrences(host, ":");
Expand All @@ -1218,7 +1218,7 @@ private List<String> parseHosts(final List<String> rawHosts) {
+ "Reserved characters such as ':' must be escaped according RFC 2396. "
+ "Any IPv6 address literal must be enclosed in '[' and ']' according to RFC 2732.", host));
} else if (colonCount == 1) {
validatePort(host, host.substring(host.indexOf(":") + 1));
validatePort(host.substring(host.indexOf(":") + 1));
}
}
hosts.add(host);
Expand All @@ -1227,19 +1227,17 @@ private List<String> parseHosts(final List<String> rawHosts) {
return hosts;
}

private void validatePort(final String host, final String port) {
boolean invalidPort = false;
private void validatePort(final String port) {
try {
int portInt = Integer.parseInt(port);
if (portInt <= 0 || portInt > 65535) {
invalidPort = true;
throw new IllegalArgumentException("The connection string contains an invalid host and port. "
+ "The port must be an integer between 0 and 65535.");
}
} catch (NumberFormatException e) {
invalidPort = true;
}
if (invalidPort) {
throw new IllegalArgumentException(format("The connection string contains an invalid host '%s'. "
+ "The port '%s' is not a valid, it must be an integer between 0 and 65535", host, port));
throw new IllegalArgumentException("The connection string contains an invalid host and port. "
+ "The port contains non-digit characters, it must be an integer between 0 and 65535. "
+ "Hint: username and password must be escaped according to RFC 3986.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.mongodb.connection.ServerMonitoringMode;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.UnsupportedEncodingException;
Expand All @@ -27,6 +28,7 @@

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -92,4 +94,20 @@ void serverMonitoringMode() {
() -> new ConnectionString(DEFAULT_OPTIONS + "serverMonitoringMode=invalid"))
);
}


@ParameterizedTest
@ValueSource(strings = {"mongodb://foo:bar/@hostname/java?", "mongodb://foo:bar?@hostname/java/",
"mongodb+srv://foo:bar/@hostname/java?", "mongodb+srv://foo:bar?@hostname/java/",
"mongodb://foo:bar/@[::1]:27018", "mongodb://foo:bar?@[::1]:27018",
"mongodb://foo:12345678/@hostname", "mongodb+srv://foo:12345678/@hostname",
"mongodb://foo:12345678/@hostname", "mongodb+srv://foo:12345678/@hostname",
"mongodb://foo:12345678%40hostname", "mongodb+srv://foo:12345678%40hostname",
"mongodb://foo:12345678@bar@hostname", "mongodb+srv://foo:12345678@bar@hostname"
})
void unescapedPasswordsShouldNotBeLeakedInExceptionMessages(final String input) {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new ConnectionString(input));
assertFalse(exception.getMessage().contains("bar"));
assertFalse(exception.getMessage().contains("12345678"));
}
}