Skip to content
This repository was archived by the owner on Nov 13, 2019. It is now read-only.

Commit e74c13d

Browse files
sehropebrianc
authored andcommitted
Centralize password md5 hashing logic
Centralize logic for md5 hashing of passwords for authentication. Adds a new function postgresMd5PasswordHash(user, password, salt) to utils and updates client.js and tests to use it.
1 parent 3ad0680 commit e74c13d

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

lib/client.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,7 @@ Client.prototype.connect = function (callback) {
106106

107107
// password request handling
108108
con.on('authenticationMD5Password', checkPgPass(function (msg) {
109-
var inner = utils.md5(self.password + self.user)
110-
var outer = utils.md5(Buffer.concat([Buffer.from(inner), msg.salt]))
111-
var md5password = 'md5' + outer
112-
con.password(md5password)
109+
con.password(utils.postgresMd5PasswordHash(self.user, self.password, msg.salt))
113110
}))
114111

115112
con.once('backendKeyData', function (msg) {

lib/utils.js

+8
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,20 @@ const md5 = function (string) {
138138
return crypto.createHash('md5').update(string, 'utf-8').digest('hex')
139139
}
140140

141+
// See AuthenticationMD5Password at https://www.postgresql.org/docs/current/static/protocol-flow.html
142+
const postgresMd5PasswordHash = function (user, password, salt) {
143+
var inner = md5(password + user)
144+
var outer = md5(Buffer.concat([Buffer.from(inner), salt]))
145+
return 'md5' + outer
146+
}
147+
141148
module.exports = {
142149
prepareValue: function prepareValueWrapper (value) {
143150
// this ensures that extra arguments do not get passed into prepareValue
144151
// by accident, eg: from calling values.map(utils.prepareValue)
145152
return prepareValue(value)
146153
},
147154
normalizeQueryConfig: normalizeQueryConfig,
155+
postgresMd5PasswordHash: postgresMd5PasswordHash,
148156
md5: md5
149157
}

test/integration/connection/test-helper.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ var connect = function (callback) {
2121
con.password(helper.args.password)
2222
})
2323
con.once('authenticationMD5Password', function (msg) {
24-
var inner = utils.md5(helper.args.password + helper.args.user)
25-
var outer = utils.md5(Buffer.concat([Buffer.from(inner), msg.salt]))
26-
con.password('md5' + outer)
24+
con.password(utils.postgresMd5PasswordHash(helper.args.user, helper.args.password, msg.salt));
2725
})
2826
con.once('readyForQuery', function () {
2927
con.query('create temp table ids(id integer)')

test/unit/client/md5-password-tests.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ test('md5 authentication', function () {
1111
test('responds', function () {
1212
assert.lengthIs(client.connection.stream.packets, 1)
1313
test('should have correct encrypted data', function () {
14-
var encrypted = utils.md5(client.password + client.user)
15-
encrypted = utils.md5(encrypted + salt.toString('binary'))
16-
var password = 'md5' + encrypted
14+
var password = utils.postgresMd5PasswordHash(client.user, client.password, salt)
1715
// how do we want to test this?
1816
assert.equalBuffers(client.connection.stream.packets[0], new BufferList()
1917
.addCString(password).join(true, 'p'))

0 commit comments

Comments
 (0)