您的位置:首页 > 编程语言 > PHP开发

phpcms V9 BLind SQL Injection Vulnerability

2011-01-23 01:44 337 查看
一、环境
Flask==0.10.1
Werkzeug==0.10.4
Jinja2==2.8

二、问题
1、蓝图架构

--app

|-user

|-static

|-templates

|-index.html

|-views.py

|-__init_.py

|-box

|-static

|-templates

|-index.html

|-views.py

|-__init_.py
|-__init__.py
2、问题重现

user和box是两个蓝图,全部注册在app项目中

如果在user的views中这样【render_template('index.html')】渲染index.html,可能会访问到别的蓝图下的同名模板文件。

三、分析
1、app创建时会从Flask注册函数中读取template_folder,如果没有设置,默认是app/templates,作为全局jinja_loader

2、render_template函数会首先访问app的全局jinja_loader,从中读取模板路径。

3、访问不到就会循环访问所有注册蓝图的jinja_loader。由于循环访问的是所有蓝图的无序键值字典, 所以恰巧碰到其中一个蓝图下有同名模板文件,便会渲染这个模板文件。
4、附上关键代码(/usr/lib/python2.6/site-packages/flask/templating.py)
def get_source(self, environment, template):
for loader, local_name in self._iter_loaders(template):
try:
return loader.get_source(environment, local_name)
except TemplateNotFound:
pass

raise TemplateNotFound(template)

def _iter_loaders(self, template):
loader = self.app.jinja_loader
if loader is not None:
yield loader, template

# old style module based loaders in case we are dealing with a
# blueprint that is an old style module
try:
module, local_name = posixpath.normpath(template).split('/', 1)
blueprint = self.app.blueprints[module]
if blueprint_is_module(blueprint):
loader = blueprint.jinja_loader
if loader is not None:
yield loader, local_name
except (ValueError, KeyError):
pass
for blueprint in itervalues(self.app.blueprints):
if blueprint_is_module(blueprint):
continue
loader = blueprint.jinja_loader
if loader is not None:
yield loader, template


四、解决办法
1、官方给出的解决办法(妥协)
将所有模板文件放在一起,按蓝图名字划分子目录。也就是全部都放在app的全局jinja_loader目录。访问的时候,使用render_template('blueprint_name/template_file.html')这种方式。参见:http://flask.pocoo.org/docs/0.10/blueprints/#templates
2、其他友人的解决办法

在创建app时修改全局jinja_loader,改为ChoiceLoader类型,使每个注册的蓝图loader都作为一个字典元素插入到ChoiceLoader中的PrefixLoader中,这样在访问时就可以在PrefixLoader中根据传进的蓝图名查找到当前loader路径。
作者称这样的好处不用修改项目架构。不过这样也会有个问题:使用渲染函数时需要修改模板字符串,因为是PrefixLoader,需要添加delimiter标示,比如访问user下的index.html要这样render_template('user.index.html');参见:http://fewstreet.com/2015/01/16/flask-blueprint-templates.html

3、github上的commit
这种方法修改了render_template函数,在渲染模板时判断是否是蓝图以及是否存在蓝图loader。如果存在优先使用蓝图自身的loader。这种方法比较干脆直接,参见:https://github.com/mitsuhiko/flask/pull/1537/files
4、我的解决方式

参照了github解决方法的思路,我在注册app时通过before_request修改全局jinja_loader,在收到请求时临时把当前蓝图的loder路径添加到全局jinja_loader的searchpath列表中,这样做的目的:①避免修改项目结构,②避免修改render_template参数,③避免修改框架代码,不方便迁移部署。

五、我的代码(app/__init__.py)
def create_app(env='development'):
"""此处略去若干行"""
@app.before_request
def before_request():
if request.blueprint is not None:
bp = app.blueprints[request.blueprint]
if bp.jinja_loader is not None:
newsearchpath = bp.jinja_loader.searchpath + app.jinja_loader.searchpath
app.jinja_loader.searchpath = newsearchpath
#以下为2016-03-11日更新:
#如果访问非蓝图模块或蓝图中没有指定template_folder,默认使用app注册时指定的全局template_floder.
else:
app.jinja_loader.searchpath = app.jinja_loader.searchpath[-1:]
else:
app.jinja_loader.searchpath = app.jinja_loader.searchpath[-1:]


本文出自 “非同小可” 博客,请务必保留此出处http://xuke1668.blog.51cto.com/2129485/1712081
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: