Skip to content

Commit

Permalink
JAVA-2882: Allow all non-escaped sub-delimiters ...
Browse files Browse the repository at this point in the history
  ... in connection string username and password

In order to continue using the built-in URLDecoder class, which replaces
all occurrences of the plus character with the space character, the plus
character is special-cased: before passing the user name or password to
the URLDecoder, all occurrences of the plus character are first replaced
with "%2B", the hex encoding of that character.
  • Loading branch information
jyemin committed Aug 21, 2018
1 parent 536ad44 commit 4a44f05
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion driver-core/src/main/com/mongodb/ConnectionString.java
Expand Up @@ -302,7 +302,7 @@ public ConnectionString(final String connectionString) {
char[] password = null;
idx = userAndHostInformation.lastIndexOf("@");
if (idx > 0) {
userInfo = userAndHostInformation.substring(0, idx);
userInfo = userAndHostInformation.substring(0, idx).replace("+", "%2B");
hostIdentifier = userAndHostInformation.substring(idx + 1);
int colonCount = countOccurrences(userInfo, ":");
if (userInfo.contains("@") || colonCount > 1) {
Expand Down
21 changes: 21 additions & 0 deletions driver-core/src/test/resources/connection-string/valid-auth.json
Expand Up @@ -240,6 +240,27 @@
"authmechanism": "MONGODB-CR"
}
},
{
"description": "Subdelimiters in user/pass don't need escaping (MONGODB-CR)",
"uri": "mongodb://!$&'()*,;=:!$&'()*,;=@127.0.0.1/admin?authMechanism=MONGODB-CR",
"valid": true,
"warning": false,
"hosts": [
{
"type": "ipv4",
"host": "127.0.0.1",
"port": null
}
],
"auth": {
"username": "!$&'()*,;=",
"password": "!$&'()*,;=",
"db": "admin"
},
"options": {
"authmechanism": "MONGODB-CR"
}
},
{
"description": "Escaped username (MONGODB-X509)",
"uri": "mongodb://CN%3DmyName%2COU%3DmyOrgUnit%2CO%3DmyOrg%2CL%3DmyLocality%2CST%3DmyState%2CC%3DmyCountry@localhost/?authMechanism=MONGODB-X509",
Expand Down
Expand Up @@ -34,7 +34,7 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

// See https://github.com/mongodb/specifications/tree/master/source/crud/tests
// See https://github.com/mongodb/specifications/tree/master/source/connection-string/tests
@RunWith(Parameterized.class)
public class ConnectionStringTest extends TestCase {
private final String filename;
Expand Down

0 comments on commit 4a44f05

Please sign in to comment.