Skip to content

Commit

Permalink
fixing connections, tests, password masking
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Feb 24, 2018
1 parent 0100336 commit fb5d3b2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
4 changes: 0 additions & 4 deletions lib/database.js
Expand Up @@ -807,8 +807,6 @@ function Database(cn, dc, config) {
/**
* @method Database#multiResult
* @description
* ** v7.0.0 **
*
* Executes a multi-query string without any expectation for the return data, to resolve
* with an array of the original $[Result] objects when successful.
*
Expand Down Expand Up @@ -846,8 +844,6 @@ function Database(cn, dc, config) {
/**
* @method Database#multi
* @description
* ** v7.0.0 **
*
* Executes a multi-query string without any expectation for the return data, to resolve
* with an array of arrays of rows when successful.
*
Expand Down
2 changes: 0 additions & 2 deletions lib/formatting.js
Expand Up @@ -357,8 +357,6 @@ const $as = {
/**
* @namespace formatting.ctf
* @description
* ** v7.1.0 **
*
* Namespace for ES6 symbols used by $[Custom Type Formatting], available from `pgp.as.ctf` before and after initializing the library.
*
* It was added to avoid explicit/enumerable extension of types that need to be used as formatting parameters, to keep their type signature intact.
Expand Down
4 changes: 1 addition & 3 deletions lib/helpers/columnSet.js
Expand Up @@ -354,8 +354,6 @@ function ColumnSet(columns, options) {
/**
* @method helpers.ColumnSet#assignColumns
* @description
* ** v7.3.0 **
*
* Generates assignments for all columns in the set, with support for aliases and column-skipping logic.
* Aliases are set by using method {@link formatting.alias as.alias}.
*
Expand All @@ -371,7 +369,7 @@ function ColumnSet(columns, options) {
* @param {string | Array<string> | function} [options.skip]
* Name(s) of the column(s) to be skipped (case-sensitive). It can be either a single string or an array of strings.
*
* **v7.3.1:** It can also be a function - iterator, to be called for every column, passing in {@link helpers.Column Column} as
* It can also be a function - iterator, to be called for every column, passing in {@link helpers.Column Column} as
* `this` context, and plus as a single parameter. The function would return a truthy value for every column that needs to be skipped.
*
* @returns {string}
Expand Down
14 changes: 11 additions & 3 deletions lib/utils/index.js
Expand Up @@ -89,14 +89,22 @@ function addReadProp(obj, name, value, hidden) {
// if password is present, it is masked with symbol '#'.
function getSafeConnection(cn) {
if (typeof cn === 'object') {
const copy = JSON.parse(JSON.stringify(cn));
const copy = Object.assign({}, cn);
if (typeof copy.password === 'string') {
copy.password = copy.password.replace(/./g, '#');
}
if (typeof copy.connectionString === 'string') {
copy.connectionString = maskPassword(copy.connectionString);
}
return copy;
}
// or else it is a connection string;
return cn.replace(/:(?![/])([^@]+)/, (_, m) => {
return maskPassword(cn);
}

///////////////////////////////////////////////////////////
// Replaces password letters with # in a connection string.
function maskPassword(connectionString) {
return connectionString.replace(/:(?![/])([^@]+)/, (_, m) => {
return ':' + new Array(m.length + 1).join('#');
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -50,7 +50,7 @@
"JSONStream": "~1.3.2",
"bluebird": "~3.5.1",
"coveralls": "~3.0.0",
"eslint": "~4.18.0",
"eslint": "~4.18.1",
"istanbul": "~0.4.5",
"jasmine-node": "~1.14.5",
"jsdoc": "~3.5.5",
Expand Down
26 changes: 18 additions & 8 deletions test/dbSpec.js
Expand Up @@ -454,21 +454,31 @@ describe('Masked Connection Log', function () {
});
});

/* Doesn't work with pg v6.2, probably due to this issue: https://github.com/brianc/node-postgres/issues/1141
describe('as a string', function () {
var connection = 'postgres://postgres:password@localhost:123/unknown';
beforeEach(function (done) {
var errDB = pgp(connection);
describe('as a string', () => {
beforeEach(done => {
const errDB = pgp('postgres://postgres:password@localhost:123/unknown');
errDB.connect()
.catch(function () {
.catch(() => {
done();
});
});
it('must report the password masked correctly', function () {
it('must report the password masked correctly', () => {
expect(cn).toBe('postgres://postgres:########@localhost:123/unknown');
});
});
*/

describe('as a config string', () => {
beforeEach(done => {
const errDB = pgp({connectionString: 'postgres://postgres:password@localhost:123/unknown'});
errDB.connect()
.catch(() => {
done();
});
});
it('must report the password masked correctly', () => {
expect(cn).toEqual({connectionString: 'postgres://postgres:########@localhost:123/unknown'});
});
});

afterEach(function () {
delete options.error;
Expand Down

0 comments on commit fb5d3b2

Please sign in to comment.