input() vs raw_input() 相关安全问题的简单解释

Simple explanation of security issues related to input() vs raw_input()

我正在阅读这个 Python 2.7 教程,他们正在阅读 raw_input(),其中提到:

The input() function will try to convert things you enter as if they were Python code, but it has security problems so you should avoid it.

我试着用谷歌搜索了一些解释,但我还是有点不清楚; input()raw_input() 所谓的固有安全问题的简单解释是什么?

Python 2.x 中的 input() 函数在返回之前计算事物。

作为示例,您可以看一下这个 -

>>> input("Enter Something : ")
Enter Something : exit()

这会导致程序退出(因为它会评估 exit())。

另一个例子-

>>> input("Enter something else :")
Enter something else :__import__("os").listdir('.')
['.gtkrc-1.2-gnome2', ...]

这样可以列出当前目录的内容,也可以使用os.chdir()os.remove()os.removedirs()os.rmdir()

等函数