Skip to content

Commit

Permalink
Merge pull request #986 from ParsePlatform/mongo-uri-encode-auth
Browse files Browse the repository at this point in the history
Add URI encoding to mongo auth parameters
  • Loading branch information
bgw committed Mar 17, 2016
2 parents 5c8ad83 + 530fad5 commit 14d3062
Show file tree
Hide file tree
Showing 4 changed files with 1,046 additions and 1 deletion.
37 changes: 37 additions & 0 deletions spec/MongoStorageAdapter.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter');
const MongoClient = require('mongodb').MongoClient;

describe('MongoStorageAdapter', () => {
it('auto-escapes symbols in auth information', () => {
spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
new MongoStorageAdapter('mongodb://user!with@+ symbols:password!with@+ symbols@localhost:1234/parse', {})
.connect();
expect(MongoClient.connect).toHaveBeenCalledWith(
'mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse',
jasmine.any(Object)
);
});

it("doesn't double escape already URI-encoded information", () => {
spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
new MongoStorageAdapter('mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse', {})
.connect();
expect(MongoClient.connect).toHaveBeenCalledWith(
'mongodb://user!with%40%2B%20symbols:password!with%40%2B%20symbols@localhost:1234/parse',
jasmine.any(Object)
);
});

// https://github.com/ParsePlatform/parse-server/pull/148#issuecomment-180407057
it('preserves replica sets', () => {
spyOn(MongoClient, 'connect').and.returnValue(Promise.resolve(null));
new MongoStorageAdapter('mongodb://test:testpass@ds056315-a0.mongolab.com:59325,ds059315-a1.mongolab.com:59315/testDBname?replicaSet=rs-ds059415', {})
.connect();
expect(MongoClient.connect).toHaveBeenCalledWith(
'mongodb://test:testpass@ds056315-a0.mongolab.com:59325,ds059315-a1.mongolab.com:59315/testDBname?replicaSet=rs-ds059415',
jasmine.any(Object)
);
});
});
7 changes: 6 additions & 1 deletion src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import MongoCollection from './MongoCollection';
import MongoSchemaCollection from './MongoSchemaCollection';
import {parse as parseUrl, format as formatUrl} from '../../../vendor/mongodbUrl';

let mongodb = require('mongodb');
let MongoClient = mongodb.MongoClient;
Expand All @@ -25,7 +26,11 @@ export class MongoStorageAdapter {
return this.connectionPromise;
}

this.connectionPromise = MongoClient.connect(this._uri, this._options).then(database => {
// parsing and re-formatting causes the auth value (if there) to get URI
// encoded
const encodedUri = formatUrl(parseUrl(this._uri));

this.connectionPromise = MongoClient.connect(encodedUri, this._options).then(database => {
this.database = database;
});
return this.connectionPromise;
Expand Down
8 changes: 8 additions & 0 deletions src/vendor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# mongoUrl

A fork of node's `url` module, with the modification that commas and colons are
allowed in hostnames. While this results in a slightly incorrect parsed result,
as the hostname field for a mongodb should be an array of replica sets, it's
good enough to let us pull out and escape the auth portion of the URL.

See also: https://github.com/ParsePlatform/parse-server/pull/986
Loading

0 comments on commit 14d3062

Please sign in to comment.