Skip to content

Commit 9bcf55d

Browse files
committed
Fix vulnerability
1 parent bc0b03e commit 9bcf55d

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

lib/result.js

Lines changed: 8 additions & 7 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dependencies": {
2121
"buffer-writer": "1.0.1",
2222
"packet-reader": "0.3.1",
23+
"js-string-escape": "1.0.1",
2324
"pg-connection-string": "0.1.3",
2425
"pg-pool": "1.*",
2526
"pg-types": "1.*",
Lines changed: 10 additions & 0 deletions
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)