【问题标题】:How to safely insert parameters into a SQL query and get the resulting query?如何安全地将参数插入 SQL 查询并获取结果查询?
【发布时间】:2021-01-04 16:51:06
【问题描述】:

我必须使用不符合 DBAPI 的库与数据库交互(qds_sdk 用于 Qubole)。这个库只允许发送不带参数的原始 SQL 查询。因此,我想要一种防 SQL 注入的方法来将参数插入到查询中,并在 Python 中获取结果格式的查询。类似于下面示例中的 format_sql 函数:

sql = 'select * from table where id = ?'
formatted_sql = format_sql(sql, (123,))  # 'select * from table where id = 123'

这完全可能还是 RDBMS 太特定了?

【问题讨论】:

  • cursor.execute。它将参数作为参数。
  • 谢谢,但正如我所说,该库不符合 DBAPI。完全没有游标的概念。

标签: python sql sql-injection qubole


【解决方案1】:

我不太了解 Quoble 及其接受的 SQL 方言,而且您可能要处理的数据类型范围很广。但在许多情况下,将参数转换为字符串,然后通过将单引号字符加倍或在它们前面加上反斜杠(例如,MySQL 允许这两种方法)来转义单引号字符,这可能是您能做的最好的事情。我会使用 %s 作为您的伪准备语句的占位符:

from datetime import date

def format_sql(query, args):
    new_args = []
    for arg in args:
        new_args.append(str(arg).replace("'", "''"))
    return query.replace("%s", "'%s'") % tuple(new_args)

print(format_sql("insert into mytable(x, y, z) values(%s, %s, %s)", ("Booboo's car", date.today(), 2)))

打印:

insert into mytable(x, y, z) values('Booboo''s car', '2021-01-04', '2')

如果%s 有任何可能出现在您的 SQL 中,而不是作为占位符,那么您需要将单引号放在那些实际占位符并且没有函数 format_sql 执行该函数的事件周围:

from datetime import date

def format_sql(query, args):
    new_args = []
    for arg in args:
        new_args.append(str(arg).replace("'", "''"))
    return query % tuple(new_args)

print(format_sql("insert into mytable(x, y, z) values('%s', '%s', '%s')", ("Booboo's car", date.today(), 2)))

【讨论】:

    猜你喜欢
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    相关资源
    最近更新 更多