|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 金刚 于 2020-6-23 17:31 编辑
对URL进行安全验证
虽然我们已经实现了重定向会上一个页面的功能,但是安全问题不容忽视,鉴于referer和next容易被串篡改的特性,我们需要对这些值进行验证,否则会形成开放重定向漏洞
以URL中的next参数为例,next变量以字符串的形式写在url里,因此任何人都可以发给某个用户一个包含next变量指向任何站点的连接,那么就会误导用户进入钓鱼网站。
我们可以验证next变量指向的url地址是否属于我们的应用内,否则不允许重定向。
确保URL安全的关键是判断URL是否属于程序内部,可以通过判断url的host、协议等信息是否和程序内部的url一致,如果一致则认识是可信的url
代码中is_safe_url()方法接受目标URL作为参数,通过request.host_url获取程序内部的主机URL,然后使用urljoin()函数将目标URL转为绝对URL。接着,分别使用urlparse模块提供的urlparse()解析两个url,最后对目标url的url模式和主机地址进行验证,确保只属于程序内部的url才会被返回。
在执行重定向会上一个页面的redirect_back()函数中,使用is_safe_url()验证next和referer的值
代码:
- # from urlparse import urlparse, urljoin # python3需要从urllib.parse导入
- from urllib.parse import urlparse, urljoin
- from flask import request, Flask, url_for, redirect
- app = Flask(__name__)
- @app.route('/bar')
- def bar():
- print("request.full_path:", request.full_path)
- return '<h1>Bar page</h1><a href="%s">Do something and redirect </a>' % url_for('do_something', next=request.full_path)
- @app.route('/do_something_and_redirect')
- def do_something():
- return redirect_back()
- def is_safe_url(target):
- print("request.host_url:", request.host_url)
- ref_url = urlparse(request.host_url)
- print("ref_url:", ref_url)
- print("target:", target)
- test_url = urlparse(urljoin(request.host_url, target))
- print("test_url:", test_url)
- print("ref_url.netloc:", ref_url.netloc)
- print("test_url.netloc:", test_url.netloc)
- print("test_url.scheme:", test_url.scheme)
- return test_url.scheme in ('http', 'https') and ref_url.netloc == test_url.netloc
- def redirect_back(default='hello', **kwargs):
- for target in request.args.get('next'), request.referrer:
- if target:
- if is_safe_url(target):
- return redirect(target)
- return redirect(url_for(default, **kwargs))
- if __name__ == '__main__':
- app.run(debug=True)
复制代码
命令行输出:
- PS C:\Users\qingming_wu\Desktop\8888\1\test2> dir
- 目录: C:\Users\qingming_wu\Desktop\8888\1\test2
- Mode LastWriteTime Length Name
- ---- ------------- ------ ----
- -a--- 2020/6/23 10:36 1120 .tags
- -a--- 2020/6/23 10:36 1120 .tags_sorted_by_file
- -a--- 2020/6/22 14:15 353 test2.py
- -a--- 2020/6/23 16:56 1375 test3.py
- PS C:\Users\qingming_wu\Desktop\8888\1\test2> python .\test3.py
- * Serving Flask app "test3" (lazy loading)
- * Environment: production
- WARNING: This is a development server. Do not use it in a production deployment.
- Use a production WSGI server instead.
- * Debug mode: on
- * Restarting with stat
- * Debugger is active!
- * Debugger PIN: 982-552-509
- * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
- request.full_path: /bar?
- 127.0.0.1 - - [23/Jun/2020 17:16:23] "GET /bar HTTP/1.1" 200 -
- 127.0.0.1 - - [23/Jun/2020 17:16:24] "GET /favicon.ico HTTP/1.1" 404 -
- request.host_url: http://127.0.0.1:5000/
- ref_url: ParseResult(scheme='http', netloc='127.0.0.1:5000', path='/', params='', query='', fragment='')
- target: /bar?
- test_url: ParseResult(scheme='http', netloc='127.0.0.1:5000', path='/bar', params='', query='', fragment='')
- ref_url.netloc: 127.0.0.1:5000
- test_url.netloc: 127.0.0.1:5000
- test_url.scheme: http
- 127.0.0.1 - - [23/Jun/2020 17:16:27] "GET /do_something_and_redirect?next=%2Fbar%3F HTTP/1.1" 302 -
- request.full_path: /bar?
- 127.0.0.1 - - [23/Jun/2020 17:16:27] "GET /bar HTTP/1.1" 200 -
复制代码
运行图示
参考文章:
https://www.cnblogs.com/xiaxiaoxu/p/10409886.html
|
|