Description
Description:
Hello
I am writing to request a feature enhancement related to the handling of passwords in MongoDB connection strings. Currently, when a password contains special characters (in my case, %), it can cause issues when establishing a connection to the database. This is because these characters are not URL-encoded, leading to potential misinterpretation of the connection string.
Use Case:
For example, if a password is p%ssword, the connection string would look like this:
spring.data.mongodb.uri: mongodb+srv://myuser:myp%[email protected]
throws the following exception:
Caused by: java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - Error at index 1 in: "as"
at java.base/java.net.URLDecoder.decode(URLDecoder.java:237) ~[na:na]
at java.base/java.net.URLDecoder.decode(URLDecoder.java:147) ~[na:na]
at com.mongodb.ConnectionString.urldecode(ConnectionString.java:1289) ~[mongodb-driver-core-5.2.1.jar:na]
at com.mongodb.ConnectionString.<init>(ConnectionString.java:411) ~[mongodb-driver-core-5.2.1.jar:na]
at com.mongodb.ConnectionString.<init>(ConnectionString.java:343) ~[mongodb-driver-core-5.2.1.jar:na]
at org.springframework.boot.autoconfigure.mongo.PropertiesMongoConnectionDetails.getConnectionString(PropertiesMongoConnectionDetails.java:49) ~[spring-boot-autoconfigure-3.4.3.jar:3.4.3]
at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration$MongoClientSettingsConfiguration.standardMongoSettingsCustomizer(MongoAutoConfiguration.java:74) ~[spring-boot-autoconfigure-3.4.3.jar:3.4.3]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:569) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.lambda$instantiate$0(SimpleInstantiationStrategy.java:171) ~[spring-beans-6.2.3.jar:6.2.3]
To make it work, we would need to manually URL-encode the password. However, our challenge is that we do not have direct access to the password, as it is injected via an environment variable and stored in a secure vault. This means we cannot pre-encode it before it is passed to the application.
Would it be possible to add automatic encoding to the spring.data.mongodb.uri
property to handle this scenario more seamlessly?
Thank you very much