【问题标题】:how to sanitize sql.js inputs?如何清理 sql.js 输入?
【发布时间】:2021-11-24 09:33:27
【问题描述】:

我正在使用 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 注入。我能做些什么来防止这样的问题?非常感谢。

【问题讨论】:

    标签: javascript sql sqlite electron sql.js


    【解决方案1】:

    您可以使用绑定参数。这些是传递给数据库引擎并且不作为 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 documentationsqljs documentation

    【讨论】:

      猜你喜欢
      • 2014-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 1970-01-01
      • 2021-01-26
      • 2020-09-03
      • 2016-04-21
      相关资源
      最近更新 更多