Flask 分页 iter 页面防止 SQL 注入并在 HTML 模板中添加下一页按钮

Flask pagination iter pages preveneting SQL injection and adding next-page button in HTML template

我正在尝试在我的 HTML 中创建一个按钮,单击该按钮时将转到 next/previous 页面。我试过分页和 peewee,但似乎没用。

这是我的应用程序:

from flask import Flask,render_template, request, json
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'example'
app.config['MYSQL_USER'] = 'example'
app.config['MYSQL_PASSWORD'] = 'example'
app.config['MYSQL_DB'] = 'fund_raiser'
mysql = MySQL(app)

@app.route('/funds/pages/', defaults={'page':0})
@app.route('/funds/pages/<int:page>')
def fund_pages(page):
    perpage = 5
    first_page = page*perpage
    cur = mysql.connection.cursor()
    cur.execute("select * from fund_table limit "+str(first_page)+", "+str(perpage)+";", mysql.connection.commit())
    data = cur.fetchall()
    return render_template('funds.html', data=data)

在 html 页面中的什么位置添加 href 标签?正确使用的变量是什么?

此外,我的代码是否容易受到此行中 SQL 注入的攻击,如果是,我该如何解决:

cur.execute("select * from fund_table limit "+str(first_page)+", "+str(perpage)+";", mysql.connection.commit())

只要您只接受 int 作为用户输入,SQL 注入的风险就会大大降低 - 稍后将其转换为字符串不是问题。我会称之为安全,因为如果路由在 /funds/pages/

之后接收到不是 int 的内容,它会抛出错误

但是,如果你想使用准备好的语句(推荐的"secure"方法),你应该将执行更改为

cur.execute("select * from fund_table limit %s, %s;", params=(first_page, perpage), mysql.connection.commit())

单独发送查询参数不仅可以防止sql注入,还可以提高性能,因为查询的整体结构是预先定义的。一旦你接受除整数以外的任何东西进行分页,你绝对应该使用它。

无法真正回答您的第一个问题,因为没有提供代码