Skip to content

Commit 9882078

Browse files
committed
show how to use mysql.escape in the sql-injection qhelp
1 parent 7d801e0 commit 9882078

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

javascript/ql/src/Security/CWE-089/SqlInjection.inc.qhelp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ immune to injection attacks.
5555
</p>
5656

5757
<sample src="examples/SqlInjectionFix.js" />
58+
59+
<p>
60+
Alternatively, we can use a library like <code>sqlstring</code> to
61+
escape the user input before embedding it into the query string:
62+
</p>
63+
<sample src="examples/SqlInjectionFix2.js" />
5864
</example>
5965

6066
<example>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const app = require("express")(),
2+
pg = require("pg"),
3+
SqlString = require('sqlstring'),
4+
pool = new pg.Pool(config);
5+
6+
app.get("search", function handler(req, res) {
7+
// GOOD: the category is escaped using mysql.escape
8+
var query1 =
9+
"SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" +
10+
SqlString.escape(req.params.category) +
11+
"' ORDER BY PRICE";
12+
pool.query(query1, [], function(err, results) {
13+
// process results
14+
});
15+
});

0 commit comments

Comments
 (0)