我正在使用sql.js管理我为一个电子应用程序创建的SQLite文件。我的问题是,我想确保所有的输入都是经过消毒的。到目前为止,我使用的语句如下:
const SQL = await sqljs();
const db = new SQL.Database();
// These values are just examples and will be user inputs through electron.
let id = 1;
let name = 'row name';
db.run(`INSERT INTO tableName VALUES (${}, 'hello');`);我非常确定这种方式是不安全的,并且会导致SQL注入。我能做些什么来防止这样的问题?非常感谢。
发布于 2021-10-04 02:16:46
您可以使用绑定参数。以下是传递到数据库引擎的值,不会作为SQL语句的一部分进行解析:
let id = 1;
let name = 'row name';
/* Either pass in a dictionary to use named bound parameters */
db.run("INSERT INTO tableName VALUES(:id, :name)", { ':id': id, ':name': name });
/* Or an array to use positional bound parameters */
db.run("INSERT INTO tableName VALUES(?, ?)", [id, name]);有关更多信息,请访问SQLite documentation和sqljs documentation。
https://stackoverflow.com/questions/69427843
复制相似问题