Skip to content

Commit

Permalink
Connection String (#1467)
Browse files Browse the repository at this point in the history
Don't output the host and port information if the port is invalid.
Reduces risk of leaking password information if the password has not
been correctly urlencoded.

JAVA-5560
  • Loading branch information
rozza committed Aug 7, 2024
1 parent 7683aab commit 54af270
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
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 @@ -1176,7 +1176,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 @@ -1185,7 +1185,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 @@ -1194,19 +1194,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"));
}
}

0 comments on commit 54af270

Please sign in to comment.