Skip to content

Commit 55a3498

Browse files
committed
Connection String (mongodb#1467)
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
1 parent 720c322 commit 55a3498

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ private List<String> parseHosts(final List<String> rawHosts) {
11571157
}
11581158
int idx = host.indexOf("]:");
11591159
if (idx != -1) {
1160-
validatePort(host, host.substring(idx + 2));
1160+
validatePort(host.substring(idx + 2));
11611161
}
11621162
} else {
11631163
int colonCount = countOccurrences(host, ":");
@@ -1166,7 +1166,7 @@ private List<String> parseHosts(final List<String> rawHosts) {
11661166
+ "Reserved characters such as ':' must be escaped according RFC 2396. "
11671167
+ "Any IPv6 address literal must be enclosed in '[' and ']' according to RFC 2732.", host));
11681168
} else if (colonCount == 1) {
1169-
validatePort(host, host.substring(host.indexOf(":") + 1));
1169+
validatePort(host.substring(host.indexOf(":") + 1));
11701170
}
11711171
}
11721172
hosts.add(host);
@@ -1175,19 +1175,17 @@ private List<String> parseHosts(final List<String> rawHosts) {
11751175
return hosts;
11761176
}
11771177

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

0 commit comments

Comments
 (0)