python - 为什么“千万不要对未知的变量使用safe过滤器”?
黄舟
黄舟 2017-04-18 09:16:28
0
2
404

Jinja2文档里说:

safe(value)
Mark the value as safe which means that in an environment with automatic escaping enabled this variable will not be escaped.

那么就是说未知变量,如果使用了safe过滤器,然后就不会转义,就有安全隐患。请问会造成什么样的隐患呢?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
阿神

I have never been exposed to this language, but like many similar examples for reference, if the input is not filtered and certain conditions happen to be met, it is very likely that the program will crash due to malicious input, or even the user information will be lost.
The simplest example is to output HTML. If certain special symbols are not transferred, it will be easily attacked by malicious scripts.

I guess this language should have automatic filtering for input under certain conditions (an environment with automatic escaping enabled). If you mark it as safe, these filters will be removed and the safety will be reduced, so if it is external APIs and their inputs should not be marked safe without good reason.

黄舟

For example

<ul>
    <h1>{{ open('/path/2/password/') }}</h1>
</ul>

The extra large password will be output in the html...

WHY?

Because jinja2 is essentially a code generator, {{ var }} will be executed by python.

So the input needs to be escaped, and Jinja2 also enables transfer by default.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template