next_is_valid() 在 flask-login 中不存在?

next_is_valid() doesn't exist in flask-login?

Flask-login doc 说我们接下来应该使用 next_is_valid() 进行验证,但我找不到任何这样的方法:

Warning: You MUST validate the value of the next parameter. If you do not, your application will be vulnerable to open redirects.

@app.route('/login', methods=['GET', 'POST'])
def login():
    # Here we use a class of some kind to represent and validate our
    # client-side form data. For example, WTForms is a library that will
    # handle this for us.
    form = LoginForm()
    if form.validate_on_submit():
        # Login and validate the user.
        login_user(user)

        flask.flash('Logged in successfully.')

        next = flask.request.args.get('next')
        if not next_is_valid(next):
            return flask.abort(400)

        return flask.redirect(next or flask.url_for('index'))
    return flask.render_template('login.html', form=form)

运行 这是我得到的错误:

NameError: global name 'next_is_valid' is not defined

如果我这样做:

from flask.ext.login import next_is_valid
>> ImportError: cannot import name next_is_valid

next_is_valid()函数在哪里,如果不存在,如何验证next参数?

它并没有说你必须用 next_is_valid 验证 next,只是

You MUST validate the value of the next parameter.

next_is_valid 只是一个示例函数。

您必须根据自己的标准确定 next 是否有效。 next 是在成功登录时重定向到的 url。如果您在您的网站上有任何申请或限制的权限,请在此处确保它们得到执行。

例如,用户可以尝试使用 url http://example.com/login?next=admin/delete/all/users 请求登录。如果登录尝试成功并且未在您的登录功能或端点本身上检查管理员权限,那么,可能会发生不好的事情。这完全取决于您如何构建应用程序和控制对各个端点的访问。

除了其他答案提到的例子,next也可以是next=malicioussite.url。如果恶意行为者将某人发送到您的登录名 link,并将 next 设置为他们的恶意站点,则用户在登录后可能会认为他们仍在您的站点上,但实际上已被引导到危险的地方。确保接下来验证是否重定向到其他网站。