今天用weblhelpers模块,碰到要自定义一个控件,考虑到xss问题,得escape下,结果被tags.escape坑爹了。(=,=|||)
如题:两者的区别请看以下代码
import webhelpers.html.tags as h
import webhelpers.util as u
print '<span>'+h.escape('<script>alert(1)</script>')+'</span>'
print '<span>'+u.html_escape('<script>alert(1)</script>')+'</span>'
h.escape的结果,整句都被escape了,包括前后的span:
<span><script>alert(1);</script></span>
u.html_escape的结果,指定变量被escape,不包括前后的span:
<span><script>alert(1);</script></span>
所以么,推荐用util.html_escape