Skip to content

Commit d5b9af2

Browse files
committed
Add URI encoding to mongo auth parameters
The mongodb driver requires auth values be URI encoded: mongodb/node-mongodb-native@0440630 This uses node's built-in url module to encode the auth portion, by parsing and re-formatting it, which causes special characters to get URI encoded properly: https://nodejs.org/api/url.html#url_escaped_characters This is all a bit silly since mongodb just takes our passed uri, and runs it through the same url parser again, but not before explicitly erroring on '@' characters in the uri. This is similiar to #148 (reverted by #297), but with much less code, and hopefully less breakage. Also, note that `uri_decode_auth` is no longer needed. That was removed in the above referenced node-mongodb-native commit. I've tested this on usernames and passwords with @, !, +, and a space. Presumably this would also work with usernames and passwords that are already URI encoded (since parseUrl will simply unescape it, and formatUrl will escape it again).
1 parent 7d78732 commit d5b9af2

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import MongoCollection from './MongoCollection';
33
import MongoSchemaCollection from './MongoSchemaCollection';
4+
import {parse as parseUrl, format as formatUrl} from 'url';
45

56
let mongodb = require('mongodb');
67
let MongoClient = mongodb.MongoClient;
@@ -25,7 +26,11 @@ export class MongoStorageAdapter {
2526
return this.connectionPromise;
2627
}
2728

28-
this.connectionPromise = MongoClient.connect(this._uri, this._options).then(database => {
29+
// parsing and re-formatting causes the auth value (if there) to get URI
30+
// encoded
31+
const encodedUri = formatUrl(parseUrl(this._uri));
32+
33+
this.connectionPromise = MongoClient.connect(encodedUri, this._options).then(database => {
2934
this.database = database;
3035
});
3136
return this.connectionPromise;

0 commit comments

Comments
 (0)