Skip to content

Commit

Permalink
fix: replace auth with string replace (eggjs#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored May 6, 2019
1 parent 3cf9e73 commit 5b9f8ba
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 26 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: node_js
node_js:
- '8'
- '10'
- '12'
install:
- npm i npminstall && npminstall
script:
Expand Down
21 changes: 6 additions & 15 deletions lib/filterURLPassword.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
'use strict';

const semver = require('semver');

/**
* filter the auth of url string
* @param {URL} input url
* @param {String} version version string from `process.version`
* @return {String} filtered url
* @see https://docs.mongodb.com/manual/reference/connection-string/
*/
module.exports = function filterURLPassword(input, version) {
if (semver.lt(version, '6.13.0')) {
const urlTool = require('url');
const url = urlTool.parse(input);
url.password = '*****';
url.auth = url.auth && (url.auth.split(':')[0] + ':*****');
return urlTool.format(url);
}
const { URL } = require('url');
const url = new URL(input);
url.password = '*****';
return url.toString();
module.exports = function filterURLPassword(input) {
const index = input.indexOf('@');
if (index === -1) return input;
const startIndex = input.lastIndexOf(':', index);
return input.substring(0, startIndex + 1) + '******' + input.substring(index);
};
2 changes: 1 addition & 1 deletion lib/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = app => {

function createOneClient(config, app) {
const { url, options } = config;
const filteredURL = filterURLPassword(url, process.version);
const filteredURL = filterURLPassword(url);

assert(url, '[egg-mongoose] url is required on config');

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"dependencies": {
"@types/mongoose": "^5.3.24",
"await-first": "^1.0.0",
"mongoose": "^5.4.20",
"semver": "^6.0.0"
"mongoose": "^5.4.20"
},
"devDependencies": {
"autod": "^3.0.1",
Expand Down Expand Up @@ -52,7 +51,7 @@
"lib"
],
"ci": {
"version": "8, 10"
"version": "8, 10, 12"
},
"repository": {
"type": "git",
Expand Down
10 changes: 3 additions & 7 deletions test/mongoose.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,9 @@ describe('test/mongoose.test.js', () => {

it('should filter password of url', () => {
const filterURLPassword = require('../lib/filterURLPassword');
const url = 'https://abc:[email protected]/';
const outputV10 = filterURLPassword(url, 'v10.0.0');
assert.equal(outputV10, 'https://abc:*****@example.com/');
const outputV8 = filterURLPassword(url, 'v8.0.0');
assert.equal(outputV8, 'https://abc:*****@example.com/');
const outputV6 = filterURLPassword(url, 'v6.0.0');
assert.equal(outputV6, 'https://abc:*****@example.com/');
assert(filterURLPassword('https://example.com/') === 'https://example.com/');
assert(filterURLPassword('https://abc:[email protected]/') === 'https://abc:******@example.com/');
assert(filterURLPassword('mongodb://abc:[email protected]:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/admin?replicaSet=myRepl') === 'mongodb://abc:******@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/admin?replicaSet=myRepl');
});
});

Expand Down

0 comments on commit 5b9f8ba

Please sign in to comment.