【问题标题】:How to prevent SQL Injections when inserting JSON Data in Postgres?在 Postgres 中插入 JSON 数据时如何防止 SQL 注入?
【发布时间】:2021-07-19 10:36:09
【问题描述】:

我是 postgres-node 的新手。来自 mySQL 背景,我正在摒弃旧习惯,现在学习新事物。我想在插入数据时防止 SQL 注入。我读到了parameterized query。这适用于 JSON 类型吗?我正在对 postgres 中只有 2 列的表进行 CRUD 操作。我正在处理 JSON 数据(SELECT、INSERT 和 UPDATE)。向表中插入 JSON 数据时如何防止 SQL 注入?

表格

id | info
1  | { "customer": "John Doe", "items": {"product": "Beer","qty": 6}}'
2  | { "customer": "Lily Bush", "items": {"product": "Diaper","qty": 24}}

查询

INSERT INTO orders (info)
VALUES('{ "customer": "Josh William", "items": {"product": "Toy Car","qty": 1}}')

【问题讨论】:

  • 您引用的INSERT 语句中没有SQL 注入危险。也许您应该显示创建语句的代码。

标签: sql node.js postgresql


【解决方案1】:

JSON 类型与其他类型的注入预防没有什么不同:

// totally wrong: concatenated unsafe input straight into the query
client.query(`INSERT INTO orders (info) VALUES (${allegedlyJsonStringifiedUserInput})`);
// totally right: parameterized query, delegating injection safeties to pg
client.query(`INSERT INTO orders (info) VALUES ($1)`, [allegedlyJsonStringifiedUserInput]);

【讨论】:

  • 好的,谢谢。我不确定。我只是在参数化查询中插入整个 json 对象,因为这是一个很大的值,哈哈让我的生活更轻松。干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
  • 2011-09-27
  • 2015-09-07
  • 2011-06-21
  • 2011-10-02
  • 2014-06-18
  • 2011-06-12
相关资源
最近更新 更多