Skip to content

Commit c0a381e

Browse files
committed
Fix vulnerability
1 parent 3de22ba commit c0a381e

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

lib/result.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
var types = require('pg-types');
10+
var escape = require('js-string-escape');
1011

1112
//result object returned from query
1213
//in the 'end' event and also
@@ -75,13 +76,13 @@ Result.prototype.addRow = function(row) {
7576

7677
var inlineParser = function(fieldName, i) {
7778
return "\nthis['" +
78-
//fields containing single quotes will break
79-
//the evaluated javascript unless they are escaped
80-
//see https://github.com/brianc/node-postgres/issues/507
81-
//Addendum: However, we need to make sure to replace all
82-
//occurences of apostrophes, not just the first one.
83-
//See https://github.com/brianc/node-postgres/issues/934
84-
fieldName.replace(/'/g, "\\'") +
79+
// fields containing single quotes will break
80+
// the evaluated javascript unless they are escaped
81+
// see https://github.com/brianc/node-postgres/issues/507
82+
// Addendum: However, we need to make sure to replace all
83+
// occurences of apostrophes, not just the first one.
84+
// See https://github.com/brianc/node-postgres/issues/934
85+
escape(fieldName) +
8586
"'] = " +
8687
"rowData[" + i + "] == null ? null : parsers[" + i + "](rowData[" + i + "]);";
8788
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"main": "./lib",
2020
"dependencies": {
2121
"buffer-writer": "1.0.1",
22+
"js-string-escape": "1.0.1",
2223
"packet-reader": "0.2.0",
2324
"pg-connection-string": "0.1.3",
2425
"pg-pool": "1.*",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var pg = require('./test-helper').pg
2+
3+
var sql = 'SELECT 1 AS "\\\'/*", 2 AS "\\\'*/\n + process.exit(-1)] = null;\n//"'
4+
5+
var client = new pg.Client()
6+
client.connect()
7+
client.query(sql, function (err, res) {
8+
if (err) throw err
9+
client.end()
10+
})

0 commit comments

Comments
 (0)