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 720c322 commit 55a3498
Showing 1 changed file with 8 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 @@ -1157,7 +1157,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 @@ -1166,7 +1166,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 @@ -1175,19 +1175,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

0 comments on commit 55a3498

Please sign in to comment.