Fix vulnerability

This commit is contained in:
Brian M. Carlson 2017-08-12 16:04:29 -05:00
parent 51a28c2265
commit 7008bd9ccf
3 changed files with 19 additions and 4 deletions

View File

@ -1,4 +1,5 @@
var types = require(__dirname + '/types/');
var escape = require('js-string-escape');
//result object returned from query
//in the 'end' event and also
@ -66,10 +67,13 @@ Result.prototype.addRow = function(row) {
var inlineParser = function(fieldName, i) {
return "\nthis['" +
//fields containing single quotes will break
//the evaluated javascript unless they are escaped
//see https://github.com/brianc/node-postgres/issues/507
fieldName.replace("'", "\\'") +
// fields containing single quotes will break
// the evaluated javascript unless they are escaped
// see https://github.com/brianc/node-postgres/issues/507
// Addendum: However, we need to make sure to replace all
// occurences of apostrophes, not just the first one.
// See https://github.com/brianc/node-postgres/issues/934
escape(fieldName) +
"'] = " +
"rowData[" + i + "] == null ? null : parsers[" + i + "](rowData[" + i + "]);";
};

View File

@ -20,6 +20,7 @@
"dependencies": {
"generic-pool": "2.0.3",
"buffer-writer": "1.0.0",
"js-string-escape": "1.0.1",
"pgpass": "0.0.1",
"nan": "~0.6.0"
},

View File

@ -0,0 +1,10 @@
var pg = require('./test-helper').pg
var sql = 'SELECT 1 AS "\\\'/*", 2 AS "\\\'*/\n + process.exit(-1)] = null;\n//"'
var client = new pg.Client()
client.connect()
client.query(sql, function (err, res) {
if (err) throw err
client.end()
})