App silently connects to local test database when the remote url contains un-encoded characters
- Dominant language
- Groovy
- Stars
- 2.9k
- Forks
- 975
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 92
Description
# Thanks to @AbhishekTaparia for finding out the issue 👏🏻
## Environment
- Grails 4.0.4
- GORM 7.0.6
- GORM MongoDB 7.0.1
## Problem
We were recently trying to connect to a remote MongoDB instance (hosted on MongoDB Atlas) and we configured the app with the following config in `application.groovy` (shows fake credentials)-
```groovy
String mongoDBUsername = "root"
String mongoDBPassword = "67ERH@45hu12"
String mongoDBName = "myFirstDatabase"
String mongoDBHost = "example-development.garhn.mongodb.net"
grails {
mongodb {
url = "mongodb+srv://${mongoDBUsername}:${mongoDBPassword}@${mongoDBHost}/${mongoDBName}?retryWrites" +
"=true&w=majority"
}
}
```
When we ran the app using `./gradlew bootRun` the app started properly and a few domain instances created properly (which we defined in `Bootstrap.groovy`). But when we got to see in the MongoDB Atlas (both using their console and `mongo shell`, we were not finding the collection and any document created.
After 4-5 hours of debugging, we got to know that somehow the Grails is skipping the configuration and connecting to the local instance of MongoDB with `test` database.
Further investigation leads us to the solution that since the password was having `@`, it needs to be URL encoded-
```groovy
String mongoDBPassword = URLEncoder.encode("67ERH@45hu12", "UTF-8")
```
So when we changed our line as above, the app connected properly to the remote MongoDB host.
## Actual Behaviour
The app silently ignores the error and connects to the `test` database in local MongoDB instance.
## Expected Behaviour
The app startup should fail and correct errors should be shown.
Demo application- https://github.com/AbhishekTaparia/test-grails-mongo-issue
## Additional Info 1
After upgrading Grails to `4.0.9` to and GORM to `7.0.8`, the silent connection to the database does not work instead the app startup freezes and the app never bootstraps.
Contributor guide
Research direction
Start with the demo application and its application.groovy MongoDB URL, then reproduce the behavior with ./gradlew bootRun; Bootstrap.groovy shows when database access occurs. Trace how the malformed remote URL is handled and verify that startup reports an error rather than connecting to the local test database or freezing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- groovy, mongodb
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100