Skip to content

Commit 14d3062

Browse files
committed
Merge pull request #986 from ParsePlatform/mongo-uri-encode-auth
Add URI encoding to mongo auth parameters
2 parents 5c8ad83 + 530fad5 commit 14d3062

File tree

4 files changed

+1046
-1
lines changed

4 files changed

+1046
-1
lines changed

spec/MongoStorageAdapter.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter');
4+
const MongoClient = require('mongodb').MongoClient;
5+
6+
describe('MongoStorageAdapter', () => {
7+
it('auto-escapes symbols in auth information', () => {
8+
spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
9+
new MongoStorageAdapter('mongodb://user!with@+ symbols:password!with@+ symbols@localhost:1234/parse', {})
10+
.connect();
11+
expect(MongoClient.connect).toHaveBeenCalledWith(
12+
'mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse',
13+
jasmine.any(Object)
14+
);
15+
});
16+
17+
it("doesn't double escape already URI-encoded information", () => {
18+
spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
19+
new MongoStorageAdapter('mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse', {})
20+
.connect();
21+
expect(MongoClient.connect).toHaveBeenCalledWith(
22+
'mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse',
23+
jasmine.any(Object)
24+
);
25+
});
26+
27+
// https://github.com/ParsePlatform/parse-server/pull/148#issuecomment-180407057
28+
it('preserves replica sets', () => {
29+
spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
30+
new MongoStorageAdapter('mongodb://test:[email protected]:59325,ds059315-a1.mongolab.com:59315/testDBname?replicaSet=rs-ds059415', {})
31+
.connect();
32+
expect(MongoClient.connect).toHaveBeenCalledWith(
33+
'mongodb://test:[email protected]:59325,ds059315-a1.mongolab.com:59315/testDBname?replicaSet=rs-ds059415',
34+
jasmine.any(Object)
35+
);
36+
});
37+
});

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 6 additions & 1 deletion
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 '../../../vendor/mongodbUrl';
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;

src/vendor/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# mongoUrl
2+
3+
A fork of node's `url` module, with the modification that commas and colons are
4+
allowed in hostnames. While this results in a slightly incorrect parsed result,
5+
as the hostname field for a mongodb should be an array of replica sets, it's
6+
good enough to let us pull out and escape the auth portion of the URL.
7+
8+
See also: https://github.com/ParsePlatform/parse-server/pull/986

0 commit comments

Comments
 (0)