我在python烧瓶项目中使用pyodbc作为SQLite DB连接。我知道并理解SQL注入,但这是我第一次处理它。我试着执行一些
我有一个函数将database.py文件中的SQL字符串连接起来:
def open_issue(self, data_object):
cursor = self.conn.cursor()
# data_object is the issue i get from the user
name = data_object["name"]
text = data_object["text"]
rating_sum = 0
# if the user provides an issue
if name:
# check if issue is already in db
test = cursor.execute(f'''SELECT name FROM issue WHERE name = "{name}"''')
data = test.fetchall()
# if not in db insert
if len(data) == 0:
# insert the issue
cursor.executescript(f'''INSERT INTO issue (name, text, rating_sum)
VALUES ("{name}", "{text}", {rating_sum})''')
else:
print("nothing inserted!")在api.py文件中,open_issue()函数被调用:
@self.app.route('/open_issue')
def insertdata():
# data sent from client
# data_object = flask.request.json
# unit test dictionary
data_object = {"name": "injection-test-table",
"text": "'; CREATE TABLE 'injected_table-1337';--"}
DB().open_issue(data_object)"';CREATE‘injection _ TABLE -1337’;--“sql注入没有创建injected_table-1337,而是像字符串一样插入到注入测试表的文本列中。
因此,我真的不知道我是否对SQL注入的标准方法是安全的(这个项目将只在本地托管,但良好的安全性总是受欢迎的)。
其次:是否有方法使用pyodbc检查字符串是否包含sql语法或符号,以便在我的示例中不会插入任何内容,或者是否需要手动检查字符串?
非常感谢
发布于 2020-11-18 14:23:25
事实证明,使用SQLite时,SQL注入问题的风险要小得多,因为默认情况下,SQLite的内置sqlite3模块和SQLite ODBC驱动程序都不允许在单个.execute调用(通常称为“匿名代码块”)中执行多个语句。此代码:
thing = "'; CREATE TABLE bobby (id int primary key); --"
sql = f"SELECT * FROM table1 WHERE txt='{thing}'"
crsr.execute(sql)为sqlite3抛出这个
sqlite3.Warning:一次只能执行一条语句。
这是为了SQLite ODBC
pyodbc.Error:( 'HY000 ',‘HY000只允许一个SQL语句(-1) (SQLExecDirectW)')
不过,您应该遵循最佳实践,并使用适当的参数化查询。
thing = "'; CREATE TABLE bobby (id int primary key); --"
sql = "SELECT * FROM table1 WHERE txt=?"
crsr.execute(sql, (thing, ))因为这也将正确地处理参数值,如果直接注入会导致错误,例如,
thing = "it's good to avoid SQL injection"https://stackoverflow.com/questions/64890851
复制相似问题