mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
* Remove password from stringified outputs Theres a security concern where if you're not careful and you include your client or pool instance in console.log or stack traces it might include the database password. To widen the pit of success I'm making that field non-enumerable. You can still get at it...it just wont show up "by accident" when you're logging things now. The backwards compatiblity impact of this is very small, but it is still technically somewhat an API change so...8.0. * Implement feedback * Fix more whitespace the autoformatter changed * Simplify code a bit * Remove password from stringified outputs (#2070) * Keep ConnectionParameters’s password property writable `Client` writes to it when `password` is a function. * Avoid creating password property on pool options when it didn’t exist previously. * Allow password option to be non-enumerable to avoid breaking uses like `new Pool(existingPool.options)`. * Make password property definitions consistent in formatting and configurability. Co-authored-by: Charmander <~@charmander.me>
148 lines
4.5 KiB
JavaScript
148 lines
4.5 KiB
JavaScript
'use strict'
|
|
/**
|
|
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* README.md file in the root directory of this source tree.
|
|
*/
|
|
|
|
var dns = require('dns')
|
|
|
|
var defaults = require('./defaults')
|
|
|
|
var parse = require('pg-connection-string').parse // parses a connection string
|
|
|
|
var val = function (key, config, envVar) {
|
|
if (envVar === undefined) {
|
|
envVar = process.env['PG' + key.toUpperCase()]
|
|
} else if (envVar === false) {
|
|
// do nothing ... use false
|
|
} else {
|
|
envVar = process.env[envVar]
|
|
}
|
|
|
|
return config[key] ||
|
|
envVar ||
|
|
defaults[key]
|
|
}
|
|
|
|
var useSsl = function () {
|
|
switch (process.env.PGSSLMODE) {
|
|
case 'disable':
|
|
return false
|
|
case 'prefer':
|
|
case 'require':
|
|
case 'verify-ca':
|
|
case 'verify-full':
|
|
return true
|
|
}
|
|
return defaults.ssl
|
|
}
|
|
|
|
var ConnectionParameters = function (config) {
|
|
// if a string is passed, it is a raw connection string so we parse it into a config
|
|
config = typeof config === 'string' ? parse(config) : config || {}
|
|
|
|
// if the config has a connectionString defined, parse IT into the config we use
|
|
// this will override other default values with what is stored in connectionString
|
|
if (config.connectionString) {
|
|
config = Object.assign({}, config, parse(config.connectionString))
|
|
}
|
|
|
|
this.user = val('user', config)
|
|
this.database = val('database', config)
|
|
this.port = parseInt(val('port', config), 10)
|
|
this.host = val('host', config)
|
|
|
|
// "hiding" the password so it doesn't show up in stack traces
|
|
// or if the client is console.logged
|
|
Object.defineProperty(this, 'password', {
|
|
configurable: true,
|
|
enumerable: false,
|
|
writable: true,
|
|
value: val('password', config)
|
|
})
|
|
|
|
this.binary = val('binary', config)
|
|
this.ssl = typeof config.ssl === 'undefined' ? useSsl() : config.ssl
|
|
this.client_encoding = val('client_encoding', config)
|
|
this.replication = val('replication', config)
|
|
// a domain socket begins with '/'
|
|
this.isDomainSocket = (!(this.host || '').indexOf('/'))
|
|
|
|
this.application_name = val('application_name', config, 'PGAPPNAME')
|
|
this.fallback_application_name = val('fallback_application_name', config, false)
|
|
this.statement_timeout = val('statement_timeout', config, false)
|
|
this.idle_in_transaction_session_timeout = val('idle_in_transaction_session_timeout', config, false)
|
|
this.query_timeout = val('query_timeout', config, false)
|
|
|
|
if (config.connectionTimeoutMillis === undefined) {
|
|
this.connect_timeout = process.env.PGCONNECT_TIMEOUT || 0
|
|
} else {
|
|
this.connect_timeout = Math.floor(config.connectionTimeoutMillis / 1000)
|
|
}
|
|
|
|
if (config.keepAlive === false) {
|
|
this.keepalives = 0
|
|
} else if (config.keepAlive === true) {
|
|
this.keepalives = 1
|
|
}
|
|
|
|
if (typeof config.keepAliveInitialDelayMillis === 'number') {
|
|
this.keepalives_idle = Math.floor(config.keepAliveInitialDelayMillis / 1000)
|
|
}
|
|
}
|
|
|
|
// Convert arg to a string, surround in single quotes, and escape single quotes and backslashes
|
|
var quoteParamValue = function (value) {
|
|
return "'" + ('' + value).replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'"
|
|
}
|
|
|
|
var add = function (params, config, paramName) {
|
|
var value = config[paramName]
|
|
if (value !== undefined && value !== null) {
|
|
params.push(paramName + '=' + quoteParamValue(value))
|
|
}
|
|
}
|
|
|
|
ConnectionParameters.prototype.getLibpqConnectionString = function (cb) {
|
|
var params = []
|
|
add(params, this, 'user')
|
|
add(params, this, 'password')
|
|
add(params, this, 'port')
|
|
add(params, this, 'application_name')
|
|
add(params, this, 'fallback_application_name')
|
|
add(params, this, 'connect_timeout')
|
|
|
|
var ssl = typeof this.ssl === 'object' ? this.ssl : this.ssl ? { sslmode: this.ssl } : {}
|
|
add(params, ssl, 'sslmode')
|
|
add(params, ssl, 'sslca')
|
|
add(params, ssl, 'sslkey')
|
|
add(params, ssl, 'sslcert')
|
|
add(params, ssl, 'sslrootcert')
|
|
|
|
if (this.database) {
|
|
params.push('dbname=' + quoteParamValue(this.database))
|
|
}
|
|
if (this.replication) {
|
|
params.push('replication=' + quoteParamValue(this.replication))
|
|
}
|
|
if (this.host) {
|
|
params.push('host=' + quoteParamValue(this.host))
|
|
}
|
|
if (this.isDomainSocket) {
|
|
return cb(null, params.join(' '))
|
|
}
|
|
if (this.client_encoding) {
|
|
params.push('client_encoding=' + quoteParamValue(this.client_encoding))
|
|
}
|
|
dns.lookup(this.host, function (err, address) {
|
|
if (err) return cb(err, null)
|
|
params.push('hostaddr=' + quoteParamValue(address))
|
|
return cb(null, params.join(' '))
|
|
})
|
|
}
|
|
|
|
module.exports = ConnectionParameters
|