【问题标题】:Sanitizing user inputs when the user input is a schema name in node postgres当用户输入是节点 postgres 中的模式名称时清理用户输入
【发布时间】:2020-11-04 12:01:11
【问题描述】:

我正在使用https://node-postgres.com/ 编写一个相当简单的应用程序,但我确实有一个复杂的约束导致了我的问题。即,我需要我的 SQL 语句来根据用户输入选择架构

SELECT * FROM {some user value}.tableName

但是当我尝试正常参数化时,我得到一个语法错误

{"errorType":"error","errorMessage":"syntax error at or near \"$1\""

这似乎表明该值没有按预期替换。

有没有办法清理传入的值而不在查询中执行它?还是有一种适当的方法可以正确执行查询中的这个值?

(我想避免为了满足我的基本需求而对类似 sequilize 的大型库进行更改)

【问题讨论】:

    标签: node.js node-postgres input-sanitization


    【解决方案1】:

    事实证明,有一个很好的方法可以利用库的胆量来做到这一点!它深入到原型中,所以如果这成为一个更常规的事情,研究像pg-format 这样的东西可能是一个不错的选择。但现在这行得通。

    const sanitizedKey = db.Client.prototype.escapeIdentifier(incomingData.orgKey)
    const result = await db.query(builder(`
            WITH policies_threatened_by_incident AS (
                select p.alert_id, p.tiv, p.distance
                from ${sanitizedKey}.wf_alert_policies_active p
    ...
    

    向有帮助的贡献者致敬 -> https://github.com/brianc/node-postgres/issues/2295#issuecomment-664767339

    【讨论】:

      猜你喜欢
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      • 2010-09-07
      • 2018-11-04
      • 1970-01-01
      相关资源
      最近更新 更多