mirror of
https://github.com/github/codeql.git
synced 2026-04-26 09:15:12 +02:00
show how to use mysql.escape in the sql-injection qhelp
This commit is contained in:
@@ -55,6 +55,12 @@ immune to injection attacks.
|
||||
</p>
|
||||
|
||||
<sample src="examples/SqlInjectionFix.js" />
|
||||
|
||||
<p>
|
||||
Alternatively, we can use a library like <code>sqlstring</code> to
|
||||
escape the user input before embedding it into the query string:
|
||||
</p>
|
||||
<sample src="examples/SqlInjectionFix2.js" />
|
||||
</example>
|
||||
|
||||
<example>
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
const app = require("express")(),
|
||||
pg = require("pg"),
|
||||
SqlString = require('sqlstring'),
|
||||
pool = new pg.Pool(config);
|
||||
|
||||
app.get("search", function handler(req, res) {
|
||||
// GOOD: the category is escaped using mysql.escape
|
||||
var query1 =
|
||||
"SELECT ITEM,PRICE FROM PRODUCT WHERE ITEM_CATEGORY='" +
|
||||
SqlString.escape(req.params.category) +
|
||||
"' ORDER BY PRICE";
|
||||
pool.query(query1, [], function(err, results) {
|
||||
// process results
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user