parameterbinding - 参数绑定(bind)不适用于 SQLite PRAGMA table_info

标签 parameterbinding

我正在使用用于 Python 的 sqlite3。为什么表达式的参数绑定(bind)不起作用:

self.cursor.execute("PRAGMA table_info(?)", table_name)

正如预期的那样?对于任何其他 SELECT查询它按预期替换我的参数。我现在用
self.cursor.execute("PRAGMA table_info('%s')" % table_name)

但这对于 SQL 注入(inject)是不安全的。我该如何解决这个问题?

最佳答案

我想做同样的事情,但它看起来像 it's not possible to bind parameters to Sqlite PRAGMAs .

为了保持安全(以及我自己最终可能会做的事情),您可以做的是 query all the table names in the current Sqlite database在 SQL 中,像这样:

SELECT * FROM sqlite_master

或者,要获取表格并忽略 View ,请执行以下操作:
SELECT * FROM sqlite_master where type="table"

然后将这些表名存储在一个数组/列表/集合中。现在您在数据库中拥有所有可能表的列表,您可以简单地检查用户输入以查看它是否与数组中的表之一匹配。如果是,那么直接插入字符串是安全的,不会有SQL注入(inject)的机会。从本质上讲,它正在根据白名单进行清理。

在 Python 中,它看起来像这样:
if table_name in tables:
    self.cursor.execute("PRAGMA table_info('%s')" % table_name)
else:
    print("Bad table name: %s" % table_name)

关于parameterbinding - 参数绑定(bind)不适用于 SQLite PRAGMA table_info,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39985599/

相关文章:

powershell - Get-Service 参数绑定(bind)未按预期工作

php - Mysqli参数绑定(bind)问题

model-binding - WebApi2 : Custom parameter binding to bind partial parameters

php - 绑定(bind)参数到 Db::raw laravel 查询

php - PDO 不绑定(bind)参数

php - Laravel 查询生成器多次绑定(bind)参数

powershell - PS 流水线多参数绑定(bind)