【问题标题】:How to fix function with postgres query to prevent SQL injection如何使用 postgres 查询修复函数以防止 SQL 注入
【发布时间】:2019-06-11 22:19:59
【问题描述】:

我需要帮助修复我编写的函数(在 node.js 无服务器微服务中)以防止 sql 注入。我是安全主题的新手,所以任何正确方向的想法或观点都会很棒,谢谢!

这是 RecipientAlerts.js 中的函数:

  updateRecipient(email, body, callback) {
    helper.removeRecipient(this.db, email) // clears old data
      .then(() => {
        const values = Object.keys(body).map(industry => 
          body[industry].map(company => 
            `('${company}', '${industry}', '${email}')`).join(', ')).join(', ');
        const insert =`INSERT INTO recipient_list(company, industry, email_address) VALUES `;
        this.db.queries.none(insert + values)             
          .catch((error) => {
            console.log(error, 'error on insert query', callback);
          });
      })
      .then(() => {
        console.log('successfully updated', null, callback);
      })
      .catch((error) => {
        console.log(error, 'failed to update recipient', callback);
      });
  }

这是 recipient.json

{ 
    "pathParameters": {
        "email": "john@gmail.com"
    },
    "body": {
        "tech": ["Apple"],
        "hospitality": ["McDonalds", "Subway"],
        "banking": ["Citi", "HSBC"]
    }
}

预期的结果(我目前得到并希望保持不变)是: recipient_list 表:

company       |  industry   | email_address
______________|_____________|________________
Apple         | tech        | john@gmail.com
--------------|-------------|---------------
McDonalds     | hospitality | john@gmail.com
--------------|-------------|---------------
Subway        | hospitality | john@gmail.com
--------------|-------------|---------------
Citi          | banking     | john@gmail.com
--------------|-------------|---------------
HSBC          | banking     | john@gmail.com

【问题讨论】:

    标签: node.js postgresql sql-injection pg-promise


    【解决方案1】:

    按照 pg-promiseMulti-Row Inserts 示例,声明一次 ColumnSet 对象:

    const cs = new pgp.helpers.ColumnSet([
        'company',
        'industry',
        {name: 'email_address', prop: 'email'}
    ], {table: 'recipient_list'});
    

    然后你可以把你的代码改成这样:

    updateRecipient(email, body, callback)
    {
        helper.removeRecipient(this.db, email) // clears old data
            .then(() => {
                const insert = pgp.helpers.insert(body, cs); // generating the INSERT query
                this.db.queries.none(insert) // executing the INSERT query
                    .catch((error) => {
                        console.log(error, 'error on insert query', callback);
                    });
            })
            .then(() => {
                console.log('successfully updated', null, callback);
            })
            .catch((error) => {
                console.log(error, 'failed to update recipient', callback);
            });
    }
    

    SQL 将以这种方式安全生成,并且不受 SQL 注入的影响。

    【讨论】:

      【解决方案2】:

      我强烈推荐使用 sequelize ,它会自动处理它

      Here is the doc

      【讨论】:

        猜你喜欢
        • 2020-01-30
        • 2022-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-01
        • 2018-08-21
        • 2020-09-16
        • 1970-01-01
        相关资源
        最近更新 更多