From 5425bc15d2c23caadaa2dcf30b636cde68bab8aa Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Wed, 15 Jul 2020 13:19:45 -0500 Subject: [PATCH] Fix untested pgpass code --- packages/pg/lib/client.js | 57 +++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 600cf89f..842de57f 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -199,36 +199,35 @@ class Client extends EventEmitter { // TODO(bmc): deprecate pgpass "built in" integration since this.password can be a function // it can be supplied by the user if required - this is a breaking change! _checkPgPass(cb) { - return function (msg) { - if (typeof this.password === 'function') { - this._Promise - .resolve() - .then(() => this.password()) - .then((pass) => { - if (pass !== undefined) { - if (typeof pass !== 'string') { - con.emit('error', new TypeError('Password must be a string')) - return - } - this.connectionParameters.password = this.password = pass - } else { - this.connectionParameters.password = this.password = null + const con = this.connection + if (typeof this.password === 'function') { + this._Promise + .resolve() + .then(() => this.password()) + .then((pass) => { + if (pass !== undefined) { + if (typeof pass !== 'string') { + con.emit('error', new TypeError('Password must be a string')) + return } - cb(msg) - }) - .catch((err) => { - con.emit('error', err) - }) - } else if (this.password !== null) { - cb(msg) - } else { - pgPass(this.connectionParameters, function (pass) { - if (undefined !== pass) { this.connectionParameters.password = this.password = pass + } else { + this.connectionParameters.password = this.password = null } - cb(msg) + cb() }) - } + .catch((err) => { + con.emit('error', err) + }) + } else if (this.password !== null) { + cb() + } else { + pgPass(this.connectionParameters, function (pass) { + if (undefined !== pass) { + this.connectionParameters.password = this.password = pass + } + cb() + }) } } @@ -239,14 +238,14 @@ class Client extends EventEmitter { } _handleAuthMD5Password(msg) { - this._checkPgPass((msg) => { + this._checkPgPass(() => { const hashedPassword = utils.postgresMd5PasswordHash(this.user, this.password, msg.salt) this.connection.password(hashedPassword) }) } - _handleAuthSASL(msg) { - this._checkPgPass((msg) => { + _handleAuthSASL() { + this._checkPgPass(() => { this.saslSession = sasl.startSession(msg.mechanisms) const con = this.connection con.sendSASLInitialResponseMessage(saslSession.mechanism, saslSession.response)