When dealing with POST data (json) in my express app, I have an endpoint that allows me to query a MySQL database safely. However, I am concerned about potential vulnerabilities when escaping and altering strings. Is there a risk of exploitation?
The goal is to modify the incoming string by inserting a '%' before the closing single quote in the LIKE clause.
var searchTerm = mysql.escape(req.body.firstName)
var newStr = "'" + searchTerm.substring(1, searchTerm.length - 1) + "%'"
// Example: 'alex' => 'alex%'
This method works fine, but I am curious if there is a more secure or recommended way to achieve this task.
Current query being used:
SELECT * FROM tbl WHERE col LIKE " + newStr + " ORDER BY key
For those seeking reference:
- I am utilizing the following package: https://github.com/felixge/node-mysql
UPDATE/ANSWER
In response to EternalHour's suggestion, a more appropriate syntax would be:
var newStr = req.body.firstName + "%"
var sql = "SELECT cols FROM tbl WHERE col LIKE ? ORDER BY key"
sql = mysql.format(sql, newStr)